99 commands · 6 cheat sheets · SQL

Performance and Planning master quick reference

Browse 99 commands from 6 focused cheat sheets on page 1 of 1. Each example opens its matching detailed section.

Performance and Planning · 15 commands

PostgreSQL Indexes and Query Plans Cheat Sheet

Open full cheat sheet
UseSyntaxExamples
Create a B-tree indexCREATE INDEX orders_customer_idx ON orders (customer_id);View examples
Enforce uniquenessCREATE UNIQUE INDEX users_email_uidx ON users (email);View examples
Index several columnsCREATE INDEX orders_customer_created_idx ON orders (customer_id, created_at DESC);View examples
Include payload columnsCREATE INDEX orders_status_idx ON orders (status) INCLUDE (total);View examples
Index a subsetCREATE INDEX orders_open_idx ON orders (created_at) WHERE status = 'open';View examples
Index an expressionCREATE INDEX users_lower_email_idx ON users (lower(email));View examples
Index composite values with GINCREATE INDEX documents_tags_idx ON documents USING GIN (tags);View examples
Summarize ordered storage with BRINCREATE INDEX events_time_brin_idx ON events USING BRIN (occurred_at);View examples
Build without blocking writesCREATE INDEX CONCURRENTLY orders_customer_idx ON orders (customer_id);View examples
Drop with less write blockingDROP INDEX CONCURRENTLY orders_customer_idx;View examples
Inspect estimated planEXPLAIN SELECT * FROM orders WHERE customer_id = 42;View examples
Measure executionEXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = 42;View examples
Return a machine-readable planEXPLAIN (FORMAT JSON) SELECT * FROM orders WHERE customer_id = 42;View examples
Refresh planner statisticsANALYZE orders;View examples
List table indexesSELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'orders';View examples

Performance and Planning · 17 commands

PostgreSQL Parallel Query and JIT Cheat Sheet

Open full cheat sheet
UseSyntaxExamples
Find parallel nodesEXPLAIN (COSTS, VERBOSE) SELECT count(*) FROM app.orders;View examples
Measure workers launchedEXPLAIN (ANALYZE, SUMMARY) SELECT count(*) FROM app.orders;View examples
Show worker detailEXPLAIN (ANALYZE, VERBOSE) SELECT count(*) FROM app.orders;View examples
Set a local worker capSET LOCAL max_parallel_workers_per_gather = 4;View examples
Inspect worker settingsSELECT name, setting FROM pg_settings WHERE name LIKE 'max_parallel%';View examples
Inspect setup costSHOW parallel_setup_cost;View examples
Inspect table thresholdSHOW min_parallel_table_scan_size;View examples
Label a function safeALTER FUNCTION app.net_amount(numeric, numeric) PARALLEL SAFE;View examples
Label a function restrictedALTER FUNCTION app.read_backend_state() PARALLEL RESTRICTED;View examples
Disable parallel useALTER FUNCTION app.write_audit(text) PARALLEL UNSAFE;View examples
Disable parallelism locallySET LOCAL max_parallel_workers_per_gather = 0;View examples
Check transaction isolationSHOW transaction_isolation;View examples
Enable JIT locallySET LOCAL jit = on;View examples
Inspect the JIT thresholdSHOW jit_above_cost;View examples
Disable JIT locallySET LOCAL jit = off;View examples
Show JIT availabilitySHOW jit_provider;View examples
Track nondefault plan settingsEXPLAIN (SETTINGS, SUMMARY) SELECT sum(total) FROM app.orders;View examples

Performance and Planning · 16 commands

PostgreSQL Prepared Statements and Plan Caching Cheat Sheet

Open full cheat sheet
UseSyntaxExamples
Prepare a typed queryPREPARE order_by_id (bigint) AS SELECT id, status, total FROM app.orders WHERE id = $1;View examples
Execute a statementEXECUTE order_by_id(42001);View examples
Prepare a writePREPARE add_event (uuid, text) AS INSERT INTO app.events(account_id, kind) VALUES ($1, $2) RETURNING id;View examples
Fix parameter meaningPREPARE events_since (timestamptz) AS SELECT id FROM app.events WHERE occurred_at >= $1::timestamptz;View examples
Inspect the chosen planEXPLAIN (ANALYZE, BUFFERS, SETTINGS) EXECUTE orders_for_tenant(42);View examples
Test custom planningSET LOCAL plan_cache_mode = 'force_custom_plan';View examples
Test generic planningSET LOCAL plan_cache_mode = 'force_generic_plan';View examples
Restore automatic choiceSET plan_cache_mode = 'auto';View examples
List prepared statementsSELECT name, parameter_types, generic_plans, custom_plans FROM pg_prepared_statements ORDER BY name;View examples
Find generic-plan usersSELECT name, generic_plans, custom_plans FROM pg_prepared_statements WHERE generic_plans > 0;View examples
Discard cached plansDISCARD PLANS;View examples
Release one statementDEALLOCATE order_by_id;View examples
Release all statementsDEALLOCATE ALL;View examples
Pin object resolutionPREPARE active_accounts AS SELECT id FROM app.accounts WHERE disabled_at IS NULL;View examples
Check server versionSHOW server_version;View examples
Identify two-phase stateSELECT gid, prepared, owner, database FROM pg_prepared_xacts ORDER BY prepared;View examples

Performance and Planning · 16 commands

PostgreSQL Statistics and Cardinality Estimation Cheat Sheet

Open full cheat sheet
UseSyntaxExamples
Inspect estimated rows onlyEXPLAIN SELECT * FROM app.orders WHERE status = 'open';View examples
Measure actual rowsEXPLAIN (ANALYZE, BUFFERS) SELECT * FROM app.orders WHERE status = 'open';View examples
Expose planning settingsEXPLAIN (ANALYZE, SETTINGS) SELECT count(*) FROM app.orders;View examples
Analyze one tableANALYZE app.orders;View examples
Analyze selected columnsANALYZE app.orders (tenant_id, status);View examples
Inspect table statisticsSELECT * FROM pg_stats WHERE schemaname = 'app' AND tablename = 'orders';View examples
Check approximate table sizeSELECT reltuples, relpages FROM pg_class WHERE oid = 'app.orders'::regclass;View examples
Set a column targetALTER TABLE app.orders ALTER COLUMN tenant_id SET STATISTICS 500;View examples
Restore the default targetALTER TABLE app.orders ALTER COLUMN tenant_id SET STATISTICS DEFAULT;View examples
Create dependency statisticsCREATE STATISTICS app.orders_dep (dependencies) ON tenant_id, region FROM app.orders;View examples
Create MCV statisticsCREATE STATISTICS app.orders_mcv (mcv) ON tenant_id, status FROM app.orders;View examples
Inspect extended statisticsSELECT * FROM pg_stats_ext WHERE statistics_name = 'orders_tenant_status';View examples
Set a proportional distinct estimateALTER TABLE app.events ALTER COLUMN tenant_id SET (n_distinct = -0.02);View examples
Remove a distinct overrideALTER TABLE app.events ALTER COLUMN tenant_id RESET (n_distinct);View examples
Analyze a partition hierarchyANALYZE app.events;View examples
Inspect extended-stat data safelySELECT statistics_name, kinds FROM pg_stats_ext WHERE schemaname = 'app';View examples

Performance and Planning · 17 commands

PostgreSQL System Catalogs and Information Schema Cheat Sheet

Open full cheat sheet
UseSyntaxExamples
List visible tablesSELECT table_schema, table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE';View examples
List PostgreSQL relationsSELECT oid::regclass, relkind FROM pg_catalog.pg_class WHERE relnamespace = 'app'::regnamespace;View examples
Resolve the catalog schemaSELECT pg_catalog.current_schema();View examples
Resolve a relation OIDSELECT to_regclass('app.orders');View examples
Find a relation kindSELECT relkind FROM pg_class WHERE oid = 'app.orders'::regclass;View examples
List portable columnsSELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = 'app' AND table_name = 'orders';View examples
Render a PostgreSQL typeSELECT format_type(atttypid, atttypmod) FROM pg_attribute WHERE attrelid = 'app.orders'::regclass AND attnum > 0;View examples
Resolve a type safelySELECT to_regtype('pg_catalog.int8');View examples
Show index definitionsSELECT indexrelid::regclass, indisvalid, pg_get_indexdef(indexrelid) FROM pg_index WHERE indrelid = 'app.orders'::regclass;View examples
Show a constraint definitionSELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'orders_pkey';View examples
Check table privilegeSELECT has_table_privilege('app_reader', 'app.orders', 'SELECT');View examples
Check schema usageSELECT has_schema_privilege('app_reader', 'app', 'USAGE');View examples
Check function executionSELECT has_function_privilege('app_reader', 'app.total(bigint)', 'EXECUTE');View examples
Describe a catalog objectSELECT pg_describe_object('pg_class'::regclass, 'app.orders'::regclass, 0);View examples
Inspect dependenciesSELECT * FROM pg_depend WHERE refobjid = 'app.orders'::regclass;View examples
Check server version numberSELECT current_setting('server_version_num')::int;View examples
Check recovery stateSELECT pg_is_in_recovery();View examples

Performance and Planning · 18 commands

PostgreSQL Table Partitioning and Pruning Cheat Sheet

Open full cheat sheet
UseSyntaxExamples
Create a range-partitioned tableCREATE TABLE events (event_id bigint, occurred_at timestamptz NOT NULL) PARTITION BY RANGE (occurred_at);View examples
Create a list-partitioned tableCREATE TABLE accounts (account_id bigint, region_code text NOT NULL) PARTITION BY LIST (region_code);View examples
Create a hash-partitioned tableCREATE TABLE jobs (job_id bigint, tenant_id bigint NOT NULL) PARTITION BY HASH (tenant_id);View examples
Create a monthly partitionCREATE TABLE events_2026_08 PARTITION OF events FOR VALUES FROM ('2026-08-01') TO ('2026-09-01');View examples
Create a default partitionCREATE TABLE events_default PARTITION OF events DEFAULT;View examples
Create a list partitionCREATE TABLE accounts_americas PARTITION OF accounts FOR VALUES IN ('BR', 'CA', 'US');View examples
Create a hash partitionCREATE TABLE jobs_h0 PARTITION OF jobs FOR VALUES WITH (MODULUS 4, REMAINDER 0);View examples
Create matching child indexesCREATE INDEX events_tenant_time_idx ON events (tenant_id, occurred_at);View examples
Declare partition-safe uniquenessPRIMARY KEY (event_id, occurred_at)View examples
Filter with matching range boundsWHERE occurred_at >= TIMESTAMPTZ '2026-08-01 00:00:00+00' AND occurred_at < TIMESTAMPTZ '2026-09-01 00:00:00+00'View examples
Inspect selected partitionsEXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE occurred_at >= TIMESTAMPTZ '2026-08-01 00:00:00+00';View examples
Check pruning configurationSHOW enable_partition_pruning;View examples
Build an attachable tableCREATE TABLE events_2026_09 (LIKE events INCLUDING DEFAULTS INCLUDING CONSTRAINTS);View examples
Validate the intended boundALTER TABLE events_2026_09 VALIDATE CONSTRAINT events_2026_09_bound;View examples
Attach a prepared partitionALTER TABLE events ATTACH PARTITION events_2026_09 FOR VALUES FROM ('2026-09-01') TO ('2026-10-01');View examples
List a partition hierarchySELECT relid, parentrelid, isleaf, level FROM pg_partition_tree('events'::regclass);View examples
Inspect partition boundsSELECT relname, pg_get_expr(relpartbound, oid) FROM pg_class WHERE relispartition ORDER BY relname;View examples
Analyze the partitioned parentANALYZE events;View examples
CMDMEMO TERMINALREAD ONLY