pg_graphql: A GraphQL extension for PostgreSQL
pg_graphql is an open-source PostgreSQL extension, written in C and SQL, that reflects a GraphQL schema from existing tables and resolves queries entirely inside the database. Supabase built it because its 1 GB free-tier VMs had no memory left for a separate GraphQL process like Graphile or Hasura. Each GraphQL request is transpiled into a single SQL statement that aggregates results as JSON. This avoids N+1 queries, respects row level security, and gives multi-operation requests transactional semantics. Pagination follows the Relay connection spec using keyset pagination, the API is one SQL function (gql.resolve) exposed over HTTP via PostgREST, and a benchmark shows about 2,205 requests/second on a 4-core machine.
Related posts
- pg_graphql v1.0
Supabase mediumAdeptpg_graphql, a PostgreSQL extension that compiles GraphQL requests into a single SQL query, reaches 1.0 with a SemVer backwards-compatibility guarantee. Running inside Postgres instead of as a separate server keeps its memory footprint negligible on 1 GB Free Plan instances. That design also gives it Postgres roles and RLS for security, an always-in-sync schema with no N+1 queries, and ACID rollback across multi-mutation requests. The original pure-SQL transpiler hit limits near 4,000 lines because SQL functions pass by value and the planner stops inlining deeply nested calls, so it was rewritten in Rust with pgx. After the rewrite, overhead is under 300µs per request, about 1,060 queries/sec per connection from SQL, and about 645 req/s through the full HTTP and auth stack on Free Plan hardware.
- GraphQL is now available in Supabase
Supabase mediumAdeptSupabase has made GraphQL generally available through pg_graphql, an open source PostgreSQL extension that reflects tables, columns and foreign keys into GraphQL types, fields and relations, queryable from SQL or over HTTP. Authorization is handled by Postgres itself: exposed types and introspection are filtered by the JWT's SQL role, and row level security applies automatically. Performance comes from transpiling every GraphQL query into exactly one SQL query, which avoids N+1 problems and duplicated join data, plus caching transpiled queries as prepared statements and making all operations bulk. On Free Plan hardware this yields about 377 requests/s via the API and 656 queries/s via SQL, and a HackerNews clone built with The Guild demonstrates the stack.
- 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.
- pg_graphql: Postgres functions now supported
Supabase mediumAdeptpg_graphql 1.4+ reflects Postgres user-defined functions into the GraphQL schema, giving custom SQL logic a GraphQL entry point. Argument nullability follows from defaults, and function volatility decides placement: immutable and stable functions become Query fields, while volatile functions become Mutations. Functions returning setof a table type get Relay-style pagination, filtering, sorting and foreign-key relationships automatically. Overloaded, variadic, void-returning, nameless-argument, array-argument and tuple-argument functions are not yet supported.
- pg_jsonschema: JSON Schema support for Postgres
Supabase mediumAdeptpg_jsonschema is a Postgres extension that validates `json` and `jsonb` values against a JSON Schema through two functions, `json_matches_schema` and `jsonb_matches_schema`. Used inside a check constraint, it keeps the flexibility of document columns, such as storing raw webhook payloads instead of normalizing them into many joined tables, while still enforcing type, required-field and no-extra-properties rules. In the example, an unconstrained `json` column handled only 2 of 8 test inputs correctly, and the schema-backed check constraint handled all 8. It is available on Supabase via `create extension pg_jsonschema` or through Docker Compose.