Skip to main content

Postgres grammar for tree-sitter

Project description

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 precedence1 + 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_postgres-1.1.1.tar.gz (5.6 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_postgres-1.1.1-cp39-abi3-win_arm64.whl (747.8 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.1.1-cp39-abi3-win_amd64.whl (764.3 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_x86_64.whl (823.2 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_aarch64.whl (832.6 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (832.2 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tree_sitter_postgres-1.1.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (825.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_postgres-1.1.1-cp39-abi3-macosx_11_0_arm64.whl (841.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.1.1-cp39-abi3-macosx_10_9_x86_64.whl (734.0 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_postgres-1.1.1.tar.gz.

File metadata

  • Download URL: tree_sitter_postgres-1.1.1.tar.gz
  • Upload date:
  • Size: 5.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tree_sitter_postgres-1.1.1.tar.gz
Algorithm Hash digest
SHA256 bd5bcc23ed879601acb103e39be8f27b5497ae2fc522b54960d8a90d9b3f841e
MD5 fa98e3de8b71661c895ae43a3724b47e
BLAKE2b-256 8a2db03cfc954682123bac3d2e409cace10ee21458c267f63b50ed03b81b4670

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1.tar.gz:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 610e4d712ac621f1b42ba5ef405d510f1ab5a8289667641267f0a2325d41aa5a
MD5 7460dd45e76706fc7a7203c4952687cc
BLAKE2b-256 55fe0a469c93e6d3db4946efd38107bb464ec987114dea05b2c136b31f72fdd9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-win_arm64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 33fdc2f48836e56a2200f649915642895f9ef1c3750fecf55457b95b000ee392
MD5 0805f2a16435bc6809e28e978c5096fb
BLAKE2b-256 724354a6d2dd029e2bd081ecb517de2a8f2fe6b0366e6f643abfb828a5bd96ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-win_amd64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8bc7cafd21bedb6e12cc719efe2f6d39c11e0cc9d9e31fd93aede152a2a073a9
MD5 1082294f26e06fd2e3c584d74c45223d
BLAKE2b-256 fcad3aa386faa7040315d7940e951c0e5f9070dc72b3d200de3bb02301165e1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 772d3c63491e2b6b47c72b07115c39f72ef2b28e5bc1dc354f8bb2e5a6d4ded6
MD5 173ceb910bd7a2eec9fe522b39e0ab07
BLAKE2b-256 aec6c7143f28a2258b836c3e55f3a6b2a59591f96e0747a1cb43ea0810b1e477

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-musllinux_1_2_aarch64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9c7b679a8437a5bf11a461f6baed3dda5b65c394b471411adf428e5b39c07908
MD5 fc661b0edaf0bd43a70d1a52254bcdf3
BLAKE2b-256 61b1d17ebf8b0e19d725c4f0f68211d77ac7f5981907e098aad73a9ac0645e16

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 39bfdaf22120499f803afe5a54192c03ebef818c15140b7138984a1ea7e53540
MD5 0af3296dc8a89b04ceba455b8c8a2869
BLAKE2b-256 163a5d1d4d71696213a34f3afc5e0fc44297ba622ef87693bda10b4cc9597bc2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0fc22736c54faef4e1189547bf8ad390ce4f9bcf254ff583ee956a470e131307
MD5 9469e85197fe35a9161937271234be1b
BLAKE2b-256 2b625d5b29bc71ce934334f968e6885ce4ba5cd05a98e5452bd56d75930341a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_postgres-1.1.1-cp39-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.1-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 99a08fe6b8ca044467e905e2cca8cdad3e69e45542f1db33acf38077f59fdfa0
MD5 7b79f9f4b61669750f90e8064632af51
BLAKE2b-256 abf8c57b44b772bb2173f487ac49c4011d9497ca0ad0539fad408864954fb567

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.1-cp39-abi3-macosx_10_9_x86_64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page