Postgres Language Server: implementing the Parser
A Postgres language server's parser builds on libpg_query rather than a custom grammar, and has to work around two gaps in it. To tolerate syntax errors, a resilient LL source parser splits input into statements by their distinct leading keywords and parses each one independently. Because libpg_query exposes only an AST with incomplete or misleading location data, the CST is reverse-engineered by matching scanned tokens (plus regex-extracted whitespace) to node properties, using a breadth-first search over the tree. Rust procedural macros generate the location, property and tree-walking code from libpg_query's protobuf definitions, which removes most of the manual match-statement boilerplate.
Related posts
- Postgres Language Server: Initial Release
Supabase mediumAdeptPostgres Language Server is an LSP implementation for Postgres offering autocompletion, syntax error highlighting, type-checking via PREPARE/EXPLAIN against a live database, and a Squawk-inspired linter, available for VSCode, Neovim and as a CLI. Its architecture treats each SQL statement as an independent compilation unit with the database schema as the source of truth. A custom Pratt-parser statement splitter falls back to semicolons and double newlines. libpg_query handles precise parsing of valid SQL, while tree-sitter covers incomplete input for autocompletion. On edits, only changed statements are invalidated and later statements just have their ranges shifted, so cached ASTs survive. Completions are scored from a lazily loaded in-memory schema cache, so no database query is needed per keystroke.
- How pg_graphql works
Supabase mediumAdeptpg_graphql is a Rust-based Postgres extension that reflects the SQL schema as a GraphQL schema and serves queries through a `graphql.resolve` SQL function, with no separate server. A request is tokenized and parsed into an AST with the graphql-parser crate, then validated against the GraphQL spec using a reflected schema built from system catalogs. That schema is memoized and reloaded only when the search path, role or schema version changes. Validated queries become builder objects that emit SQL, with quote_ident/quote_literal guarding against injection, and the SQL returns jsonb that is passed back to the client as JSON.
- Bringing psql’s \d to your web browser
Neon mediumAdept
Neon's web SQL Editor now supports psql backslash introspection commands like \d, \l, \dx and \sf, which live in psql's C client code rather than the Postgres server. Instead of compiling psql to WASM with emscripten, the C was translated to JavaScript via RegExp search-and-replace, reimplementations of helpers like sprintf, and a test script comparing output against real psql until every command matched; most fixes involved pointer arithmetic. Because backslash commands must be handled separately, SQL is now split client-side with a small open-source lexer using sticky RegExps that understands quoted identifiers, nested comments, escape and dollar-quoted strings, so only significant semicolons terminate statements.
- What To Know About Postgres if You’re Coming From SQL Server
Neon mediumAdept
Postgres and SQL Server share SQL but differ in dialect details that trip up migrating developers. Identifiers are quoted with double quotes instead of square brackets. GETDATE/DATEADD/DATEDIFF become CURRENT_TIMESTAMP, interval arithmetic and date subtraction, and TOP becomes LIMIT/OFFSET. Strings concatenate with || rather than +, CROSS APPLY becomes CROSS JOIN LATERAL, and string comparison is case-sensitive by default. Beyond syntax, Postgres is community-driven, so features like UUID generation or trigram search arrive as extensions, and tooling and hosting choices are open rather than vendor-defined.
- We're building Postgres in Rust. Using the LLVM of databases
Turso easyNoviceTurso, a Rust rewrite of SQLite, is adding a Postgres frontend by parsing Postgres SQL into a common AST and compiling it to Turso's VDBE bytecode, turning the engine into an "LLVM of databases" with pluggable frontends. The argument is that SQL databases differ mostly in on-disk layout, not in core operations, so Postgres semantics can be expressed in or added to the VDBE. A C-to-VDBE compiler running Doom illustrates that the bytecode works as a real VM. The work starts from the earlier pgmicro experiment and is early-stage but runnable from source. Planned pieces include the Postgres wire protocol, embedded and in-browser use, live materialized views, and WASM-sandboxed extensions, aiming for broad rather than 100% compatibility.