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
Show wal_level
SHOW wal_level;

# Inspect the current WAL level setting.

Show max_wal_senders
SHOW max_wal_senders;

# Check capacity for replication sender processes.

Create logical publication
CREATE PUBLICATION app_pub FOR TABLE public.users, public.orders;

# Publish table changes for logical replication.

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

# Subscribe to logical replication changes from another server.

Refresh subscription
ALTER SUBSCRIPTION app_sub REFRESH PUBLICATION;

# Pull publication membership changes to the subscriber.

Create physical replication slot
SELECT * FROM pg_create_physical_replication_slot('standby_1');

# Create a physical slot to retain WAL for a replica.

Drop replication slot
SELECT pg_drop_replication_slot('standby_1');

# Remove a replication slot when it is no longer needed.

Current WAL LSN
SELECT pg_current_wal_lsn();

# Inspect the current write-ahead log position.

## Transactions, Savepoints, and Locking
Begin transaction
BEGIN;

# Start an explicit transaction block.

Commit transaction
COMMIT;

# Persist all changes in the current transaction.

Rollback transaction
ROLLBACK;

# Discard all uncommitted work.

Create savepoint
SAVEPOINT before_step_two;

# Mark a partial rollback point within a transaction.

Rollback to savepoint
ROLLBACK TO SAVEPOINT before_step_two;

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

Lock table
LOCK TABLE orders IN SHARE ROW EXCLUSIVE MODE;

# Acquire an explicit table lock.

Set isolation level
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

# Change isolation for the current transaction.

Set statement timeout
SET statement_timeout = '5s';

# Abort long-running statements automatically.

## Foreign Data Wrappers and Cross-DB Access
Enable postgres_fdw
CREATE EXTENSION IF NOT EXISTS postgres_fdw;

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

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

# Define a remote PostgreSQL server.

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

# Store remote authentication details for one local role.

Import foreign schema
IMPORT FOREIGN SCHEMA public FROM SERVER analytics_remote INTO analytics_fdw;

# Bring remote tables into a local schema automatically.

Create foreign table manually
CREATE FOREIGN TABLE analytics_fdw.events (
  event_id bigint,
  payload jsonb
)
SERVER analytics_remote
OPTIONS (schema_name 'public', table_name 'events');

# Define a foreign table manually.

Drop foreign server
DROP SERVER analytics_remote CASCADE;

# Remove the foreign server and dependent objects.

## Extensions and Specialized Utilities
Enable pg_trgm
CREATE EXTENSION IF NOT EXISTS pg_trgm;

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

Create trigram index
CREATE INDEX idx_users_email_trgm ON public.users USING GIN (email gin_trgm_ops);

# Accelerate ILIKE and fuzzy matches on text.

Enable citext
CREATE EXTENSION IF NOT EXISTS citext;

# Install case-insensitive text type support.

Use CITEXT
ALTER TABLE public.users ALTER COLUMN email TYPE citext;

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

Enable pgstattuple
CREATE EXTENSION IF NOT EXISTS pgstattuple;

# Install size and tuple-stat inspection helpers.

Check table bloat stats
SELECT * FROM pgstattuple('public.users');

# Estimate dead tuples and free space in a relation.

Enable uuid-ossp
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

# Install UUID generation helpers.

Generate UUID
SELECT uuid_generate_v4();

# Generate a random UUID using `uuid-ossp`.

Recommended next

No recommendations yet.