{"id":13439727,"url":"https://github.com/pganalyze/libpg_query","last_synced_at":"2026-01-27T07:12:49.251Z","repository":{"id":36094090,"uuid":"40395126","full_name":"pganalyze/libpg_query","owner":"pganalyze","description":"C library for accessing the PostgreSQL parser outside of the server environment","archived":false,"fork":false,"pushed_at":"2026-01-21T01:22:44.000Z","size":20921,"stargazers_count":1408,"open_issues_count":49,"forks_count":209,"subscribers_count":31,"default_branch":"17-latest","last_synced_at":"2026-01-21T12:56:04.571Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pganalyze.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2015-08-08T07:24:16.000Z","updated_at":"2026-01-21T01:21:34.000Z","dependencies_parsed_at":"2023-12-21T00:54:42.850Z","dependency_job_id":"9d6e1523-3a76-4ccb-bc3b-877fbae98ef0","html_url":"https://github.com/pganalyze/libpg_query","commit_stats":{"total_commits":451,"total_committers":38,"mean_commits":"11.868421052631579","dds":0.4101995565410199,"last_synced_commit":"c3ed78bd3c5ceef446e8a769991e89421af7b6b3"},"previous_names":["lfittl/libpg_query"],"tags_count":48,"template":false,"template_full_name":null,"purl":"pkg:github/pganalyze/libpg_query","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pganalyze%2Flibpg_query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pganalyze%2Flibpg_query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pganalyze%2Flibpg_query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pganalyze%2Flibpg_query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pganalyze","download_url":"https://codeload.github.com/pganalyze/libpg_query/tar.gz/refs/heads/17-latest","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pganalyze%2Flibpg_query/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28807615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T06:25:51.065Z","status":"ssl_error","status_checked_at":"2026-01-27T06:25:50.640Z","response_time":168,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:01:16.577Z","updated_at":"2026-01-27T07:12:49.235Z","avatar_url":"https://github.com/pganalyze.png","language":"C","readme":"# libpg_query\n\nC library for accessing the PostgreSQL parser outside of the server.\n\nThis library uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.\n\nNote that this is mostly intended as a base library for [pg_query](https://github.com/pganalyze/pg_query) (Ruby), [pg_query.go](https://github.com/pganalyze/pg_query_go) (Go), [pg_query.rs](https://github.com/pganalyze/pg_query.rs) (Rust), [pgsql-parser](https://github.com/launchql/pgsql-parser) (Node), [psqlparse](https://github.com/alculquicondor/psqlparse) (Python) and [pglast](https://pypi.org/project/pglast/) (Python 3).\n\nYou can find further background to why a query's parse tree is useful here: https://pganalyze.com/blog/parse-postgresql-queries-in-ruby.html\n\n\n## Installation\n\n```sh\ngit clone -b 17-latest git://github.com/pganalyze/libpg_query\ncd libpg_query\nmake\n```\n\nDue to compiling parts of PostgreSQL, running `make` will take a bit. Expect up to 3 minutes.\n\nFor a production build, its best to use a specific git tag (see CHANGELOG).\n\nWhen compiling on Windows with Visual Studio, instead use `nmake` with the `Makefile.msvc`:\n\n```sh\nnmake /F Makefile.msvc\n```\n\n\n## Usage: Parsing a query\n\nA [full example](https://github.com/pganalyze/libpg_query/blob/master/examples/simple.c) that parses a query looks like this:\n\n```c\n#include \u003cpg_query.h\u003e\n#include \u003cstdio.h\u003e\n\nint main() {\n  PgQueryParseResult result;\n\n  result = pg_query_parse(\"SELECT 1\");\n\n  printf(\"%s\\n\", result.parse_tree);\n\n  pg_query_free_parse_result(result);\n\n  return 0;\n}\n```\n\nCompile it like this:\n\n```\ncc -Ilibpg_query -Llibpg_query example.c -lpg_query\n```\n\nThis will output the parse tree (whitespace adjusted here for better readability):\n\n```json\n{\n    \"version\": 170007,\n    \"stmts\": [\n        {\n            \"stmt\": {\n                \"SelectStmt\": {\n                    \"targetList\": [\n                        {\n                            \"ResTarget\": {\n                                \"val\": {\n                                    \"A_Const\": {\n                                        \"val\": {\n                                            \"Integer\": {\n                                                \"ival\": 1\n                                            }\n                                        },\n                                        \"location\": 7\n                                    }\n                                },\n                                \"location\": 7\n                            }\n                        }\n                    ],\n                    \"limitOption\": \"LIMIT_OPTION_DEFAULT\",\n                    \"op\": \"SETOP_NONE\"\n                }\n            }\n        }\n    ]\n}\n```\n\n## Usage: Scanning a query into its tokens using the PostgreSQL scanner/lexer\n\npg_query also exposes the underlying scanner of Postgres, which is also used in\nthe very first part in the parsing process. It can be useful on its own for e.g.\nsyntax highlighting, where one is mostly concerned with differentiating keywords\nfrom identifiers and other parts of the query:\n\n```c\n#include \u003cstdio.h\u003e\n\n#include \u003cpg_query.h\u003e\n#include \"protobuf/pg_query.pb-c.h\"\n\nint main() {\n  PgQueryScanResult result;\n  PgQuery__ScanResult *scan_result;\n  PgQuery__ScanToken *scan_token;\n  const ProtobufCEnumValue *token_kind;\n  const ProtobufCEnumValue *keyword_kind;\n  const char *input = \"SELECT update AS left /* comment */ FROM between\";\n\n  result = pg_query_scan(input);\n  scan_result = pg_query__scan_result__unpack(NULL, result.pbuf.len, (const uint8_t *) result.pbuf.data);\n\n  printf(\"  version: %d, tokens: %ld, size: %zu\\n\", scan_result-\u003eversion, scan_result-\u003en_tokens, result.pbuf.len);\n  for (size_t j = 0; j \u003c scan_result-\u003en_tokens; j++) {\n    scan_token = scan_result-\u003etokens[j];\n    token_kind = protobuf_c_enum_descriptor_get_value(\u0026pg_query__token__descriptor, scan_token-\u003etoken);\n    keyword_kind = protobuf_c_enum_descriptor_get_value(\u0026pg_query__keyword_kind__descriptor, scan_token-\u003ekeyword_kind);\n    printf(\"  \\\"%.*s\\\" = [ %d, %d, %s, %s ]\\n\", scan_token-\u003eend - scan_token-\u003estart, \u0026(input[scan_token-\u003estart]), scan_token-\u003estart, scan_token-\u003eend, token_kind-\u003ename, keyword_kind-\u003ename);\n  }\n\n  pg_query__scan_result__free_unpacked(scan_result, NULL);\n  pg_query_free_scan_result(result);\n\n  return 0;\n}\n```\n\nThis will output the following:\n\n```\n  version: 170007, tokens: 7, size: 77\n  \"SELECT\" = [ 0, 6, SELECT, RESERVED_KEYWORD ]\n  \"update\" = [ 7, 13, UPDATE, UNRESERVED_KEYWORD ]\n  \"AS\" = [ 14, 16, AS, RESERVED_KEYWORD ]\n  \"left\" = [ 17, 21, LEFT, TYPE_FUNC_NAME_KEYWORD ]\n  \"/* comment */\" = [ 22, 35, C_COMMENT, NO_KEYWORD ]\n  \"FROM\" = [ 36, 40, FROM, RESERVED_KEYWORD ]\n  \"between\" = [ 41, 48, BETWEEN, COL_NAME_KEYWORD ]\n```\n\nWhere the each element in the token list has the following fields:\n\n1. Start location in the source string\n2. End location in the source string\n3. Token value - see Token type in `protobuf/pg_query.proto`\n4. Keyword type - see KeywordKind type in `protobuf/pg_query.proto`, possible values:\n  `NO_KEYWORD`: Not a keyword\n  `UNRESERVED_KEYWORD`: Unreserved keyword (available for use as any kind of unescaped name)\n  `COL_NAME_KEYWORD`: Unreserved keyword (can be unescaped column/table/etc names, cannot be unescaped function or type name)\n  `TYPE_FUNC_NAME_KEYWORD`: Reserved keyword (can be unescaped function or type name, cannot be unescaped column/table/etc names)\n  `RESERVED_KEYWORD`: Reserved keyword (cannot be unescaped column/table/variable/type/function names)\n\nNote that whitespace does not show as tokens.\n\n## Usage: Fingerprinting a query\n\nFingerprinting allows you to identify similar queries that are different only because\nof the specific object that is being queried for (i.e. different object ids in the WHERE clause),\nor because of formatting.\n\nExample:\n\n```c\n#include \u003cpg_query.h\u003e\n#include \u003cstdio.h\u003e\n\nint main() {\n  PgQueryFingerprintResult result;\n\n  result = pg_query_fingerprint(\"SELECT 1\");\n\n  printf(\"%s\\n\", result.fingerprint_str);\n\n  pg_query_free_fingerprint_result(result);\n\n  return 0;\n}\n```\n\nThis will output:\n\n```\n50fde20626009aba\n```\n\nSee https://github.com/pganalyze/libpg_query/wiki/Fingerprinting for the full fingerprinting rules.\n\n## Usage: Parsing a PL/pgSQL function\n\nA [full example](https://github.com/pganalyze/libpg_query/blob/master/examples/simple_plpgsql.c) that parses a [PL/pgSQL](https://www.postgresql.org/docs/current/static/plpgsql.html) method looks like this:\n\n```c\n#include \u003cpg_query.h\u003e\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n\nint main() {\n  PgQueryPlpgsqlParseResult result;\n\n  result = pg_query_parse_plpgsql(\" \\\n  CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name varchar, \\\n                                                  v_version varchar) \\\nRETURNS varchar AS $$ \\\nBEGIN \\\n    IF v_version IS NULL THEN \\\n        RETURN v_name; \\\n    END IF; \\\n    RETURN v_name || '/' || v_version; \\\nEND; \\\n$$ LANGUAGE plpgsql;\");\n\n  if (result.error) {\n    printf(\"error: %s at %d\\n\", result.error-\u003emessage, result.error-\u003ecursorpos);\n  } else {\n    printf(\"%s\\n\", result.plpgsql_funcs);\n  }\n\n  pg_query_free_plpgsql_parse_result(result);\n\n  return 0;\n}\n```\n\nThis will output:\n\n```json\n[\n{\"PLpgSQL_function\":{\"datums\":[{\"PLpgSQL_var\":{\"refname\":\"found\",\"datatype\":{\"PLpgSQL_type\":{\"typname\":\"UNKNOWN\"}}}}],\"action\":{\"PLpgSQL_stmt_block\":{\"lineno\":1,\"body\":[{\"PLpgSQL_stmt_if\":{\"lineno\":1,\"cond\":{\"PLpgSQL_expr\":{\"query\":\"SELECT v_version IS NULL\"}},\"then_body\":[{\"PLpgSQL_stmt_return\":{\"lineno\":1,\"expr\":{\"PLpgSQL_expr\":{\"query\":\"SELECT v_name\"}}}}]}},{\"PLpgSQL_stmt_return\":{\"lineno\":1,\"expr\":{\"PLpgSQL_expr\":{\"query\":\"SELECT v_name || '/' || v_version\"}}}}]}}}}\n]\n```\n\n## Versions\n\nFor stability, it is recommended you use individual tagged git versions, see CHANGELOG.\n\nEach major version is maintained in a dedicated git branch. Only the latest Postgres stable release receives active updates.\n\n| PostgreSQL Major Version | Branch     | Status              |\n|--------------------------|------------|---------------------|\n| 17                       | 17-latest  | Active development  |\n| 16                       | 16-latest  | Critical fixes only |\n| 15                       | 15-latest  | Critical fixes only |\n| 14                       | 14-latest  | Critical fixes only |\n| 13                       | 13-latest  | Critical fixes only |\n| 12                       | (n/a)      | Not supported       |\n| 11                       | (n/a)      | Not supported       |\n| 10                       | 10-latest  | No longer supported |\n| 9.6                      | (n/a)      | Not supported       |\n| 9.5                      | 9.5-latest | No longer supported |\n| 9.4                      | 9.4-latest | No longer supported |\n\n## Resources\n\npg_query wrappers in other languages:\n\n* Ruby: [pg_query](https://github.com/pganalyze/pg_query)\n* Go: [pg_query_go](https://github.com/pganalyze/pg_query_go)\n* Javascript (Node): [pgsql-parser](https://github.com/launchql/pgsql-parser)\n* Javascript (Browser): [pg-query-emscripten](https://github.com/pganalyze/pg-query-emscripten)\n* Python: [psqlparse](https://github.com/alculquicondor/psqlparse), [pglast](https://github.com/lelit/pglast),\n[psqlparse2](https://github.com/dani-maarouf/psqlparse2)\n* OCaml: [pg_query-ocaml](https://github.com/roddyyaga/pg_query-ocaml)\n* Rust: [pg_query.rs](https://github.com/pganalyze/pg_query.rs)\n\nProducts, tools and libraries built on pg_query:\n\n* [pganalyze](https://pganalyze.com/)\n* [hsql](https://github.com/JackDanger/hsql)\n* [sqlint](https://github.com/purcell/sqlint)\n* [pghero](https://github.com/ankane/pghero)\n* [dexter](https://github.com/ankane/dexter)\n* [pgscope](https://github.com/gjalves/pgscope)\n* [pg_materialize](https://github.com/aanari/pg-materialize)\n* [DuckDB](https://github.com/cwida/duckdb) ([details](https://github.com/cwida/duckdb/tree/master/third_party/libpg_query))\n* [pgspot](https://github.com/timescale/pgspot)\n* [Postgres Language Server](https://github.com/supabase/postgres_lsp)\n* and more\n\nPlease feel free to [open a PR](https://github.com/pganalyze/libpg_query/pull/new/master) to add yours! :)\n\n\n## Authors\n\n- [Lukas Fittl](mailto:lukas@fittl.com)\n\n\n## License\n\nPostgreSQL server source code, used under the [PostgreSQL license](https://www.postgresql.org/about/licence/).\u003cbr\u003e\nPortions Copyright (c) 1996-2023, The PostgreSQL Global Development Group\u003cbr\u003e\nPortions Copyright (c) 1994, The Regents of the University of California\n\nAll other parts are licensed under the 3-clause BSD license, see LICENSE file for details.\u003cbr\u003e\nCopyright (c) 2015, Lukas Fittl \u003clukas@fittl.com\u003e\nCopyright (c) 2016-2025, Duboce Labs, Inc. (pganalyze) \u003cteam@pganalyze.com\u003e\n","funding_links":[],"categories":["Database","HarmonyOS","C","SQL"],"sub_categories":["Windows Manager","Parsers"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpganalyze%2Flibpg_query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpganalyze%2Flibpg_query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpganalyze%2Flibpg_query/lists"}