Cracking PostgreSQL Interview Questions
Interview preparation for the transactional side of PostgreSQL, organized around five areas: data modeling, indexes, views, roles and functions. It covers modeling 1-1, 1-M and M-M relationships with foreign keys and join tables, and using rich types like JSONB, enums and timestamptz. On performance and access control it explains B-tree and other index types, comparing plans with EXPLAIN ANALYZE, auto-updatable views, GRANTs to PUBLIC, and row level security policies. It also shows PL/pgSQL trigger functions for auto-updating timestamps, advises thinking in sets rather than loops, and introduces CTEs, all with short SQL examples.
Related posts
- PostgREST v10: EXPLAIN and Improved Relationship Detection
Supabase mediumAdeptPostgREST v10 adds HTTP-level EXPLAIN support: requests with the `application/vnd.pgrst.plan` media type, or supabase-js's `explain()` transform, return the PostgreSQL execution plan for reads, writes and RPC, which helps verify that RLS policies are applied and gives you the pg_stat_statements query identifier. Relationship detection also improves. One-to-one relationships are inferred from unique or primary keys on foreign keys, and "computed relationships" use inlinable SQL functions to define joins on views and materialized views that lack foreign keys. A breaking change now requires a join table's primary key to include both foreign key columns before it is detected as many-to-many. EXPLAIN is gated behind `db-plan-enabled` and intended only for development.
- Higher-level CRUD Abstraction for Postgres RLS
Neon mediumAdept
Postgres Row-Level Security requires separate USING and WITH CHECK policies per operation, which gets repetitive for CRUD apps. Neon's Drizzle ORM helper `crudPolicy` takes a role plus `read` and `modify` expressions (true, false, a SQL expression, or null to skip) and generates the select, insert, update and delete policies. A companion `authUid` helper ties pg_session_jwt's `auth.user_id()` to a table column. The post shows the raw SQL for a todos table next to the equivalent concise Drizzle schema. The functionality has since moved into the Neon Data API.
- SQL or NoSQL? Why not use both (with PostgreSQL)?
Supabase mediumAdeptPostgreSQL's JSONB columns let an app store nested, variable-length data (here, a daily food, water and exercise log) as a single row instead of spreading it across several normalized tables, while keeping SQL available for analysis. A worked example builds a calendar table with JSONB log fields and inserts a whole day in one statement. It then uses jsonb_array_elements with the -> and ->> operators to flatten the arrays into rows. Because aggregates cannot wrap set-returning functions, the flattened query goes into a CTE before summing daily calories or filtering for specific items.
- What is new in PostgREST v11.1?
Supabase mediumAdeptPostgREST 11.1 lets impersonated JWT roles (anon, authenticated, service_role) carry Postgres settings via ALTER ROLE ... SET, applied after a NOTIFY pgrst 'reload config'. That enables per-role statement_timeout and pg_plan_filter cost limits, where the cost limit rejects expensive queries at planning time but needs careful tuning. Transaction isolation is now configurable per function or role, alongside bulk JSON inserts that respect column defaults and new any/all filter modifiers. Breaking changes from v10 are small: the Range header is ignored on non-GET requests and Prefer: params=multiple-objects is dropped for RPC, and usage logs show almost no projects relied on either.
- Postgres Roles: What to Know Before You Begin
Neon mediumAdept
Postgres has no separate users and groups, only roles: a role with LOGIN behaves like a user, and a role without it acts as a group whose privileges pass to its members through inheritance. Access depends on privileges at every level of the hierarchy. Reading a table requires CONNECT on the database, USAGE on the schema and SELECT on the table, so revoking schema USAGE silently blocks table-level grants. Common gotchas include the public schema defaults, SECURITY DEFINER functions, search_path hijacking, implicit owner privileges and inconsistent attribute inheritance. Migrations with tangled role and object dependencies are the biggest pain point, so the post recommends mapping dependencies, writing custom scripts beyond pg_dump/pg_restore, testing with a rollback plan, and following least privilege.