---
title: "Postgres Language Server: implementing the Parser"
description: "A Postgres language server's parser builds on libpg_query rather than a custom grammar, and has to work around two gaps in it."
url: "https://swe.quest/articles/postgres-language-server-implementing-the-parser-3a3e33745c9c0915"
canonical: "https://supabase.com/blog/postgres-language-server-implementing-parser"
source: "Supabase"
published: "2023-12-08"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 14
tags: ["postgres","compilers","rust","developer-tools","sql"]
---

# Postgres Language Server: implementing the Parser

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2023-12-08, 14 min read, Deep dive. Original: [https://supabase.com/blog/postgres-language-server-implementing-parser](https://supabase.com/blog/postgres-language-server-implementing-parser)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)
- [Rust](https://swe.quest/tags/rust.md) (Languages)
- [Developer tools](https://swe.quest/tags/developer-tools.md) (Engineering practices)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- parsers and syntax trees
- Rust procedural macros ([easy posts on Rust](https://swe.quest/tags/rust.md?difficulty=easy))
- Language Server Protocol basics

## Keywords

libpg\_query, Postgres Language Server, Language Server Protocol, Supabase, protobuf, quote crate

## Related posts

- [Postgres Language Server: Initial Release](https://swe.quest/articles/postgres-language-server-initial-release-4b7148c84b3bcff0.md): Supabase · 2025-03-29 · Medium · 13 min read · PostgreSQL, Developer tools, Rust, SQL, Static analysis
  Postgres 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.
- [How pg\_graphql works](https://swe.quest/articles/how-pg-graphql-works-70dff1dd9d5a0c27.md): Supabase · 2024-01-24 · Medium · 9 min read · GraphQL, PostgreSQL, Rust, Compilers
  pg\_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.
- [Bringing psql’s \\d to your web browser](https://swe.quest/articles/bringing-psql-s-d-to-your-web-browser-032015b940df37e2.md): Neon · 2024-04-17 · Medium · 6 min read · PostgreSQL, JavaScript, Developer tools
  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.
- [What To Know About Postgres if You’re Coming From SQL Server](https://swe.quest/articles/what-to-know-about-postgres-if-you-re-coming-from-sql-server-2dbf370eb4f4c29d.md): Neon · 2024-12-10 · Medium · 9 min read · PostgreSQL, SQL, Databases
  Postgres and SQL Server share SQL but differ in dialect details that trip up migrating developers.
- [We're building Postgres in Rust. Using the LLVM of databases](https://swe.quest/articles/we-re-building-postgres-in-rust-using-the-llvm-of-databases-e44d34208278a99e.md): Turso · 2026-07-16 · Easy · 11 min read · PostgreSQL, SQLite, Rust, Query engines, Open source
  Turso, 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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
