{"id":3209,"library":"pglast","title":"PostgreSQL Languages AST and statements prettifier","description":"A Python 3 module that exposes the parse tree of a PostgreSQL statement (extracted by the almost standard PG parser repackaged as a standalone static library by libpg_query) as a set of interconnected nodes, usually called an abstract syntax tree. It provides functionalities for parsing SQL, generating ASTs, and prettifying SQL statements. The library is actively maintained with frequent releases, currently at version 7.13, and targets PostgreSQL 17.","status":"active","version":"7.13","language":"en","source_language":"en","source_url":"https://github.com/lelit/pglast","tags":["postgresql","sql","ast","parser","prettifier","database"],"install":[{"cmd":"pip install pglast","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"parse_sql","correct":"from pglast import parse_sql"},{"symbol":"prettify","correct":"from pglast import prettify"},{"note":"Provides the Python classes representing PostgreSQL AST nodes (e.g., ast.SelectStmt).","symbol":"ast","correct":"from pglast import ast"}],"quickstart":{"code":"from pglast import parse_sql, prettify\nfrom pglast.ast import SelectStmt, A_Const, Integer\n\nsql_statement = \"SELECT 1 + 2 AS sum_result, 'hello' WHERE a = B;\"\nast_tree = parse_sql(sql_statement)\n\n# The root of the AST is a tuple of RawStmt objects\nroot_stmt = ast_tree[0].stmt\n\n# Example of inspecting the AST\nif isinstance(root_stmt, SelectStmt):\n    print(f\"Parsed a SelectStmt.\")\n    for target in root_stmt.targetList:\n        print(f\"  Target: {target.name or ''} value type: {type(target.val)}\")\n        if isinstance(target.val, A_Const) and isinstance(target.val.val, Integer):\n            print(f\"    Integer constant: {target.val.val.val}\")\n\n# Prettify the SQL statement\npretty_sql = prettify(sql_statement)\nprint(\"\\nOriginal SQL:\")\nprint(sql_statement)\nprint(\"\\nPrettified SQL:\")\nprint(pretty_sql)","lang":"python","description":"Parses an SQL statement into an Abstract Syntax Tree (AST) and then demonstrates how to traverse the AST for basic inspection, followed by prettifying the original SQL statement."},"warnings":[{"fix":"Update code to directly interact with `pglast.ast` objects. Remove any JSON parsing logic after calling `parse_sql()`.","message":"Since v3.0, `parse_sql()` returns native Python AST objects (subclasses of `pglast.ast.Node`) directly, not a JSON string. Code expecting JSON output or requiring manual JSON parsing will break.","severity":"breaking","affected_versions":">=3.0"},{"fix":"Replace usages of `pglast.node` wrappers with direct interaction with `pglast.ast` classes.","message":"The `pglast.node` wrapper classes were removed in v3.0. All AST manipulation now operates directly on the `pglast.ast` classes.","severity":"breaking","affected_versions":">=3.0"},{"fix":"Adjust import paths and function calls to use `pglast.printers` and `pglast.stream` as appropriate.","message":"The `pglast.printer` module was removed in v3.0. Print-specific functionalities are now in the `pglast.printers` subpackage, and serialization classes are in the new `pglast.stream` module.","severity":"breaking","affected_versions":">=3.0"},{"fix":"Ensure that code handling `Float` node values expects a string and performs explicit conversion if numerical operations are required.","message":"In v5.0, the type of `pglast.ast.Float` values changed from `Decimal` to `str`. This was to resolve issues with floating-point representation and rendering.","severity":"breaking","affected_versions":">=5.0"},{"fix":"Upgrade Python environment to 3.9 or higher. Be aware of potential syntax changes or new features in PostgreSQL 17 that might affect parsing or prettification if interacting with older database versions.","message":"Version 7.0 introduced a requirement for Python >= 3.9 and explicitly targets PostgreSQL 17. Older Python versions or applications targeting older PostgreSQL versions might encounter compatibility issues.","severity":"breaking","affected_versions":">=7.0"},{"fix":"For complex PL/pgSQL, be aware that the internal body might not be fully parsed into a detailed AST. You might need to extract the function body as a string and process it with external tools or custom logic.","message":"The `parse_plpgsql()` function has known limitations and may not fully parse complex PL/pgSQL bodies, often treating the body as a single string literal within the AST. The maintainer has noted that its support is 'still vaporware'.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}