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.0.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.0-cp39-abi3-win_arm64.whl (747.8 kB view details)

Uploaded CPython 3.9+Windows ARM64

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

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.1.0-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.0-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.0-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.0-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (825.5 kB view details)

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

tree_sitter_postgres-1.1.0-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.0-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.0.tar.gz.

File metadata

  • Download URL: tree_sitter_postgres-1.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 b94ef966368153dc85a9b1f540cf99be332e1111aece18b95202caada62d980b
MD5 67d337cfbd88fc330f63ebb2756b081a
BLAKE2b-256 f1260e177fa1dd388b91edee5085c3b8809b5d8862c049a075dee75779006961

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.0-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 af9ab5f10ad3a81b99f40a24019397f2f8137e9aeb5e232b5837630cb8234e7d
MD5 d87b516c8383bd1626035c29e9e3101e
BLAKE2b-256 bb11fd46a69ebf6d1350d90644d604ebbb10db7512ddd7fadc05e4de4d461c66

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d6ac84f9082af3e78028f0d17f174aa6cd43cd70374e008c338fa41bf2bb09e7
MD5 2314bd47640d542adde416af9269754a
BLAKE2b-256 b802d1cfca2d86987c095bd63c57150a52c095bd91afa11238cc0c3252e17e6b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dde06b17a7b7a15efa4058aa7cac7cfd056dd87d48505d7681d59f108452e80a
MD5 87c86d4fae87617b1c15b44dba9efc3d
BLAKE2b-256 0473d56d775845ad93175c4393fffbb11e86b8e20f71caff1897a7f3c678652c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 21333bdf7d02ff9d085a6ad07178a817f38fa99e3ff3d3fe1db71fb7cf6332ee
MD5 ab9704368d3dca40a68002da201d4297
BLAKE2b-256 b663f4ce12bebfc3354d9c798f44a8ee9f578ace74b9f665b92ee3af549bc944

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ae8e43f06ce242466379d5f334b519ddfe2c42384b539c946ec63710a425bdbb
MD5 a82381d9fba0dd5cdf963d0082e6805b
BLAKE2b-256 c0490dcd72ec20fbdf912aa627f33b4726a3419972119e6c379b7c3e07a82548

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.0-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.0-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.0-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 d28ff7327a49ccdacd750063cb30de9477b6777de29c613283996ea4126f4980
MD5 f767eae482dec34e2d76a1fe42ea6b51
BLAKE2b-256 2045833df16fef0fb4f3f69422bdda402b032505826f0ef779c87dedfda7c48f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 54fc77ff75ff207796969dc841144cb159be44c27e56ef9b5f6d07e9152eee11
MD5 4b5f7acdcaf70cbd8815b2b28e27e401
BLAKE2b-256 928e79b21d9e98acffca07970f4bb344802d98dcd8b849fc03987eb6acb0aefd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.0-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8a1f35f84954b62ad796cd342df01fe7dedca9113adeaa17eddce0f58e90cd0f
MD5 d4f9f8485c11af448c93e6cb09f23c5b
BLAKE2b-256 1e10cc82f18063ed440d53a9f202bac05331362933b36f797c3262ba97555013

See more details on using hashes here.

Provenance

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