Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

tree-sitter-postgres

A tree-sitter grammar for PostgreSQL, generated directly from PostgreSQL's Bison grammar (gram.y) and keyword list (kwlist.h).

Features

  • Current as of PostgreSQL 19 (generated from REL_19_STABLE @ b368bdd2301, post-beta 3, with the reverted beta features removed)
  • 743 grammar rules covering the full PostgreSQL SQL syntax
  • 499 case-insensitive keywords across all four PG keyword categories
  • Correct operator precedence — 1 + 2 * 3 parses as 1 + (2 * 3)
  • PL/pgSQL support via a separate grammar with language injection
  • Generated, not hand-written — regenerate for any PostgreSQL version

Quick start

npm install
cd postgres && npx tree-sitter generate && npx tree-sitter test

Regenerating from PostgreSQL source

The grammar is generated from a local PostgreSQL checkout. Set PG_SOURCE_DIR to point at your PostgreSQL source tree:

export PG_SOURCE_DIR=/path/to/postgres

# Using just (recommended)
just generate

# Or run the script directly
node script/generate-grammar.js "$PG_SOURCE_DIR"
cd postgres && npx tree-sitter generate

Input files

File Source
src/backend/parser/gram.y Bison grammar (733 rules, 3236 alternatives)
src/include/parser/kwlist.h Keyword definitions (511 keywords)

Generator scripts

Script Purpose
script/generate-grammar.js Orchestrator — reads PG source, writes postgres/grammar.js
script/parse-gram-y.js Parses Bison grammar: rules, terminals, precedence, %prec annotations
script/parse-kwlist.js Parses keyword list into categories
script/codegen.js Generates tree-sitter grammar with precedence and optional-rule handling
postgres/harvest-conflicts.sh Iteratively discovers GLR conflicts needed by tree-sitter

Repository structure

postgres/               PostgreSQL SQL grammar
  grammar.js            Generated tree-sitter grammar
  src/                  Generated parser (C)
  test/corpus/          Test cases (35 tests)
  known-conflicts.json  GLR conflict pairs

plpgsql/                PL/pgSQL grammar
  grammar.js            Hand-written tree-sitter grammar
  src/scanner.c         External scanner for dollar-quoting and keywords
  test/corpus/          Test cases
  queries/              Highlights and injection queries

script/                 Shared generator code
  generate-grammar.js   SQL grammar orchestrator
  parse-gram-y.js       Bison parser
  parse-kwlist.js       Keyword parser
  codegen.js            Grammar code generator

bindings/               Language bindings (Node, Rust, Python, Go, Swift, C)

Design notes

Empty rule handling

Bison's /* EMPTY */ alternatives cannot be directly translated — tree-sitter forbids non-start rules that match the empty string. The generator propagates optionality upward via a fixpoint loop and wraps references with optional() at call sites.

Operator precedence

Binary operators are split into a separate a_expr_prec rule resolved by static precedence (no GLR), while complex patterns (IS, IN, BETWEEN, LIKE, subquery operators) stay in a_expr with GLR conflict resolution. Both prec.left/prec.right (generation-time) and prec.dynamic (runtime) are emitted.

PL/pgSQL

PL/pgSQL is implemented as a separate hand-written grammar in plpgsql/ with an external scanner for dollar-quoting and context-sensitive keywords. SQL expressions and statements within PL/pgSQL blocks are delegated to the postgres grammar via tree-sitter language injection (plpgsql/queries/injections.scm).

License

BSD 3-Clause

Release files for tree-sitter-postgres 19.0.0b4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tree-sitter-postgres 19.0.0b4
File Size Uploaded
tree_sitter_postgres-19.0.0b4.tar.gz 6.1 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for tree-sitter-postgres 19.0.0b4
File
tree_sitter_postgres-19.0.0b4-cp39-abi3-win_arm64.whl CPython 3.9 abi3 Windows ARM64 Details
tree_sitter_postgres-19.0.0b4-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
tree_sitter_postgres-19.0.0b4-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details
tree_sitter_postgres-19.0.0b4-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details
tree_sitter_postgres-19.0.0b4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
tree_sitter_postgres-19.0.0b4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.9 abi3 Linux glibc 2.28+ x86-64, Linux glibc 2.5+ x86-64 Details
tree_sitter_postgres-19.0.0b4-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
tree_sitter_postgres-19.0.0b4-cp39-abi3-macosx_10_9_x86_64.whl CPython 3.9 abi3 macOS 10.9+ x86-64 Details

Total release size: 12.9 MB

Release files / tree_sitter_postgres-19.0.0b4.tar.gz

Download URL tree_sitter_postgres-19.0.0b4.tar.gz
Size 6.1 MB
Tags Source
SHA-256 checksum
How to use checksums
a2f5c260a46fb0de91a7ac2d07b47601321eae9f675008a5949d3a53a6299294
BLAKE2b-256 checksum
How to use checksums
dfa98ad999cea5482fea710a36fcaa455c56ffd8b90349c1e91617321b00b3be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-win_arm64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-win_arm64.whl
Size 805.7 kB
Tags CPython 3.9 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
2ca641c6d1483aa786c225d79fe7a7c275075a9f7cdcf7b789ae2e88331a9a96
BLAKE2b-256 checksum
How to use checksums
27591a7089c5c4cd183d12cedca3ccc97712cac0c91cacdd28f56c7e1ceaa911
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-win_amd64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-win_amd64.whl
Size 787.1 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
1ff16de70b57fb8e2de397d30651dc6358174a2ca44b356bb171ab551a38220a
BLAKE2b-256 checksum
How to use checksums
463b27076097ddf680f546e5bce59f5557bbd08766b73341a3e8954b01c578c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-musllinux_1_2_x86_64.whl
Size 889.7 kB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
e4dcb8536d7d04ab3cfcfdf448dc3003701ed202f39b1382c88d3b10e388b913
BLAKE2b-256 checksum
How to use checksums
545d7a3c726611fdfbb7496025067a3da20a880b082faf235fea4cab5b0ae511
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-musllinux_1_2_aarch64.whl
Size 899.7 kB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
b5587f70a7cb1fc32afa9ccedc5ff2b6ed25694af8dab700b785539a2342914d
BLAKE2b-256 checksum
How to use checksums
0cbb91c5fbdf30125cac86a480739f3d8509b2ead89377354b0d0170a78c83d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 898.2 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
390d51932e3b257b792ce6b806f5e51ca3a556c25030f02dc79ee75fa351dae9
BLAKE2b-256 checksum
How to use checksums
c2f486384bb184b04bff012081791465e9ded6b62fb6c3a557defb4f00104104
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 890.0 kB
Tags CPython 3.9 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 abi3
SHA-256 checksum
How to use checksums
3f89d280c5a203c98f3e4af61eebe167a365d96a7804ba028ce300f33b7faf9f
BLAKE2b-256 checksum
How to use checksums
845fcfbd966c85ec35935536bc8a456092c517bc3ff3925640ab6f8b17fe9f4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-macosx_11_0_arm64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-macosx_11_0_arm64.whl
Size 907.1 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b8b62ca101c6daa87c16b556312ba98e41444038ac3bcf038cc99fb78a526927
BLAKE2b-256 checksum
How to use checksums
e31cf0eb4d176a61fbf658974fe3ff4617ac800aa37100c7c2a8facf3d3ce000
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / tree_sitter_postgres-19.0.0b4-cp39-abi3-macosx_10_9_x86_64.whl

Download URL tree_sitter_postgres-19.0.0b4-cp39-abi3-macosx_10_9_x86_64.whl
Size 793.8 kB
Tags CPython 3.9 abi3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
e7c9e38706ecfd6c29dbc7bb8e89565b7e0d04fcf2bd5c71b981c7d1f95f9fab
BLAKE2b-256 checksum
How to use checksums
45f058b92fd24acd892324320e2e3b31c9316a264add74def7a9a6bc0aff8e7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page