pg_gaps
pg_gaps · Probabilistic graphical models over incomplete relations
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 102416 | pg_gaps | pg_gaps | 0.2.0 | FEAT | PostgreSQL | C |
PostgreSQL · 17, 18, 19
Usage
Sources:
pg_gaps is an experimental Bayesian-model extension for incomplete relational data. Version 0.2.0 contains model catalogs, exact inference, optional sampling and conditional-probability learning. The control, installation SQL and changelog identify 0.2.0; the README status heading still says 0.1.0. It targets PostgreSQL 17–19 and installs in the fixed gaps schema.
Define a Small Model
Create the extension as a superuser. Model functions use the caller’s table permissions and the catalogs apply role-based row security.
A declared attribute has a finite domain; entity keys default to the table’s primary key. Stripes identify groups of random variables, factors attach conditional-probability tables, and parent mappings connect related entities. gaps.set_cpt() replaces a factor’s probabilities; gaps.learn_cpts() estimates them from complete observations with optional additive smoothing.
Interpret Results
gaps.marginals() reports a distribution for each missing value. Variables in one component can be correlated, so multiplying their marginal probabilities does not produce the joint distribution. gaps.impute() returns the joint most likely assignment for exact components. With sampling it returns per-variable marginal choices, not a joint optimum, and the joint-probability field is NULL.
Inference defaults to exact computation or an error when resource bounds are exceeded. Positive gaps.samples enables sampling for components beyond those bounds; agreement between chains is not proof of correctness. gaps.max_factor_cells and gaps.max_total_cells bound inference allocations.
Boundaries
The catalog tables and their sequences are registered for dump/restore. Data is read under caller privileges; learned probabilities become visible to roles allowed to read the model. Treat the learned model as data that can disclose information about its training observations.
Schema changes can invalidate model assumptions, and constants whose input depends on session settings remain an open issue. Validation is conservative and a failed structural proof does not always establish an invalid grounded model. Inference creates temporary tables and does not run on a hot standby. Keep model backups and test upgrades in an isolated database.
Learning and 0.2.0 Upgrade
Counts use Dirichlet smoothing or per-cell priors from gaps.set_prior; method => 'em' also learns from missing observations. gaps.set_frozen preserves selected CPTs. Composite models use gaps.add_member and infer the combined network; logistic and noisy-OR parameters are not learned. EM’s tol now means an absolute estimate of CPT-probability error, not relative objective change. Its default is 1e-6, max_iterations defaults to 500, and the new last_change output reports objective change in nats.
The supplied upgrade handles both released 0.1.0 and the earlier d35680c catalog. Install the matching library and close all old backends, including poolers; run the update from a fresh connection. Views/functions depending on changed signatures or results can block the atomic update. Save their definitions, deliberately drop and recreate the dependents, and review new result columns; do not use CASCADE as a shortcut.
Model data is retained. Existing table REVOKEs survive, but recreated functions regain PUBLIC EXECUTE, so reapply restrictions. Upgrading d35680c also creates gaps.model_member with default grants that may need restriction. The new library refuses incompatible old SQL for C operations until the update is complete.