Version-controlling ParadeDB queries means storing each query revision in a structured table so you can audit, compare, and roll back SQL used by ParadeDB.
Query version control records every change to the SQL text that powers ParadeDB so you can safely iterate, audit, and revert. Instead of editing live views, you append new versions to a metadata table and reference only the latest version in production.
You create a query_versions
table, enforce one active version per query name, and wrap execution in a helper function that always returns the newest SQL.Developers update a query by inserting a new row with a higher version.
Use this pattern when ParadeDB drives product search or recommendation logic that changes often.Versioning lets you A/B test different vector similarity thresholds, rollback performance regressions, and document why changes were made.
You need:
query_versions
– stores name, version, SQL, author, notes.query_latest
– a materialized view or ON CONFLICT
-driven unique index to keep one active version.CREATE TABLE query_versions (
id serial PRIMARY KEY,
name text NOT NULL,
version int NOT NULL,
sql text NOT NULL,
author text,
notes text,
created_at timestamptz DEFAULT now(),
UNIQUE(name, version)
);.
CREATE UNIQUE INDEX query_latest_idx
ON query_versions (name)
WHERE version IS DISTINCT FROM NULL; -- ensures one latest row
Insert the new SQL with version = (SELECT COALESCE(MAX(version),0)+1 FROM query_versions WHERE name = 'customer_ltv')
.The unique-partial index guarantees only one latest version remains.
CREATE OR REPLACE FUNCTION run_query(p_name text)
RETURNS SETOF record LANGUAGE plpgsql AS $$
DECLARE
v_sql text;
BEGIN
SELECT sql INTO v_sql
FROM query_versions
WHERE name = p_name
ORDER BY version DESC
LIMIT 1;.
RETURN QUERY EXECUTE v_sql;
END;$$;
Call it with SELECT * FROM run_query('customer_ltv')
.
Yes. Use LAG(sql) OVER (PARTITION BY name ORDER BY version)
to show line-level diffs or export to a Git-style patch.
Avoid updating rows in place; that loses audit history.Always INSERT
new versions.
Add a check constraint to prevent version gaps if you rely on sequential integers.
.
Both matter. Git tracks code, while the database table records the exact SQL that actually ran in production, including hot-fixes.
No. PostgreSQL compiles the executed SQL at runtime. Overhead is negligible for typical ParadeDB workloads.
Yes. Either delete the latest row or insert a new row that copies the old SQL with a higher version number.