fbsql
fbsql : Closure-preserving formula-based statistical modeling in SQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4695 | fbsql
|
fbsql
|
0.1.0 |
FUNC
|
MIT
|
SQL
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
----d--
|
No
|
No
|
No
|
Yes
|
no
|
no
|
| Relationships | |
|---|---|
| Schemas | fbsql |
| Requires | plr
|
| See Also | pg4ml
pgml
pg_math
weighted_statistics
|
Requires PL/R 8.4.0 or newer; PIGSTY packages target PostgreSQL 16 through 18.
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY
|
0.1.0 |
18
17
16
15
14
|
fbsql |
plr |
| RPM | PIGSTY
|
0.1.0 |
18
17
16
15
14
|
fbsql_$v |
plr_$v |
| DEB | PIGSTY
|
0.1.0 |
18
17
16
15
14
|
postgresql-$v-fbsql |
postgresql-$v-plr |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
el8.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
el9.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
el9.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
el10.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
el10.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
d12.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
d12.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
d13.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
d13.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
u22.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
u22.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
u24.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
u24.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
u26.x86_64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
u26.aarch64
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
PIGSTY 0.1.0
|
MISS
|
MISS
|
Source
pig build pkg fbsql; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install fbsql; # install via package name, for the active PG version
pig install fbsql -v 18; # install for PG 18
pig install fbsql -v 17; # install for PG 17
pig install fbsql -v 16; # install for PG 16Create this extension with:
CREATE EXTENSION fbsql CASCADE; -- requires plrUsage
Sources:
fbsql is a proof-of-concept statistical-modeling DSL that keeps fitting and prediction relational: SQL queries go in and rows come back, while models are described with R formula syntax. Release 0.1.0 implements generalized linear models through PL/R for fitting and pure PL/pgSQL for prediction.
Prerequisites
FbSQL was developed and tested with PostgreSQL 16 and requires PL/R 8.4.0 or newer plus R. plr is an untrusted language, so a superuser must install the dependency and extension.
CREATE EXTENSION fbsql CASCADE;
SELECT fbsql.version();Grant regular users only the function access and source-data privileges they require.
Core Workflow
Fit a binomial churn model and retain the returned relation:
CREATE TEMP TABLE churn_model AS
SELECT *
FROM fbsql.fit_glm(
relation => $$
SELECT churn_flag, age, gender
FROM customer
WHERE created_at >= DATE '2025-01-01'
AND created_at < DATE '2026-01-01'
$$,
formula => 'churn_flag ~ age + gender',
family => 'binomial'
);Prediction accepts a query for new rows and a query returning the saved model. Because it returns SETOF record, supply the output columns at the call site:
SELECT customer_id, churn_flag_predicted
FROM fbsql.predict_glm(
relation => $$SELECT customer_id, age, gender FROM customer_2026$$,
model => $$SELECT * FROM churn_model$$
) AS p(
customer_id bigint,
age integer,
gender text,
churn_flag_predicted double precision
);Important Objects
fbsql.fit_glm(relation, formula, family)returns one row per model term, repeated fit statistics, andmetadata jsonbcontaining the information needed for prediction.fbsql.predict_glm(relation, model, on_new_levels)appends<response>_predictedto the input rows.on_new_levelsiserrorby default ornato produce a null prediction for unseen factor levels.fbsql.version()reports the extension version.
Supported Surface and Caveats
Version 0.1.0 supports Gaussian models with the identity link and binomial models with the logit link, using numeric and factor predictors. Fitting applies complete-case analysis and reports used and dropped row counts; prediction returns NULL when a predictor is null. Prediction uses stored coefficients and metadata and does not invoke R at runtime.
Interactions, custom contrasts, offsets, weights, prediction intervals, additional families and links, and distributed fitting are not supported. The relation and model parameters contain SQL text: construct them from trusted SQL, not unsanitized user input, and review the executing role’s privileges.