Skip to content

pg_sexp

Adds a compact S-expression type with parsing, list operations, matching, containment, and hash/GIN indexing.

pg_sexp · Adds a compact S-expression type with parsing, list operations, matching, containment, and hash/GIN indexing.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
101089pg_sexppg_sexp0.1.0TYPEApache-2.0C

PostgreSQL · 14, 15, 16, 17, 18

Usage

Sources:

pg_sexp adds a compact binary sexp type for Lisp-style S-expressions. Use it when PostgreSQL must store, inspect, match, and index symbolic lists without flattening them into text.

Core Workflow

CREATE EXTENSION pg_sexp;

CREATE TABLE document (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    expr sexp NOT NULL
);

INSERT INTO document (expr)
VALUES ('(user (name "alice") (age 30))');

SELECT id, car(expr), cdr(expr)
FROM document
WHERE expr @>> '(name "alice")';

CREATE INDEX document_expr_gin
ON document USING gin (expr sexp_gin_ops);

Objects and Operators

  • car(sexp), cdr(sexp), nth(sexp, integer), head(sexp), and sexp_length(sexp) inspect lists.
  • sexp_typeof(sexp) and the is_list, is_atom, is_symbol, is_string, is_number, and is_nil predicates inspect value kinds.
  • @> performs structural containment; @>> performs key-based, order-independent containment; @~ and sexp_find support wildcard, rest, and capture patterns.
  • sexp_gin_ops supports GIN containment searches, while equality has hash-index support.

Compatibility and Caveats

Upstream requires PostgreSQL 14 or later. Version 0.1.0 is pre-1.0, so pin the binary format and extension version before storing durable data. Test parser canonicalization, symbol versus string and integer versus float distinctions, duplicate keys, nesting depth, wildcard complexity, and agreement between indexed and sequential results. Deeply nested or adversarial expressions and broad wildcard searches may be expensive; apply input and query limits at the application boundary.

Table reader