pgpyml
pgpyml · Run scikit-learn machine-learning models inside PostgreSQL through PL/Python functions and triggers.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100491 | pgpyml | pgpyml | 0.3.1 | RAG | PostgreSQL | Python |
| Relationships | |
|---|---|
| Requires | plpython3u |
Usage
Sources:
pgpyml loads Python joblib/scikit-learn models inside PostgreSQL and exposes prediction functions plus trigger helpers. Upstream labels it work in progress and not production-ready. Model loading uses untrusted Python and server-local files, so a model artifact is executable code from the database server’s security perspective.
Prerequisites and Core Workflow
Install plpython3u and the Python packages needed by the saved model in PostgreSQL’s Python environment, then create the extension in a dedicated schema:
The model path is resolved on the database server and must be readable by its service account. The nested array contains one feature vector per prediction, and the function returns a text array.
Important Objects
predictapplies a saved model to one or more feature arrays.predict_table_rowreads named columns from a table row before predicting.classification_triggerwrites a prediction into a target column on insert or update.trigger_abort_if_prediction_isandtrigger_abort_unless_prediction_isreject rows based on a class.- Combined trigger helpers both save a classification and conditionally reject the operation.
Trigger arguments include model paths and column names. Test generated triggers against schema changes, nulls, type conversions, model feature ordering, and prediction errors.
Security and Operations
plpython3u is untrusted, and joblib/pickle deserialization can execute arbitrary Python code. Only a tightly controlled administrator should install the extension, write model files, or call APIs with a model path. Store models in an immutable, service-owned directory and verify their digest and provenance before deployment.
Inference runs synchronously inside a backend or row trigger, increasing transaction latency and memory use; exceptions can abort writes. Python environment, native dependencies, model files, and exact scikit-learn versions are not captured by a database dump. Pin them separately, load-test concurrency, and prove restore and model compatibility before any non-disposable use.