Operations cheat sheet - PostgreSQL wiki (original) (raw)

Introduction

This page is aimed at learning from the wisdom of the PostgreSQL community.

People involved in the PostgreSQL community, as organizations or individuals, are posting vast amounts of useful information on blogs, wikis, and websites. However, they are scattered, and it will not be easy to find the information you are looking for.

Therefore, this page has compiled a collection of links to articles that the editor(s) informative, from hundreds of blog sites registered on Planet PostgreSQL, plus the PostgreSQL wiki and websites. Some notable topics from these articles are picked up here, trying to organize and summarize them for introduction purposes. Please feel free to add links to the articles you find helpful to others. Adding the summaries of them is also appreciated in addition to the links.

Notes

Architecture

Client-Server architecture

Logical database structures

Database object hierarchy

Object identifier (OID)

Physical database structures

Directories

Files in data directory

Page (= block)

Table row

Instance

The instance is a group of server-side processes, their local memory, and the shared memory.

Processes

Memory

Reading and writing database data

Query processing

  1. A client connects to a database, sends a query (SQL command) to the server, and receives the result.
  2. The parser first checks the query for correct syntax. Then, it interprets the semantics of the query to understand which tables, views, functions, data types, etc. are referenced.
  3. The rewrite system (rewriter) transforms the query based on the rules stored in the system catalog pg_rewrite. One example is the view: a query that accesses a view is rewritten to use the base table.
  4. The planner/optimizer creates a query plan.
  5. The executor executes the query plan and returns the result set to the client.

Notes

References

PostgreSQL Documentation

Other resources

Reliability and availability

Connection

What to check when troubleshooting connectivity

Connection termination and query cancellation

Connection failover

WAL

What WAL is for

WAL structure

Writing WAL

Transaction

ACID: what they are attributed to

Transaction ID (XID)

MVCC: Multi-Version Concurrency Control

Hint bit

Lock

A lock request can wait, even when the requested mode is compatible with held locks.

Prepared transactions lurk holding locks

Data integrity validation

Data checksums

WAL CRC

Utilities to detect, bypass, or repair data corruption (some could be dangerous!)

Backup and recovery

Backup and recovery methods

  1. File system level backup (binary format)
  2. SQL Dump with pg_dump/pg_dumpall (text format)
  3. Continuous archiving (binary format)

Characteristics of backup and recovery methods

Streaming replication

Architecture

General administration

Causes of replication lag

Hot standby

Monitoring replication lag

Logical replication

Architecture

General administration

Replication conflicts

Monitoring

References

PostgreSQL Documentation

Connection

WAL

Transaction

Lock

Data integrity validation

Backup and recovery

Streaming replication

Logical replication

Security

Authentication

Encrypt password when changing it

In-database authentication profile is very limited

Implementing password complexity: use either of:

Tracking failed login attempts: Do either of:

Role privileges are inherited by default

Predefined roles

Default privileges

References

PostgreSQL Documentation

Authentication

Authorization

Hiding data

Manageability

Memory

A large result set causes out of memory on the client

Common causes of server-side out of memory (OOM) issues

Cannot retrieve a large bytea value

Storage

Common causes of full storage

Storage quota

Logging and debugging

FATAL: database is starting up

Avoid excessive logging by restricting targets

Debug-logging can be enabled for a session without cluttering the server log

psql -d postgres -c "select 1"

Find out what psql's backslash commands do

Deleting duplicate rows

`WITH x AS (SELECT some_table dup, min(ctid) FROM some_table GROUP BY 1 HAVING count() > 1 ) DELETE FROM some_table USING x WHERE (some_table) = (dup) AND some_table.ctid <> x.min RETURNING some_table.;

`

Vacuum

Purposes of vacuum

Vacuum types

Main steps of vacuum

  1. Starts a transaction.
    • When there are multiple target relations, vacuum starts and commits a transaction for each relation to release locks as soon as possible.
  2. Gets an Share Update Exclusive lock for a heap and opens it. Non-wrap-around-prevention vacuum gives up vacuuming the relation if the relation cannot get the lock, emitting the following message.
    • LOG: skipping of vacuum "rel_name" --- lock not available
  3. Gets Row Exclusive locks for the indexes and opens them.
  4. Allocates the work memory to accumulate the TIDs of dead tuples.
  5. Repeats the following steps until the entire heap has been processed:
    • Scans the heap: Accumulates dead tuple TIDs in the work memory until it gets full or the end of the heap is reached. The item IDs for the dead tuples are retained. Also, prunes and defragments each page if required, and possibly freezes live tuples.
    • Vacuums the indexes: Delete index entries that contain dead tuple TIDs.
    • Vacuums the heap: Reclaims the item IDs for the dead tuples. This is done here, not while scanning the heap, because the item ID cannot be freed until the index entries pointing to it have been deleted.
    • Updates the FSM and VM during the above processing.
  6. Cleans up the indexes.
    • Updates every index's stats in pg_class's relpages and reltuples.
    • Closes the indexes but retains their locks until transaction end.
  7. Truncate the heap so as to return empty pages at the end of the relation to the operating system.
    • The data file is truncated if the heap has at least the lesser of 1,000 blocks and (relation_size / 16) contiguous empty blocks at its end.
    • Takes an Access Exclusive lock on the heap. If another transaction holds a conflicting lock, wait for at most 5 seconds. If the lock cannot be obtained, gives up truncating.
    • Scans backwards the heap to verify that the end pages are still empty. Periodically checks if another transaction is waiting for a conflicting lock. If someone else is waiting, releases the Access Exclusive lock and gives up truncating.
  8. Updates relation stats.
    • Updates pg_class's relpages, reltuples, relallvisible, relhasindex, relhasrules, relhastriggers, relfrozenxid, and relminmxid.
  9. Close the relation.
  10. Commits a transaction.
  11. Vacuums the relation's TOAST table.
  12. Repeats the above processing for each relation.
  13. Updates database stats.

Autovacuum is designed to be non-intrusive

Autovacuum is not run against a relation

Why vacuum does not remove dead tuples

Reducing the risk of XID wrap-around

Configuration to speed up autovacuum

Upgrade

Characteristics of versions

Major upgrade methods

  1. pg_dumpall/pg_dump and psql/pg_restore: easy, long downtime
  2. pg_upgrade: relatively easy, shorter downtime
  3. Logical replication: complex setup and operation, minimal downtime

Overview of pg_upgrade

Main steps of pg_upgrade

  1. Creates output directory for log and intermediate files.
  2. Checks that the major version of the target cluster is newer, and that the old and new clusters are binary-compatible by comparing information in pg_control.
  3. Gets the list of databases and relations (table, index, TOAST table, matview) of the old cluster.
  4. Gets the list of library names that contain C-language functions.
  5. Performs various checks to find blockers of upgrade, such as the inability to connect to databases and the presence of prepared transactions.
  6. Creates a dump of global objects by running pg_dumpall --globals-only.
  7. Creates dumps of each database by running pg_dump --schema-only. This is parallelized by spawning one process or thread for each database when --jobs is specified.
  8. Checks the previously extracted loadable libraries with C-language functions exist in the new cluster by running LOAD.
  9. Copies commit log files in pg_xact/ and MultiXact files in pg_multixact/ from the old cluster to the new cluster.
  10. Sets next XID and MultiXact ID for the new cluster to take over the old cluster.
  11. Restores global objects in the new cluster by running psql.
  12. Restores database schemas in the new cluster by running pg_restore. This is parallelized by spawning one process or thread for each database when --jobs is specified.
  13. Gets the list of databases and relations (table, index, TOAST table, matview) of the new cluster.
  14. Links or copies user relation files from the old cluster to the new cluster. This is parallelized by spawning one process or thread for each tablespace when --jobs is specified.
  15. Sets next OID for the new cluster.
  16. Creates a script to delete the old cluster (delete_old_cluster.sh). This script removes the data directory and tablespace version directories.
  17. Reports extensions that should be updated and creates update_extensions.sql. This script contains a list of ALTER EXTENSION ... UPDATE commands.

Log files for troubleshooting pg_upgrade

References

PostgreSQL Documentation

Memory

Storage

Internationalization and localization

Logging

Vacuum

Partitioning

Upgrade

Miscellaneous tips

Application development

Data type

Numeric

Timestamp

Binary

Tips for using large objects

Sequence

There is no gapless sequence

References

PostgreSQL Documentation

Data type

Large object

Pagination

Sequence

Scalability and performance

Many connections

Want to handle many concurrent clients? Then do:

Detecting problems

Increase track_activity_query_size for ORMs (Object Relational Mappers)

Utilize plprofiler to diagnose the bottlenecks of PL/pgSQL functions and procedures

Logging

Server logging could block but not show waits

Import and export

ALTER TABLE SET UNLOGGED/LOGGED is heavy

Queries or the first vacuum are slow after loading data with COPY

Foreign Data Wrapper (FDW)

Speeding up queries via postgres_fdw

Full-text search queries got much slower after inserting many new documents

Utility

Fast random sampling of table rows

Memory

Use huge pages

Tips for shared buffers

Tips for local memory

Network

Watch out for network latency when running lots of short SQL commands

Cursor

Lock

Utilize fast-path locks for high performance

HOT

Take advantage of HOT (Heap-Only Tuple)

Table layout

For best storage efficiency and performance, declare table columns from largest fixed length types (e.g., bigint, timestamp) to smallest fixed length types (e.g., smallint, bool), then variable length types (e.g., numeric, text, bytea)

Use functions returning a composite type in FROM clause instead of SELECT column list

TOAST (The Oversized-Attribute Storage Technique)

Transaction

Killed (dead) index tuples can give mysterious query speedup

Subtransactions can be harmful

MultiXact can harm performance under the hood

WAL and checkpoint

Overview of checkpoint

Reducing the impact of checkpoints

Index

Disadvantages of indexes

Benefits of indexes you might not notice

Making an index-only scan work

Query planning

Pitfalls of ANALYZE

Use of a set-returning function could lead to a poor query plan

Custom plan and generic plan

References

PostgreSQL Documentation

Many connections

Detecting problems

Memory

Storage

Network

Table layout

SQL tricks

Transaction

HOT

WAL and checkpoint

Lock

Index

Query planning

Join

Logging

Parallel query

Import and export

Foreign Data Wrapper (FDW)

Trigger

Full text search

Utility