pg_fsql
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_fsql | 1.1.0 | UTIL | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4110 | pg_fsql | No | Yes | No | Yes | No | No | fsql |
| Related | plpgsql plpgsql pg_readme schedoc |
|---|
Release tag 1.1.0 still ships extension SQL version 1.0; shared_preload_libraries is optional and only needed for session-start GUC availability.
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.1.0 | 1817161514 | pg_fsql | plpgsql |
| RPM | PIGSTY | 1.1.0 | 1817161514 | pg_fsql_$v | - |
| DEB | PIGSTY | 1.1.0 | 1817161514 | postgresql-$v-pg-fsql | - |
Build
You can build the RPM / DEB packages for pg_fsql using pig build:
pig build pkg pg_fsql # build RPM / DEB packages
Install
You can install pg_fsql directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
pig repo add pgsql -u # Add repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install pg_fsql; # Install for current active PG version
pig ext install -y pg_fsql -v 18 # PG 18
pig ext install -y pg_fsql -v 17 # PG 17
pig ext install -y pg_fsql -v 16 # PG 16
pig ext install -y pg_fsql -v 15 # PG 15
pig ext install -y pg_fsql -v 14 # PG 14
dnf install -y pg_fsql_18 # PG 18
dnf install -y pg_fsql_17 # PG 17
dnf install -y pg_fsql_16 # PG 16
dnf install -y pg_fsql_15 # PG 15
dnf install -y pg_fsql_14 # PG 14
apt install -y postgresql-18-pg-fsql # PG 18
apt install -y postgresql-17-pg-fsql # PG 17
apt install -y postgresql-16-pg-fsql # PG 16
apt install -y postgresql-15-pg-fsql # PG 15
apt install -y postgresql-14-pg-fsql # PG 14
Create Extension:
CREATE EXTENSION pg_fsql CASCADE; -- requires: plpgsql
Usage
Syntax:
CREATE EXTENSION pg_fsql; INSERT INTO fsql.templates (path, cmd, body) VALUES ('user_count', 'exec', 'SELECT jsonb_build_object(''total'', count(*)) FROM users WHERE status = {d[status]!r}'); SELECT fsql.run('user_count', '{"status":"active"}');Source: README
pg_fsql is a recursive SQL template engine for PostgreSQL. It combines a C-based placeholder renderer with PL/pgSQL template execution, hierarchical template composition, and optional SPI plan caching. The upstream project emphasizes that it does not require superuser privileges.
Core Objects
The extension installs two main catalog tables:
fsql.templates (
path varchar(500) primary key,
cmd varchar(50),
body text,
defaults text,
cached boolean default false
)
fsql.params (
key_param varchar(255) primary key,
type_param varchar(255) not null
)
path is dot-separated and defines the template hierarchy.
Template Commands
The README documents six command types:
execto execute SQL and returnjsonbrefto redirect to another templateifto choose a child branchexec_tplto execute SQL and re-render the result as a templatemapto collect children into a JSON objectNULLfor text fragments inserted into parents
Placeholders
The renderer supports placeholders such as:
{d[key]}{d[key]!r}forquote_literal{d[key]!j}for JSONB literals{d[key]!i}forquote_identifier
The special key _self injects the full input JSON object.
Public API
The upstream public functions include:
fsql.run(path, data, debug)to execute a template treefsql.render(path, data)to preview rendered SQLfsql.tree(path)to inspect hierarchyfsql.explain(path, data)to trace expansionfsql.validate()to check templatesfsql.depends_on(path)to inspect dependenciesfsql.clear_cache()to free cached SPI plans
Example
INSERT INTO fsql.templates (path, cmd, body) VALUES
('report', 'exec',
'SELECT jsonb_build_object(''data'', array_agg(row_to_json(t)))
FROM (SELECT {d[cols]} FROM {d[src]} {d[where]}) t'),
('report.cols', NULL, 'id, name, email'),
('report.src', NULL, 'customers'),
('report.where', NULL, 'WHERE city = {d[city]!r}');
SELECT fsql.run('report', '{"city":"Moscow"}');
SELECT fsql.render('report', '{"city":"Moscow"}');
Requirements
The README lists PostgreSQL 14+, plpgsql, and standard build dependencies such as gcc, make, and PostgreSQL server development headers.
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.