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

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.2.2-cp39-abi3-win_amd64.whl (779.1 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.2.2-cp39-abi3-musllinux_1_2_x86_64.whl (880.4 kB view details)

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

tree_sitter_postgres-1.2.2-cp39-abi3-musllinux_1_2_aarch64.whl (888.6 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.2.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (888.3 kB view details)

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

tree_sitter_postgres-1.2.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (882.0 kB view details)

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

tree_sitter_postgres-1.2.2-cp39-abi3-macosx_11_0_arm64.whl (898.0 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.2.2-cp39-abi3-macosx_10_9_x86_64.whl (784.8 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_postgres-1.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 b195b4aacac1df1cda9e4658c913ead5a315ca40607470c5595b0431ad4ddd86
MD5 41e06544260115c05a3bb9079c1c1d54
BLAKE2b-256 342fb36acb24d9472f8baf2761121a8755fb3d404b6e2281421a55963e44923e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.2-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 224c3ba7abad9aee359821c3069837b07b46254d53a9847e45e90e8889726da9
MD5 044dd26c7f9ee79ade5299c624378064
BLAKE2b-256 984379ee68e1020c7155dc56c17d6b6b3693d87e9a324cb46551435214209c16

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 202c8012d252ec1543a26bb519b3aec4824c0688081d9e90dd10e65ee952acef
MD5 973e027f69b71485decab7c7a4e1069f
BLAKE2b-256 008ce9b45699fc38b3dd97ecd4fe47d22a589e0eed48e652f5d4a89d064f3f10

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.2-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f4b2b0b46422d1cc05f812b87dd78d78ff8668ad4900c9361697589a2e6d5ce5
MD5 eed5981f669ade07197c4390e3f364e1
BLAKE2b-256 909ce839e463e4aa9b27346d5cbeb724b86a6551f00daf1334db0f9628ad8cb9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.2-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 1ba72143d5438379c346535d4ce6d76d83bb8b5e4029dfc3993eb6113950f324
MD5 f7c205e98e7a7545b09d546f02399f8b
BLAKE2b-256 d4eba3f54a39092f771658077e98b132e87e856783ffafcffe09427889bf5854

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b7e8f20905ceea3159146579ebe5098e4c4e878fda24d0022674c04fc6476120
MD5 42c2b5028af9bdd97ee28df650806cc5
BLAKE2b-256 d8fcdb71715009e4001dac2236b4e3e0ea5f72bfc86c706517e1709b8c69ed59

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.2-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.2-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.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 d4027b002ce838e4731baf1612a43714d326c808c3154f56cf42c5a1916b286b
MD5 364da60cd720be909097fa54c6957a23
BLAKE2b-256 d0be3ff09e5e7934c1b43f404084fd675504627c40908afd506ff5e9b140b1d6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d31a2c686db775d7091870a31907c11834f4b359d22abe49574b4e4d4feb4d6e
MD5 db60d158e94a94466edbf3b8c63d2b44
BLAKE2b-256 0e4962f705dbea431f02549cb517fb2ae38ff9454d6506f68f6e209b1cc239bf

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.2-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1221faf87c14b6f481fad4a63459464b0a048ec6a647edd5bba0e32fbd7000c0
MD5 d90ad4b09bbc4c26430c39d4df02b310
BLAKE2b-256 2e369ecd33d152c92d1b085582fc2320c9f806f0d5a8f42b9dab12526ec68e90

See more details on using hashes here.

Provenance

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