PostgreSQL Advanced Cheat Sheet

Replication, transactions, FDWs, extensions, and specialized PostgreSQL workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Replication and WAL

Foundational streaming replication and WAL commands.

Show wal_level

Inspect the current WAL level setting.

sqlANYwalreplicationsettings
sql
SHOW wal_level;

See summary for usage details.

Show max_wal_senders

Check capacity for replication sender processes.

sqlANYwalreplicationsettings
sql
SHOW max_wal_senders;

See summary for usage details.

Create logical publication

Publish table changes for logical replication.

sqlANYlogical-replicationpublication
sql
CREATE PUBLICATION app_pub FOR TABLE public.users, public.orders;

See summary for usage details.

Create subscription

Subscribe to logical replication changes from another server.

sqlANYlogical-replicationsubscription
sql
CREATE SUBSCRIPTION app_sub
CONNECTION 'host=primary-db port=5432 dbname=appdb user=replicator password=secret'
PUBLICATION app_pub;

See summary for usage details.

Refresh subscription

Pull publication membership changes to the subscriber.

sqlANYlogical-replicationsubscription
sql
ALTER SUBSCRIPTION app_sub REFRESH PUBLICATION;

See summary for usage details.

Create physical replication slot

Create a physical slot to retain WAL for a replica.

sqlANYreplication-slotwal
sql
SELECT * FROM pg_create_physical_replication_slot('standby_1');

See summary for usage details.

Drop replication slot

Remove a replication slot when it is no longer needed.

sqlANYreplication-slotwal
sql
SELECT pg_drop_replication_slot('standby_1');

See summary for usage details.

Current WAL LSN

Inspect the current write-ahead log position.

sqlANYwallsn
sql
SELECT pg_current_wal_lsn();

See summary for usage details.

Transactions, Savepoints, and Locking

Commands for safe changes and contention debugging.

Begin transaction

Start an explicit transaction block.

sqlANYtransaction
sql
BEGIN;

See summary for usage details.

Commit transaction

Persist all changes in the current transaction.

sqlANYtransaction
sql
COMMIT;

See summary for usage details.

Rollback transaction

Discard all uncommitted work.

sqlANYtransaction
sql
ROLLBACK;

See summary for usage details.

Create savepoint

Mark a partial rollback point within a transaction.

sqlANYtransactionsavepoint
sql
SAVEPOINT before_step_two;

See summary for usage details.

Rollback to savepoint

Undo work after the named savepoint while keeping the transaction open.

sqlANYtransactionsavepoint
sql
ROLLBACK TO SAVEPOINT before_step_two;

See summary for usage details.

Lock table

Acquire an explicit table lock.

sqlANYlocktabletransaction
sql
LOCK TABLE orders IN SHARE ROW EXCLUSIVE MODE;

See summary for usage details.

Set isolation level

Change isolation for the current transaction.

sqlANYtransactionisolation
sql
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

See summary for usage details.

Set statement timeout

Abort long-running statements automatically.

sqlANYtimeoutsession
sql
SET statement_timeout = '5s';

See summary for usage details.

Foreign Data Wrappers and Cross-DB Access

Useful federation commands when querying remote systems.

Enable postgres_fdw

Install the built-in foreign data wrapper for PostgreSQL servers.

sqlANYfdwextension
sql
CREATE EXTENSION IF NOT EXISTS postgres_fdw;

See summary for usage details.

Create foreign server

Define a remote PostgreSQL server.

sqlANYfdwserver
sql
CREATE SERVER analytics_remote
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'analytics-db', dbname 'analytics', port '5432');

See summary for usage details.

Create user mapping

Store remote authentication details for one local role.

sqlANYfdwuser-mapping
sql
CREATE USER MAPPING FOR appuser
SERVER analytics_remote
OPTIONS (user 'analytics_user', password 'secret');

See summary for usage details.

Import foreign schema

Bring remote tables into a local schema automatically.

sqlANYfdwimport
sql
IMPORT FOREIGN SCHEMA public FROM SERVER analytics_remote INTO analytics_fdw;

See summary for usage details.

Create foreign table manually

Define a foreign table manually.

sqlANYfdwforeign-table
sql
CREATE FOREIGN TABLE analytics_fdw.events (
  event_id bigint,
  payload jsonb
)
SERVER analytics_remote
OPTIONS (schema_name 'public', table_name 'events');

See summary for usage details.

Drop foreign server

Remove the foreign server and dependent objects.

sqlANYfdwdrop
sql
DROP SERVER analytics_remote CASCADE;

See summary for usage details.

Extensions and Specialized Utilities

High-value extension and utility patterns beyond the basics.

Enable pg_trgm

Install trigram support for similarity search and GIN/GiST indexes.

sqlANYextensionpg_trgm
sql
CREATE EXTENSION IF NOT EXISTS pg_trgm;

See summary for usage details.

Create trigram index

Accelerate ILIKE and fuzzy matches on text.

sqlANYpg_trgmindexsearch
sql
CREATE INDEX idx_users_email_trgm ON public.users USING GIN (email gin_trgm_ops);

See summary for usage details.

Enable citext

Install case-insensitive text type support.

sqlANYextensioncitext
sql
CREATE EXTENSION IF NOT EXISTS citext;

See summary for usage details.

Use CITEXT

Make a column case-insensitive with the `citext` type.

sqlANYcitextalter
sql
ALTER TABLE public.users ALTER COLUMN email TYPE citext;

See summary for usage details.

Enable pgstattuple

Install size and tuple-stat inspection helpers.

sqlANYextensionpgstattuple
sql
CREATE EXTENSION IF NOT EXISTS pgstattuple;

See summary for usage details.

Check table bloat stats

Estimate dead tuples and free space in a relation.

sqlANYpgstattuplebloat
sql
SELECT * FROM pgstattuple('public.users');

See summary for usage details.

Enable uuid-ossp

Install UUID generation helpers.

sqlANYextensionuuid
sql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

See summary for usage details.

Generate UUID

Generate a random UUID using `uuid-ossp`.

sqlANYuuidfunction
sql
SELECT uuid_generate_v4();

See summary for usage details.

Recommended next

No recommendations yet.