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.2.3.tar.gz (6.0 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.2.3-cp39-abi3-win_arm64.whl (796.9 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.2.3-cp39-abi3-win_amd64.whl (779.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.2.3-cp39-abi3-musllinux_1_2_x86_64.whl (881.0 kB view details)

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

tree_sitter_postgres-1.2.3-cp39-abi3-musllinux_1_2_aarch64.whl (889.3 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.2.3-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (889.0 kB view details)

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

tree_sitter_postgres-1.2.3-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (882.5 kB view details)

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

tree_sitter_postgres-1.2.3-cp39-abi3-macosx_11_0_arm64.whl (898.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.2.3-cp39-abi3-macosx_10_9_x86_64.whl (785.0 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tree_sitter_postgres-1.2.3.tar.gz
Algorithm Hash digest
SHA256 0de5718218e08ec12ddd5112ff2df514aabfcb6ca7dbf132d14433f7b9b97f58
MD5 61cda7e0d55a4d53bab2e9b89f54f255
BLAKE2b-256 3ad44fab969eeff613c133ced7e01c394d8d01c624b5d3cb7cad4eb0b6ba8943

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.3-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 6479f95f39967150e367b91b25375d2ab91349d8ed235b1f2ad9567d303f91f8
MD5 a00f6410403add0101b9119fe8e57180
BLAKE2b-256 3cce07346d2fd9f15834c75302362af7a9816248ce24b4f730a25ffa9f2d7dba

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ac47080f7b1c92a0a3f3a34f531bed2c87f4b937f90c30caa9069730189878ac
MD5 15344b0eb72fcdfe064f66134e5e715c
BLAKE2b-256 84245306fd3eef35cb08ddd95adee2170e8b11b5549591e279ea2cf9b21ac0f6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.3-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7273228ae9be865d5221536e24bc8b79d841980652a1a9a00f1f5d2c14f8072b
MD5 3fe3316a50c258113cdc7c3b500bb32d
BLAKE2b-256 a941cb08706c20efe84da3ddd2fe14ca1ffe9d80fa182919e330aae546945b3e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.3-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 7bb9ba31b42524573930ce7bfeb9ffa8e886028b8ee686d8eb138a10d9deb2b0
MD5 d59048c07df3faed9c0d2921faa09a66
BLAKE2b-256 bcfc74575504e6f8ad541506c0d9fc43898ba253d032ede1c3e3c24c61745997

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.3-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5ed1657cacbe77b1b324940a64e9f2465f55ff2efb0ea3423ff9d8306449e986
MD5 12c5be650afa49e6cd1fdf959b800ebd
BLAKE2b-256 9ab5b5a6cec78c9bd29b588dfbdd1eff4376bc49d2e35c2423f00454cc7e2096

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.3-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.2.3-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.2.3-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 530abea2433b2e2b33532afabce4f97174bffc0bc0fab3bccdb794e084b26f89
MD5 82f55d5bc015f2518cd7efe9fef4b3b0
BLAKE2b-256 734a0c6fa6111a2fa53fbe2e4a194b97992ca6ca0fcc142fd322e90218323a2b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 43ee1a1d489a07458c456fc34a462a0e698886a3e6d80f60b92ea621af21f9d6
MD5 6b0aee32f9b8fdf34fb3569d340dfd67
BLAKE2b-256 abe3e902e21fc65089d3342483f9fd85e1cac3f4be52a92b6b735c7d4b1ed1f8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.3-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9da8d06c1e39961b605a094f8ea0fdf039317974c1a27e42a82076491b57b734
MD5 542a6512068319f6d7b21b68e04d3036
BLAKE2b-256 79905653cd8d692606b938d2bd1840b9acfa93f8bb842412116652cd204f7289

See more details on using hashes here.

Provenance

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