← ClaudeAtlas

plpgsql-internalslisted

PostgreSQL's PL/pgSQL procedural-language implementation — `src/pl/plpgsql/src/` — the parser (`pl_gram.y` + `pl_scanner.c`), compiler (`pl_comp.c` — turns source text into `PLpgSQL_function` struct), executor (`pl_exec.c` — the interpreter with 268 KB of statement handlers), function/DO/procedure dispatch (`pl_handler.c`), and the trusted-language sandbox boundary. Loads when the user asks about PL/pgSQL semantics not obvious from SQL (nested exceptions, RAISE / GET STACKED DIAGNOSTICS, cursor lifecycle, RECORD variables, EXECUTE dynamic SQL, GET DIAGNOSTICS, transaction control from within a procedure, plan caching for expressions, or the trusted-vs-untrusted distinction), when investigating "why is my PL/pgSQL slower than raw SQL" (typically plan-cache or exception-block reasons), when adding a new PL/pgSQL feature (has scenario `integrate-with-plpgsql`), or when working with PL/pgSQL security (recall from `2026-06-04-a9-plpgsql` session that trusted-PL gate is enforced exactly twice in `pl_handler.c`, EXE
matejformanek/postgres-claude · ★ 0 · API & Backend · score 70
Install: claude install-skill matejformanek/postgres-claude
# plpgsql-internals — the SQL procedural language PL/pgSQL is PG's most-used procedural language: functions, procedures, DO blocks, triggers. Under the hood it's a **parser + compiler + interpreter** living in a self-contained subdirectory. The interpreter (`pl_exec.c`) is 268 KB — the biggest source file in PG core after `postmaster.c`. ## The file map | File | KB | Role | |---|---:|---| | `pl_handler.c` | 15 | The `PL_handler_*` fmgr entry points. Compiles + caches + executes. **This is where the trusted-language sandbox gate lives — enforced exactly twice.** | | `pl_gram.y` | 119 | Bison grammar — PL/pgSQL statement syntax (IF / LOOP / WHILE / FOR / DECLARE / RAISE / GET DIAGNOSTICS / etc.). | | `pl_scanner.c` | 19 | Tokenizer that feeds the grammar. Wraps `core_yylex` — shares tokens with the main SQL parser. | | `pl_comp.c` | 66 | Compiler — takes the parsed AST + turns it into `PLpgSQL_function` struct with `PLpgSQL_stmt`s + datum types + variable slots. | | `pl_exec.c` | **268** | Interpreter. `exec_stmt_block`, `exec_stmt_execsql`, `exec_stmt_return`, `exec_stmt_raise`, EXCEPTION handling. Every statement type has its own `exec_stmt_*` handler. | | `pl_funcs.c` | 39 | Utility functions — data structures, formatting, err-context callback, dumping compiled functions. | | `plpgsql.h` | 37 | Public API + PLpgSQL_* struct definitions. | | `pl_reserved_kwlist.h` + `pl_unreserved_kwlist.h` | — | Keyword tables. PL/pgSQL has its own reserved-word set separate from SQL. |