Skip to main content

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 18 (generated from REL_18_3)
  • 727 grammar rules covering the full PostgreSQL SQL syntax
  • 494 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 (494 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 1.2.4

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 1.2.4
File Size Uploaded
tree_sitter_postgres-1.2.4.tar.gz 6.0 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for tree-sitter-postgres 1.2.4
File
tree_sitter_postgres-1.2.4-cp39-abi3-win_arm64.whl CPython 3.9 abi3 Windows ARM64 Details
tree_sitter_postgres-1.2.4-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details
tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details
tree_sitter_postgres-1.2.4-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-1.2.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.9 abi3 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
tree_sitter_postgres-1.2.4-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
tree_sitter_postgres-1.2.4-cp39-abi3-macosx_10_9_x86_64.whl CPython 3.9 abi3 macOS 10.9+ x86-64 Details

Total release size: 12.8 MB

Release files / tree_sitter_postgres-1.2.4.tar.gz

Download URL tree_sitter_postgres-1.2.4.tar.gz
Size 6.0 MB
Tags Source
SHA-256 checksum
How to use checksums
887ab99369e79af58f6f38b129b9640b94629f22c0e70ee2517049600f41dd38
BLAKE2b-256 checksum
How to use checksums
b15f40418ff505f3b531f1809aabbf5154a3cf3a9ed8106e6b3499eb698ad5ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

Release files / tree_sitter_postgres-1.2.4-cp39-abi3-win_arm64.whl

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-win_arm64.whl
Size 799.2 kB
Tags CPython 3.9 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
ab586f969fb0dba648de49edc8b6473affb2aba8e93e425d39260b059cc4cbd3
BLAKE2b-256 checksum
How to use checksums
ea2a898aca7aec34e3ece487010a15fae96830364955cb8b4ce9eec1df7c51f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

Release files / tree_sitter_postgres-1.2.4-cp39-abi3-win_amd64.whl

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-win_amd64.whl
Size 781.5 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
0cf15e61fc1cb8d809ae04de81d8c04f77486fb85e8e871826ab0ffd4f3c6bf7
BLAKE2b-256 checksum
How to use checksums
ef67c7711e529914c465501b15b6e95e7c991e31bee6144a09cbd08917b0bf92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

Release files / tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_x86_64.whl
Size 883.7 kB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
6c27b0bbb7fd666ce6054ab42e73557b43d1a7f2da432a03ee16d920b727e35c
BLAKE2b-256 checksum
How to use checksums
08ecbc9ef4a8dc88bfc4a1b0a242d2cce379a009bdf29b8dbe8e870c67b50d4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

Release files / tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_aarch64.whl
Size 893.6 kB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
33a80e82f349b3d7c996aaa86fd3e08add3a30a5d19954574b2343ca5e621be1
BLAKE2b-256 checksum
How to use checksums
35ddd2947015b586ad4044f5f7115ce13bdb25400e1dc78034833bb118acd97d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

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

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 892.6 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
f37ba332618cc41670200a24e440df93a8f3cd189cca00e9f11d25a9bddae7ac
BLAKE2b-256 checksum
How to use checksums
47c24cdac06437dc00f741d87ede6fcb08d297a5a33095e94e18318db23dcc01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

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

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 885.4 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
9354d438477a84bc105c9a022689f0f6e9a1dbd57ccdcc709bf7058bdca7e36c
BLAKE2b-256 checksum
How to use checksums
3e09769a97c9375b6da7b7a9f4e1946df9cf741c14e6a8d907eff0617ef946d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

Release files / tree_sitter_postgres-1.2.4-cp39-abi3-macosx_11_0_arm64.whl

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-macosx_11_0_arm64.whl
Size 900.9 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6be1e8f6c34e23ecb9a3363f56cadba4517191ad22be2ef789ffff27fbe6f50e
BLAKE2b-256 checksum
How to use checksums
3355a6fb9e6219b2322a15b31f1066f2bdcd5bd7b09a1c743422b7abf4e18895
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 2026.

Transparency log

Release files / tree_sitter_postgres-1.2.4-cp39-abi3-macosx_10_9_x86_64.whl

Download URL tree_sitter_postgres-1.2.4-cp39-abi3-macosx_10_9_x86_64.whl
Size 787.5 kB
Tags CPython 3.9 abi3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7c4cb1230acac921d50f5f9fa62b8b9ee7ad8b8cb1371b12ac43a862e4d7aa1b
BLAKE2b-256 checksum
How to use checksums
ad5c9d82b495c143969807b9727336491c31eb867f7f792f366e99d57f95dc58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 15, 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