{"id":43630,"library":"plpgsql-deparser","title":"PL/pgSQL Deparser","description":"Converts parsed PL/pgSQL function ASTs back into SQL source code. Part of the pgsql-parser ecosystem. v0.7.7 (stable, weekly releases). Key differentiator: round-tripping from AST to SQL with high fidelity, supporting PostgreSQL's PL/pgSQL dialect including complex control structures. Depends on libpg_query for parsing; you must parse first with pgsql-parser.","status":"active","version":"0.7.7","language":"javascript","source_language":"en","source_url":"https://github.com/constructive-io/pgsql-parser","tags":["javascript","sql","postgres","postgresql","pg","plpgsql","query","ast","deparser","typescript"],"install":[{"cmd":"npm install plpgsql-deparser","lang":"bash","label":"npm"},{"cmd":"yarn add plpgsql-deparser","lang":"bash","label":"yarn"},{"cmd":"pnpm add plpgsql-deparser","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Produces the AST that plpgsql-deparser consumes.","package":"pgsql-parser","optional":false}],"imports":[{"note":"ESM-only. CommonJS require will not work; use import or dynamic import.","wrong":"const deparse = require('plpgsql-deparser')","symbol":"deparse","correct":"import { deparse } from 'plpgsql-deparser'"},{"note":"Named export, not default.","wrong":"import deparse from 'plpgsql-deparser'","symbol":"deparse","correct":"import { deparse } from 'plpgsql-deparser'"},{"note":"TypeScript type export for the AST node types.","symbol":"PlPgSqlStatements","correct":"import { PlPgSqlStatements } from 'plpgsql-deparser'"}],"quickstart":{"code":"import { parsePlPgSql } from 'pgsql-parser';\nimport { deparse } from 'plpgsql-deparser';\n\nconst sql = `CREATE OR REPLACE FUNCTION add(a INT, b INT) RETURNS INT AS $$\nBEGIN\n  RETURN a + b;\nEND;\n$$ LANGUAGE plpgsql;`;\n\nconst parsed = parsePlPgSql(sql);\nconst deparsed = deparse(parsed);\n\nconsole.log('Deparsed SQL:');\nconsole.log(deparsed);","lang":"typescript","description":"Parses a PL/pgSQL function and then deparses it back to SQL."},"warnings":[{"fix":"Always use parsePlPgSql from pgsql-parser.","message":"Deparser expects AST from pgsql-parser's parsePlPgSql only; parsing with other libraries will fail.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Check the documentation for known limitations; test round-tripping.","message":"Not all PL/pgSQL constructs are supported; some may be omitted or produce different SQL.","severity":"gotcha","affected_versions":"<1.0.0"},{"fix":"Ensure pgsql-parser is also v0.7.x.","message":"Version 0.7.0 changed internal AST node names; deparse may fail with ASTs from older parser versions.","severity":"breaking","affected_versions":"0.7.0 - 0.7.7"}],"env_vars":null,"search_vec":"'ast':7,30,66 'back':8 'code':12 'complex':42 'control':43 'convert':3 'depars':2,67 'depend':45 'dialect':40 'differenti':25 'ecosystem':19 'fidel':35 'first':54 'function':6 'high':34 'includ':41 'javascript':59 'key':24 'libpg':47 'must':52 'pars':4,50,53 'parser':18,58 'part':13 'pg':63 'pgsql':17,57 'pgsql-parser':16,56 'pl/pgsql':1,5,39 'plpgsql':64 'postgr':61 'postgresql':37,62 'queri':48,65 'releas':23 'round':27 'round-trip':26 'sourc':11 'sql':10,32,60 'stabl':21 'structur':44 'support':36 'trip':28 'typescript':68 'v0.7.7':20 'week':22","created_at":"2026-06-05T17:00:43.135172+00:00","updated_at":"2026-06-05T17:00:43.135172+00:00","problems":[{"fix":"import { deparse } from 'plpgsql-deparser'","cause":"Default import used instead of named import.","error":"TypeError: deparse is not a function"},{"fix":"Use pgsql-parser's parsePlPgSql to parse the SQL before deparsing.","cause":"Called deparse without parsing first, or parsed with wrong function.","error":"Error: Expected AST from parsePlPgSql, got undefined"},{"fix":"npm install plpgsql-deparser","cause":"Package not installed or TypeScript cannot find types. Ensure it is installed and tsconfig includes node_modules.","error":"Cannot find module 'plpgsql-deparser' or its corresponding type declarations."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/constructive-io/pgsql-parser","github":"https://github.com/constructive-io/pgsql-parser","docs":null,"changelog":null,"pypi":null,"npm":"plpgsql-deparser","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}