brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: EXPLAIN (ANALYZE, BUFFERS) shows what a PostgreSQL database query really did

Database
~/tips/tip-database-explain-analyze-buffers

EXPLAIN (ANALYZE, BUFFERS) UPDATE orders SET paid = true WHERE id = 42;

Runs a PostgreSQL statement and reports real timings and cache reads for each plan step.

A plain EXPLAIN only shows the planner’s guess. In PostgreSQL I add the options that show what really happened.

BEGIN;
EXPLAIN (ANALYZE, BUFFERS) UPDATE orders SET paid = true WHERE id = 42;
ROLLBACK;

ANALYZE runs the statement and prints the real row counts and timings next to the estimates. BUFFERS adds how many pages came from cache (shared hit) and how many had to be read from disk. Big gaps between estimated and actual rows usually point at stale statistics, which ANALYZE tablename fixes.

Because the statement is really executed, an UPDATE or DELETE changes data. Wrap it in BEGIN and ROLLBACK as above when you only want the plan.

Further reading: PostgreSQL documentation: EXPLAIN

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link