Skip to main content

Python wrapper for libpg_query

Project description

pgparse

Python bindings for libpg_query, exposing PostgreSQL's internal parser to Python. Parse, normalize, and fingerprint SQL statements using the same parser that PostgreSQL itself uses.

Version Coverage License

PostgreSQL Compatibility

pgparse libpg_query PostgreSQL
1.x 17-latest 17

Installation

pip install pgparse

Wheels are provided for Linux (manylinux and musllinux, x86_64 and aarch64) and macOS (arm64) for Python 3.11+ via the stable ABI (abi3). Installing from source requires gcc, make, and the libpg_query build dependencies.

Usage

Parse

Returns the internal PostgreSQL parse tree as a list of statement dicts:

import pgparse

result = pgparse.parse("SELECT * FROM orders WHERE id = 1")
# [{'stmt': {'SelectStmt': {...}}}]

Normalize

Replaces literal values with positional placeholders — useful for query grouping and log analysis:

pgparse.normalize("SELECT * FROM orders WHERE id = 1")
# "SELECT * FROM orders WHERE id = $1"

pgparse.normalize("SELECT * FROM orders WHERE id = 2")
# "SELECT * FROM orders WHERE id = $1"

Fingerprint

Produces a stable hash that is identical for structurally equivalent queries regardless of literal values or formatting:

pgparse.fingerprint("SELECT * FROM orders WHERE id = 1")
# "0357e3db3ead2de761ea5c0f064bfddc0048cad5eb"

pgparse.fingerprint("SELECT * FROM orders WHERE id = 99")
# "0357e3db3ead2de761ea5c0f064bfddc0048cad5eb"  # same fingerprint

Parse PL/pgSQL

Parse a PL/pgSQL function body:

func = """
CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$
    BEGIN
        RETURN subtotal * 0.06;
    END;
$$ LANGUAGE plpgsql;
"""
result = pgparse.parse_pgsql(func)

Error Handling

Invalid SQL raises pgparse.PGQueryError with the error message and cursor position:

try:
    pgparse.parse("SELECT FROM WHERE")
except pgparse.PGQueryError as e:
    print(e.message)   # syntax error at or near "WHERE"
    print(e.position)  # 13

Documentation

Full API reference and examples: gmr.github.io/pgparse

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

pgparse-1.0.3.tar.gz (3.0 MB view details)

Uploaded Source

Built Distributions

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

pgparse-1.0.3-cp311-abi3-musllinux_1_2_x86_64.whl (3.0 MB view details)

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

pgparse-1.0.3-cp311-abi3-musllinux_1_2_aarch64.whl (3.0 MB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ ARM64

pgparse-1.0.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pgparse-1.0.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (3.0 MB view details)

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

pgparse-1.0.3-cp311-abi3-macosx_11_0_arm64.whl (690.9 kB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

Details for the file pgparse-1.0.3.tar.gz.

File metadata

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

File hashes

Hashes for pgparse-1.0.3.tar.gz
Algorithm Hash digest
SHA256 4f5e15f14d459d90cf38a4e594079db206f8e8c2dab8153f3700e9333d068af0
MD5 31d7b0c4a02550e47c7175d8dc567b43
BLAKE2b-256 e1bfc90517b33ff69b1e0f534ae0c143598d582ad3a53c4d501a54e958ebaf88

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.3.tar.gz:

Publisher: deploy.yaml on gmr/pgparse

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

File details

Details for the file pgparse-1.0.3-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.3-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 de8a3584bbc82abcb7b833ed6aa6575bc746cede82b6c8f8294dafb115e8c566
MD5 69ee139612b72b2ff4b793728f0d5764
BLAKE2b-256 cd6b7a5d9209cbf519655a662ec40b03d1ebcded5cb2637fe08a9b4cfa3343bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.3-cp311-abi3-musllinux_1_2_x86_64.whl:

Publisher: deploy.yaml on gmr/pgparse

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

File details

Details for the file pgparse-1.0.3-cp311-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.3-cp311-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 b6f009abca3dc9c5a5a0f0fdf1e00c81bd5a2f6c6f51e0c80d944bb93fbef4ba
MD5 5c703c82ca90905ef53c5770263cbbb0
BLAKE2b-256 b8de48db5269759c7c441bf4fb550b6a9d965e76940b9633aa7e3b2b87d42f52

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.3-cp311-abi3-musllinux_1_2_aarch64.whl:

Publisher: deploy.yaml on gmr/pgparse

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

File details

Details for the file pgparse-1.0.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f5d24c7a6192546888b75a879e9b0aad32f6e723fd50c1d5b75811e8f87e75f3
MD5 6f447380d60b38f84e5c4f6a1b80f186
BLAKE2b-256 626d48127deabf2c3af38959a03f6e17363db173c870af10c00f57d84fe01a58

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: deploy.yaml on gmr/pgparse

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

File details

Details for the file pgparse-1.0.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d039a78754bbba535713365744326ff18529ef6742db1d059d451bd6f452265d
MD5 f343098523d0649d5277ca2df2d55d83
BLAKE2b-256 dee2efaba57614172682398f26ec6731e0a6d3ae836dac070f900b053e6592b8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: deploy.yaml on gmr/pgparse

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

File details

Details for the file pgparse-1.0.3-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.3-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1227fccd54cf2d09e889e747fef408a3dff91799dd572a930bea101236e5fa3c
MD5 078b532acbc3d7f5a708127fd1406f17
BLAKE2b-256 b72ebf2f163ff5d2a0380483796974719200df1d9e9da6da03a84ecc0842038b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.3-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: deploy.yaml on gmr/pgparse

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