{"id":21300913,"url":"https://github.com/kibae/pg_onnx","last_synced_at":"2026-04-28T19:01:08.168Z","repository":{"id":195041002,"uuid":"692061993","full_name":"kibae/pg_onnx","owner":"kibae","description":"pg_onnx: ONNX Runtime integrated with PostgreSQL. Perform ML inference with data in your database.","archived":false,"fork":false,"pushed_at":"2026-03-18T15:18:46.000Z","size":105,"stargazers_count":58,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-03-19T05:42:41.388Z","etag":null,"topics":["ai","contributions-welcome","cuda","deep-learning","inference","machine-learning","onnx","onnxruntime","postgresql","postgresql-extension"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kibae.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-09-15T13:27:34.000Z","updated_at":"2026-03-18T15:18:26.000Z","dependencies_parsed_at":"2023-10-14T18:16:16.548Z","dependency_job_id":"79a885ff-eab4-4284-a4f3-a26a0ebe9432","html_url":"https://github.com/kibae/pg_onnx","commit_stats":{"total_commits":39,"total_committers":1,"mean_commits":39.0,"dds":0.0,"last_synced_commit":"89ea24302c7d3d444f4a249f1b34e7fbdddd8cfd"},"previous_names":["kibae/pg_onnx"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/kibae/pg_onnx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kibae%2Fpg_onnx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kibae%2Fpg_onnx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kibae%2Fpg_onnx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kibae%2Fpg_onnx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kibae","download_url":"https://codeload.github.com/kibae/pg_onnx/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kibae%2Fpg_onnx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32394478,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T14:34:11.604Z","status":"ssl_error","status_checked_at":"2026-04-28T14:32:37.009Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","contributions-welcome","cuda","deep-learning","inference","machine-learning","onnx","onnxruntime","postgresql","postgresql-extension"],"created_at":"2024-11-21T15:42:14.771Z","updated_at":"2026-04-28T19:01:08.160Z","avatar_url":"https://github.com/kibae.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pg_onnx\n\n[![ONNX Runtime](https://img.shields.io/github/v/release/microsoft/onnxruntime?filter=v1.25.1\u0026label=ONNX%20Runtime)](https://github.com/microsoft/onnxruntime)\n[![License](https://img.shields.io/github/license/kibae/pg_onnx)](https://github.com/kibae/pg_onnx/blob/main/LICENSE)\n\n----\n\n- [ONNX: Open Neural Network Exchange](https://onnx.ai/)\n- **ONNX Runtime integrated with PostgreSQL. Perform ML inference with data in your database.**\n- PostgreSQL creates a process every new client connects. If every process ran ONNX inference, the system and GPU would\n  run out of memory. pg_onnx runs **[onnxruntime-server](https://github.com/kibae/onnxruntime-server)** as a background\n  worker and creates and recycles one onnx runtime session per ONNX file.\n\n```mermaid\ngraph LR\n    subgraph P[PostgreSQL]\n        direction LR\n        PM((postmaster))\n        PS1[postgres]\n        PS2[postgres]\n        PS3[postgres]\n        TABLE[(onnx model data)]\n        subgraph ONNX[pg_onnx Background Worker]\n            direction TB\n            OS[onnxruntime-server]\n            ONNX1([onnxruntime\\nsession:\\nmodel1])\n            ONNX2([onnxruntime\\nsession:\\nmodel2])\n            OS -. \" create/execute session \" .-o ONNX1 \u0026 ONNX2\n        end\n        PM -. \" fork \" .-o PS1 \u0026 PS2 \u0026 PS3\n        PM -. \" dynamic background worker \" .-o ONNX\n        PS3 \u003c-- \" import model \" --\u003e TABLE\n        PS1 \u0026 PS2 \u0026 PS3 \u003c-- \" ONNX operation \" --\u003e ONNX\n    end\n    C[Client trying to use pg_onnx] ==\u003e PS3\n```\n\n----\n\n\u003c!-- TOC --\u003e\n\n- [Supported Platforms and PostgreSQL Versions](#supported-platforms-and-postgresql-versions)\n- [Build pg_onnx](#build-pg_onnx)\n    - [Requirements](#requirements)\n    - [Install ONNX Runtime](#install-onnx-runtime)\n    - [Install dependencies](#install-dependencies)\n    - [Compile and Install](#compile-and-install)\n- [Functions](#functions)\n    - [ONNX Model Functions](https://github.com/kibae/pg_onnx/wiki/Functions#onnx-model-functions)\n        - [pg_onnx_import_model(TEXT, TEXT, BYTEA, JSONB, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_import_modeltext-text-bytea-jsonb-text)\n        - [pg_onnx_drop_model(TEXT, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_drop_modeltext-text)\n        - [pg_onnx_list_model()](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_list_model)\n        - [pg_onnx_inspect_model_bin(BYTEA, JSONB)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_inspect_model_bin-bytea-jsonb)\n    - [ONNX Session Functions](https://github.com/kibae/pg_onnx/wiki/Functions#onnx-session-functions)\n        - [pg_onnx_create_session(TEXT, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_create_sessiontext-text)\n        - [pg_onnx_describe_session(TEXT, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_describe_sessiontext-text)\n        - [pg_onnx_execute_session(TEXT, TEXT, JSONB)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_execute_sessiontext-text-jsonb)\n        - [pg_onnx_destroy_session(TEXT, TEXT, JSONB)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_destroy_sessiontext-text-jsonb)\n        - [pg_onnx_list_session()](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_list_session)\n- [How to use](#how-to-use)\n    - [Install extension](#install-extension)\n    - [Simple Usage](#simple-usage)\n    - [Using inference results with triggers](#using-inference-results-with-triggers)\n\n----\n\n# Supported Platforms and PostgreSQL Versions\n\n| Platform      | Ubuntu                                                                                                                                                        | MacOS                                                                                                                                                       |\n|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| PostgreSQL 13 | ![Ubuntu PG13](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-ubuntu-pgsql13.json) | Not tested (Homebrew no longer provides PG13)                                                                                                               |\n| PostgreSQL 14 | ![Ubuntu PG14](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-ubuntu-pgsql14.json) | ![MacOS PG14](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-macos-pgsql14.json) |\n| PostgreSQL 15 | ![Ubuntu PG15](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-ubuntu-pgsql15.json) | ![MacOS PG15](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-macos-pgsql15.json) |\n| PostgreSQL 16 | ![Ubuntu PG16](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-ubuntu-pgsql16.json) | ![MacOS PG16](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-macos-pgsql16.json) |\n| PostgreSQL 17 | ![Ubuntu PG17](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-ubuntu-pgsql17.json) | ![MacOS PG17](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-macos-pgsql17.json) |\n| PostgreSQL 18 | ![Ubuntu PG18](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-ubuntu-pgsql18.json) | ![MacOS PG18](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kibae/a6e5351fdbced3d780f4f153e99de723/raw/pg_onnx-macos-pgsql18.json) |\n\n----\n\n# Build pg_onnx\n\n## Requirements\n\n- [PostgreSQL](https://www.postgresql.org/) \u003e= 13\n- [ONNX Runtime](https://onnxruntime.ai/)\n- [Boost](https://www.boost.org/)\n- [CMake](https://cmake.org/)\n- CUDA(*optional, for Nvidia GPU support*)\n\n----\n\n## Install ONNX Runtime\n\n#### Linux\n\n- Use `download-onnxruntime-linux.sh` script\n    - This script downloads the latest version of the binary and install to `/usr/local/onnxruntime`.\n    - Also, add `/usr/local/onnxruntime/lib` to `/etc/ld.so.conf.d/onnxruntime.conf` and run `ldconfig`.\n- Or manually download binary from [ONNX Runtime Releases](https://github.com/microsoft/onnxruntime/releases).\n\n#### Mac OS\n\n```shell\nbrew install onnxruntime\n```\n\n----\n\n## Install dependencies\n\n#### Ubuntu/Debian\n\n```shell\nsudo apt install cmake libboost-all-dev libpq-dev postgresql-server-dev-all\n```\n\n##### (optional) CUDA support (CUDA 12.x/13.x, cuDNN 9.x)\n\n- Follow the instructions below to install the CUDA Toolkit and cuDNN.\n    - [CUDA Toolkit Installation Guide](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html)\n    - [CUDA Download for Ubuntu](https://developer.nvidia.com/cuda-downloads?target_os=Linux\u0026target_arch=x86_64\u0026Distribution=Ubuntu\u0026target_version=22.04\u0026target_type=deb_network)\n\n```shell\n# CUDA 12\nsudo apt install cuda-toolkit-12 libcudnn9-dev-cuda-12\n# CUDA 13\nsudo apt install cuda-toolkit-13 libcudnn9-dev-cuda-13\n```\n\n#### Mac OS\n\n```shell\nbrew install cmake boost postgresql\n```\n\n----\n\n## Compile and Install\n\n#### Git clone\n\n- Clone the repository with submodules.\n    - [ONNX Runtime Server](https://github.com/kibae/onnxruntime-server) is included as a submodule.\n    - If you already cloned the repository, run `git submodule update --init --recursive` to update submodules.\n\n```shell\ngit clone --recursive https://github.com/kibae/pg_onnx.git\n```\n\n```shell\ncmake -B build -S . -DCMAKE_BUILD_TYPE=Release\ncmake --build build --target pg_onnx --parallel\nsudo cmake --install build/pg_onnx\n```\n\n----\n\n# Functions\n\n- Provides several functions for importing ONNX file and executing and managing it.\n- [ONNX Model Functions](https://github.com/kibae/pg_onnx/wiki/Functions#onnx-model-functions)\n    - [pg_onnx_import_model(TEXT, TEXT, BYTEA, JSONB, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_import_modeltext-text-bytea-jsonb-text)\n    - [pg_onnx_drop_model(TEXT, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_drop_modeltext-text)\n    - [pg_onnx_list_model()](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_list_model)\n    - [pg_onnx_inspect_model_bin(BYTEA, JSONB)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_inspect_model_bin-bytea-jsonb)\n- [ONNX Session Functions](https://github.com/kibae/pg_onnx/wiki/Functions#onnx-session-functions)\n    - [pg_onnx_create_session(TEXT, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_create_sessiontext-text)\n    - [pg_onnx_describe_session(TEXT, TEXT)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_describe_sessiontext-text)\n    - [pg_onnx_execute_session(TEXT, TEXT, JSONB)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_execute_sessiontext-text-jsonb)\n    - [pg_onnx_destroy_session(TEXT, TEXT, JSONB)](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_destroy_sessiontext-text-jsonb)\n    - [pg_onnx_list_session()](https://github.com/kibae/pg_onnx/wiki/Functions#pg_onnx_list_session)\n\n----\n\n# How to use\n\n## Install extension\n\n```sql\nCREATE\nEXTENSION IF NOT EXISTS pg_onnx;\n```\n\n## Simple Usage\n\n- Import an ONNX file and get the inference results.\n\n```sql\nSELECT pg_onnx_import_model(\n               'sample_model', --------------- model name\n               'v20230101', ------------------ model version \n               PG_READ_BINARY_FILE('/your_model_path/model.onnx')::bytea, -- model binary data\n               '{\"cuda\": true}'::jsonb, ------ options\n               'sample model' ---------------- description\n       );\n\nSELECT pg_onnx_execute_session(\n               'sample_model', -- model name\n               'v20230101', ----- model version\n               '{\n                 \"x\": [[1], [2], [3]],\n                 \"y\": [[3], [4], [5]],\n                 \"z\": [[5], [6], [7]]\n               }' --------------- inputs\n       );\n```\n\n- Depending on the type and shape of the inputs and outputs of the ML model, you can see different results. Below is an\n  example of the result.\n\n```\n                            pg_onnx_execute                             \n--------------------------------------------------------------------------------\n {\"output\": [[0.7488641738891602], [0.8607008457183838], [0.9725375175476074]]}\n```\n\n## Using inference results with triggers\n\n- When data is added, use the BEFORE INSERT trigger to update some columns with ML inference results.\n- *Depending on your ML model, this can have a significant performance impact, so be careful when using it.*\n- [Example](https://github.com/kibae/pg_onnx/blob/main/pg_onnx/expected/05-trigger.out)\n\n```sql\n-- Create a test table\nCREATE TABLE trigger_test\n(\n    id         SERIAL PRIMARY KEY,\n    value1     INT,\n    value2     INT,\n    value3     INT,\n    prediction FLOAT\n);\n\n-- Create a trigger function\nCREATE\nOR REPLACE FUNCTION trigger_test_insert()\n    RETURNS TRIGGER AS\n$$\nDECLARE\nresult jsonb;\nBEGIN\n    result\n:= pg_onnx_execute_session(\n            'sample_model', 'v20230101',\n            JSONB_BUILD_OBJECT(\n                    'x', ARRAY [[NEW.value1]],\n                    'y', ARRAY [[NEW.value2]],\n                    'z', ARRAY [[NEW.value3]]));\n\n    -- output shape: float[-1,1]\n    -- eg: {\"output\": [[0.6492120623588562]]}\n    NEW.prediction\n:= result -\u003e 'output' -\u003e 0 -\u003e 0;\nRETURN NEW;\nEND;\n$$\nLANGUAGE plpgsql;\n\n-- Create a trigger\nCREATE TRIGGER trigger_test_insert\n    BEFORE INSERT\n    ON trigger_test\n    FOR EACH ROW\n    EXECUTE PROCEDURE trigger_test_insert();\n```\n\n## ORT Extensions Support (onnxruntime-extensions)\n\nIf your model relies on custom ops provided by onnxruntime-extensions, you can load the extension library by passing\n`ortextensions_path` in the options when importing the model.\n\n```sql\nSELECT pg_onnx_import_model(\n               'e5-tok',\n               'v1',\n               PG_READ_BINARY_FILE('/PATH/tokenizer.onnx')::bytea,\n               '{\"ortextensions_path\": \"libortextensions.so\"}'::jsonb,\n               'e5 tokenizer'\n       );\n```\n\n## Contributors\n\n\u003ca href=\"https://github.com/kibae/pg_onnx/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=kibae/pg_onnx\" /\u003e\n\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkibae%2Fpg_onnx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkibae%2Fpg_onnx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkibae%2Fpg_onnx/lists"}