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.
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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgparse-1.0.1-cp311-abi3-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: pgparse-1.0.1-cp311-abi3-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.11+, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19ab916074b395149aaf1c9bb1723fdaa80d9a0a80f601312739c35105c4aaa1
|
|
| MD5 |
871f7ff861ccb35a9f7e0898d302cc17
|
|
| BLAKE2b-256 |
6b9698f7e9b95e05c77eacd6cb41b7e467db16f34e536f23fa2cb9eb9e725ca8
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgparse-1.0.1-cp311-abi3-musllinux_1_2_x86_64.whl -
Subject digest:
19ab916074b395149aaf1c9bb1723fdaa80d9a0a80f601312739c35105c4aaa1 - Sigstore transparency entry: 1126811063
- Sigstore integration time:
-
Permalink:
gmr/pgparse@141de9fad54a854d5f973ed2a87735b8ab294035 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/gmr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@141de9fad54a854d5f973ed2a87735b8ab294035 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pgparse-1.0.1-cp311-abi3-musllinux_1_2_aarch64.whl.
File metadata
- Download URL: pgparse-1.0.1-cp311-abi3-musllinux_1_2_aarch64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.11+, musllinux: musl 1.2+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0795cac3163450f8692b6938ea76e2b0ac9d224558e6e160aae8456b78f912a7
|
|
| MD5 |
509e3f6b2946445773424a853242f1d3
|
|
| BLAKE2b-256 |
f78f8d4167ff31e218226a8a27b737d115ed7637cf1eebbc0cbb5a5c4cfdc90d
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgparse-1.0.1-cp311-abi3-musllinux_1_2_aarch64.whl -
Subject digest:
0795cac3163450f8692b6938ea76e2b0ac9d224558e6e160aae8456b78f912a7 - Sigstore transparency entry: 1126810971
- Sigstore integration time:
-
Permalink:
gmr/pgparse@141de9fad54a854d5f973ed2a87735b8ab294035 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/gmr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@141de9fad54a854d5f973ed2a87735b8ab294035 -
Trigger Event:
push
-
Statement type:
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
- Download URL: pgparse-1.0.1-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.11+, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
370e3e2fdd681f5a5576fa9ccb779cedaad4fec6c39bc964e15a4a07de018fa9
|
|
| MD5 |
be4338db314c628ebe7dd2def929e4a0
|
|
| BLAKE2b-256 |
4f8a3897778bbb70a93b8e0739380d5d73443d2394d4cf226e78170ad7cd1938
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgparse-1.0.1-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
370e3e2fdd681f5a5576fa9ccb779cedaad4fec6c39bc964e15a4a07de018fa9 - Sigstore transparency entry: 1126811039
- Sigstore integration time:
-
Permalink:
gmr/pgparse@141de9fad54a854d5f973ed2a87735b8ab294035 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/gmr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@141de9fad54a854d5f973ed2a87735b8ab294035 -
Trigger Event:
push
-
Statement type:
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
- Download URL: pgparse-1.0.1-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.11+, manylinux: glibc 2.17+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
401822d2ee08d817ed23c73418ca53611f80575b40a2cd13a7330015715efae0
|
|
| MD5 |
ed2b5d5ae57c4402cc06306b30e7bab8
|
|
| BLAKE2b-256 |
cacca0b1bbd17965ecb75ce22a783143fe4a7580a48ba42d33efd6166e3c621c
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgparse-1.0.1-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl -
Subject digest:
401822d2ee08d817ed23c73418ca53611f80575b40a2cd13a7330015715efae0 - Sigstore transparency entry: 1126811006
- Sigstore integration time:
-
Permalink:
gmr/pgparse@141de9fad54a854d5f973ed2a87735b8ab294035 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/gmr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@141de9fad54a854d5f973ed2a87735b8ab294035 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pgparse-1.0.1-cp311-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: pgparse-1.0.1-cp311-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 690.9 kB
- Tags: CPython 3.11+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab432f8eedc3ed266ce393bfee6e57a732d2ce187ac53ac8c3f6b82a3f7a7f1b
|
|
| MD5 |
1574aa042163af21f5f645b54497734a
|
|
| BLAKE2b-256 |
5cf0833f54a98f82f68fbbcaf4d9df141c6b094bc979ba81e9a326f4112bc0d1
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgparse-1.0.1-cp311-abi3-macosx_11_0_arm64.whl -
Subject digest:
ab432f8eedc3ed266ce393bfee6e57a732d2ce187ac53ac8c3f6b82a3f7a7f1b - Sigstore transparency entry: 1126810917
- Sigstore integration time:
-
Permalink:
gmr/pgparse@141de9fad54a854d5f973ed2a87735b8ab294035 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/gmr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@141de9fad54a854d5f973ed2a87735b8ab294035 -
Trigger Event:
push
-
Statement type: