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 19 beta 2 (generated from REL_19_BETA2)
  • 784 grammar rules covering the full PostgreSQL SQL syntax
  • 511 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 (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

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-19.0.0b2.tar.gz (6.9 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-19.0.0b2-cp39-abi3-win_arm64.whl (874.0 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-19.0.0b2-cp39-abi3-win_amd64.whl (855.4 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-19.0.0b2-cp39-abi3-musllinux_1_2_x86_64.whl (959.3 kB view details)

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

tree_sitter_postgres-19.0.0b2-cp39-abi3-musllinux_1_2_aarch64.whl (969.1 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-19.0.0b2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (967.4 kB view details)

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

tree_sitter_postgres-19.0.0b2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (959.9 kB view details)

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

tree_sitter_postgres-19.0.0b2-cp39-abi3-macosx_11_0_arm64.whl (992.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-19.0.0b2-cp39-abi3-macosx_10_9_x86_64.whl (862.4 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_postgres-19.0.0b2.tar.gz.

File metadata

  • Download URL: tree_sitter_postgres-19.0.0b2.tar.gz
  • Upload date:
  • Size: 6.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tree_sitter_postgres-19.0.0b2.tar.gz
Algorithm Hash digest
SHA256 4e5b7a8d263a0a5ec2d7464c7045b82e632a431f96f75174ee47b3d56578a066
MD5 4e7a10b6ee7e80067e694015acb440d3
BLAKE2b-256 f2bf8667501dd91b5d4266217d5eae6707f5889e1c1f6304dd98ea4e11da995e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2.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-19.0.0b2-cp39-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-19.0.0b2-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 7d62ee7d98ae0cb6fc20fb0af9bd680b6b87e4f05a9f8f32dc0f8176a530ee22
MD5 81edb82ec366dc8c4729a33432861a73
BLAKE2b-256 f182b30d24aa5e421d9f3f0912a62e1877b08d345f08269251dd982f10e701b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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-19.0.0b2-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-19.0.0b2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d4f9f5cc254c68337f548284967d6621116df9dcb224e38bdc56feba99fe1c5e
MD5 deea69129847c7f74c797eee3fec9c63
BLAKE2b-256 65f124dfb6c46184f4fc9cb7e4d974b2cb7ce5e9aa5a61ef1f17fb8aa7eb1367

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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-19.0.0b2-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-19.0.0b2-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 042d4df04ced781a1d622d6ef09bd08a76cd8313ccde22e0fe920a00240667fb
MD5 d1ba0472485c9ff7d0e09dc3ba9e7102
BLAKE2b-256 56edecca8aa540034fd9e10ee85476875c19c106e0fc14c974630111b26d7a03

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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-19.0.0b2-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-19.0.0b2-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f15484bab44ef983079f4e394e3b7050a95d573492191cb6aa7bdad7a7a8897d
MD5 61cd996c104632238d15fd865431546d
BLAKE2b-256 1dfeaf8330fbf3901c381828d85eb13ce1d6637da2d9a0a790e09bc5490869fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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-19.0.0b2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-19.0.0b2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2fd14230fb19a967f12278f89d3d66ced97cfcdc86712210408bf67cc128be97
MD5 bfafa47effa2b4ab66087bcd46b88160
BLAKE2b-256 8fbbfda739ddf4e9628ae4eae43a6547ba6b964564d10171da48d0a486299ffa

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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-19.0.0b2-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-19.0.0b2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 93357e0509ccfd9e7426d7b8606fa65a58e8f94243cdcd8a8eca919a3ae013f1
MD5 9223395b251d6c7d87dc194636169382
BLAKE2b-256 1fa629cb72375f81a74d8da6172a2a9208a5976b5ac5b5ebbcbb24cfab7f76e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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-19.0.0b2-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-19.0.0b2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33f64fa5edc0460ea5b8bdd6abf3ab7032cf1bc46471c167af494e0746fd6df7
MD5 43ff31dfcf337e254828b7bf36ae98ba
BLAKE2b-256 e98af51b1a4e98ca627a076db0162ed88b10cb09e47b7d4707e70050638245fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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-19.0.0b2-cp39-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-19.0.0b2-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f0feca22da4d3d8bbaa53d5c6defde4266248d0359453704473ce3ef63e9f27f
MD5 eda3e59d6f1393c44a5bf9bb8c135a6e
BLAKE2b-256 3f5cc074ea696588db92529277bf8efa4381632bd76ff6ad2a283d73b83bdb80

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-19.0.0b2-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