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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

pgparse-1.0.1-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.1-cp311-abi3-musllinux_1_2_aarch64.whl (3.0 MB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ ARM64

pgparse-1.0.1-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.1-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.1-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.1-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.1-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 19ab916074b395149aaf1c9bb1723fdaa80d9a0a80f601312739c35105c4aaa1
MD5 871f7ff861ccb35a9f7e0898d302cc17
BLAKE2b-256 6b9698f7e9b95e05c77eacd6cb41b7e467db16f34e536f23fa2cb9eb9e725ca8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.1-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.1-cp311-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.1-cp311-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 0795cac3163450f8692b6938ea76e2b0ac9d224558e6e160aae8456b78f912a7
MD5 509e3f6b2946445773424a853242f1d3
BLAKE2b-256 f78f8d4167ff31e218226a8a27b737d115ed7637cf1eebbc0cbb5a5c4cfdc90d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.1-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.1-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.1-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 370e3e2fdd681f5a5576fa9ccb779cedaad4fec6c39bc964e15a4a07de018fa9
MD5 be4338db314c628ebe7dd2def929e4a0
BLAKE2b-256 4f8a3897778bbb70a93b8e0739380d5d73443d2394d4cf226e78170ad7cd1938

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.1-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.1-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.1-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 401822d2ee08d817ed23c73418ca53611f80575b40a2cd13a7330015715efae0
MD5 ed2b5d5ae57c4402cc06306b30e7bab8
BLAKE2b-256 cacca0b1bbd17965ecb75ce22a783143fe4a7580a48ba42d33efd6166e3c621c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.1-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.1-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pgparse-1.0.1-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ab432f8eedc3ed266ce393bfee6e57a732d2ce187ac53ac8c3f6b82a3f7a7f1b
MD5 1574aa042163af21f5f645b54497734a
BLAKE2b-256 5cf0833f54a98f82f68fbbcaf4d9df141c6b094bc979ba81e9a326f4112bc0d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgparse-1.0.1-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