| Crates.io | pg_liquid |
| lib.rs | pg_liquid |
| version | 0.2.0 |
| created_at | 2025-10-03 09:31:08.137754+00 |
| updated_at | 2025-10-03 09:31:08.137754+00 |
| description | A PostgreSQL extension for Liquid template processing. |
| homepage | |
| repository | https://github.com/joelonsql/pg_liquid |
| max_upload_size | |
| id | 1866437 |
| size | 92,237 |
A high-performance PostgreSQL extension for Liquid template processing, built with Rust and pgrx.
pg_liquid brings the power of Liquid templating directly into PostgreSQL. This extension provides two main functions for validating and rendering Liquid templates with JSON data, enabling dynamic content generation within your database.
Liquid is a safe, template language originally created by Shopify and used by Jekyll, GitHub Pages, and many other platforms. With pg_liquid, you can leverage this powerful templating system directly in your PostgreSQL queries and stored procedures.
cargo install --locked cargo-pgrx
Replace pg17 with your PostgreSQL version (pg13, pg14, pg15, pg16, or pg17):
cargo pgrx init --pg17 $(which pg_config)
# Clone just the pg_liquid directory or download the standalone package
git clone https://github.com/pitorg/pg_liquid
cd pg_liquid
cargo pgrx install --release
Connect to your PostgreSQL database and run:
CREATE EXTENSION liquid;
For development and testing:
cd pg_liquid
cargo pgrx run --release
Note: This extension is completely standalone and does not require any other components from the liquid-rust repository. It uses the published liquid crate from crates.io.
Architecture: The extension follows the pgrx pattern where the crate is named pg_liquid but creates an extension called liquid with functions in the liquid schema.
This will start a PostgreSQL instance with the extension already loaded.
Once installed, verify the extension works:
-- Connect to your PostgreSQL database
CREATE EXTENSION liquid;
-- Test basic functionality
SELECT liquid.check_valid_syntax('Hello {{ name }}!');
-- Should return: true
SELECT liquid.render('Hello {{ name }}!', '{"name": "World"}'::jsonb);
-- Should return: "Hello World!"
The extension provides two main functions in the liquid schema:
liquid.check_valid_syntax(liquid_code TEXT) โ BOOLEANValidates whether the provided Liquid template code has valid syntax.
Parameters:
liquid_code (TEXT): The Liquid template code to validateReturns:
BOOLEAN: true if the syntax is valid, false otherwiseExamples:
-- Valid syntax examples
SELECT liquid.check_valid_syntax('Hello {{ name }}!');
-- Returns: true
liquid.render(liquid_code TEXT, data JSONB) โ TEXTRenders a Liquid template with the provided JSON data.
Parameters:
liquid_code (TEXT): The Liquid template code to renderdata (JSONB): JSON object containing the data to use in template renderingReturns:
TEXT: The rendered template outputExamples:
-- Basic variable substitution
SELECT liquid.render(
'Hello {{ user.name }}! You have {{ user.credits }} credits.',
'{
"user": {
"name": "Alice",
"credits": 100
}
}'::jsonb
);
-- Returns: "Hello Alice! You have 100 credits."
-- Conditional rendering
SELECT liquid.render(
'{% if user.is_premium %}Premium user{% else %}Regular user{% endif %}',
'{"user": {"is_premium": true}}'::jsonb
);
-- Returns: "Premium user"
pg_liquid supports the complete Liquid syntax.
pg_liquid is built with performance in mind:
Real-world performance testing shows excellent throughput for template rendering:
-- Test: 1 million simple liquid template renders
SELECT count(*) FROM (
SELECT liquid.render(
'Hej {{ user.name }}'::text,
format('{"user":{"name":"%s"}}', generate_series)::jsonb
)
FROM generate_series(1,1000000)
) AS x;
-- Result: 1,000,000 renders in 13.716 seconds
-- Performance: ~73,000 renders per second
-- Per-operation: ~13.7 microseconds per render
Performance Summary:
replace() (expected for full template engine)The extension handles errors gracefully:
check_valid_syntax() to check templates before rendering-- Validate before rendering
SELECT CASE
WHEN liquid.check_valid_syntax('{{ user.name }') THEN
liquid.render('{{ user.name }', '{"user": {"name": "Alice"}}'::jsonb)
ELSE
'Invalid template syntax'
END;
Run the test suite:
cargo pgrx test
This will run both Rust unit tests and PostgreSQL integration tests.
cargo install cargo-pgrxcargo pgrx initcargo pgrx testcargo pgrx runDependencies: This extension only depends on the published liquid crate from crates.io and does not require any local dependencies.
This project is licensed under the MIT License - see the LICENSE file for details.