RefLib

Reference library for AI

Back

postgresql_16:3.6.27.3 Backup Control Functions

PostgreSQL 16 > The SQL Language > Functions and Operators > System Administration Functions > Backup Control Functions

The functions shown in (see functions-admin-backup-table) assist in making on-line backups. These functions cannot be executed during recovery (except pg_backup_start, pg_backup_stop, and pg_wal_lsn_diff).

For details about proper usage of these functions, see (see postgresql_16:4.11.3).

| Function Description | | --- | | pg_create_restore_point ( name text ) pg_lsn Creates a named marker record in the write-ahead log that can later be used as a recovery target, and returns the corresponding write-ahead log location. The given name can then be used with (see guc-recovery-target-name) to specify the point up to which recovery will proceed. Avoid creating multiple restore points with the same name, since recovery will stop at the first one whose name matches the recovery target. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_current_wal_flush_lsn () pg_lsn Returns the current write-ahead log flush location (see notes below). | | pg_current_wal_insert_lsn () pg_lsn Returns the current write-ahead log insert location (see notes below). | | pg_current_wal_lsn () pg_lsn Returns the current write-ahead log write location (see notes below). | | pg_backup_start ( label text , fast boolean ) pg_lsn Prepares the server to begin an on-line backup. The only required parameter is an arbitrary user-defined label for the backup. (Typically this would be the name under which the backup dump file will be stored.) If the optional second parameter is given as true, it specifies executing pg_backup_start as quickly as possible. This forces an immediate checkpoint which will cause a spike in I/O operations, slowing any concurrently executing queries. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_backup_stop ( wait_for_archive boolean ) record ( lsn pg_lsn, labelfile text, spcmapfile text ) Finishes performing an on-line backup. The desired contents of the backup label file and the tablespace map file are returned as part of the result of the function and must be written to files in the backup area. These files must not be written to the live data directory (doing so will cause PostgreSQL to fail to restart in the event of a crash). There is an optional parameter of type boolean. If false, the function will return immediately after the backup is completed, without waiting for WAL to be archived. This behavior is only useful with backup software that independently monitors WAL archiving. Otherwise, WAL required to make the backup consistent might be missing and make the backup useless. By default or when this parameter is true, pg_backup_stop will wait for WAL to be archived when archiving is enabled. (On a standby, this means that it will wait only when archive_mode = always. If write activity on the primary is low, it may be useful to run pg_switch_wal on the primary in order to trigger an immediate segment switch.) When executed on a primary, this function also creates a backup history file in the write-ahead log archive area. The history file includes the label given to pg_backup_start, the starting and ending write-ahead log locations for the backup, and the starting and ending times of the backup. After recording the ending location, the current write-ahead log insertion point is automatically advanced to the next write-ahead log file, so that the ending write-ahead log file can be archived immediately to complete the backup. The result of the function is a single record. The lsn column holds the backup's ending write-ahead log location (which again can be ignored). The second column returns the contents of the backup label file, and the third column returns the contents of the tablespace map file. These must be stored as part of the backup and are required as part of the restore process. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_switch_wal () pg_lsn Forces the server to switch to a new write-ahead log file, which allows the current file to be archived (assuming you are using continuous archiving). The result is the ending write-ahead log location plus 1 within the just-completed write-ahead log file. If there has been no write-ahead log activity since the last write-ahead log switch, pg_switch_wal does nothing and returns the start location of the write-ahead log file currently in use. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_walfile_name ( lsn pg_lsn ) text Converts a write-ahead log location to the name of the WAL file holding that location. | | pg_walfile_name_offset ( lsn pg_lsn ) record ( file_name text, file_offset integer ) Converts a write-ahead log location to a WAL file name and byte offset within that file. | | pg_split_walfile_name ( file_name text ) record ( segment_number numeric, timeline_id bigint ) Extracts the sequence number and timeline ID from a WAL file name. | | pg_wal_lsn_diff ( lsn1 pg_lsn, lsn2 pg_lsn ) numeric Calculates the difference in bytes (lsn1 - lsn2) between two write-ahead log locations. This can be used with pg_stat_replication or some of the functions shown in (see functions-admin-backup-table) to get the replication lag. |

pg_current_wal_lsn displays the current write-ahead log write location in the same format used by the above functions. Similarly, pg_current_wal_insert_lsn displays the current write-ahead log insertion location and pg_current_wal_flush_lsn displays the current write-ahead log flush location. The insertion location is the “logical” end of the write-ahead log at any instant, while the write location is the end of what has actually been written out from the server's internal buffers, and the flush location is the last location known to be written to durable storage. The write location is the end of what can be examined from outside the server, and is usually what you want if you are interested in archiving partially-complete write-ahead log files. The insertion and flush locations are made available primarily for server debugging purposes. These are all read-only operations and do not require superuser permissions.

You can use pg_walfile_name_offset to extract the corresponding write-ahead log file name and byte offset from a pg_lsn value. For example: postgres=# SELECT * FROM pg_walfile_name_offset((pg_backup_stop()).lsn); file_name | file_offset --------------------------+------------- 00000001000000000000000D | 4039624 (1 row) Similarly, pg_walfile_name extracts just the write-ahead log file name. When the given write-ahead log location is exactly at a write-ahead log file boundary, both these functions return the name of the preceding write-ahead log file. This is usually the desired behavior for managing write-ahead log archiving behavior, since the preceding file is the last one that currently needs to be archived.

pg_split_walfile_name is useful to compute a LSN from a file offset and WAL file name, for example: postgres=# \set file_name '000000010000000100C000AB' postgres=# \set offset 256 postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset AS lsn FROM pg_split_walfile_name(:'file_name') pd, pg_show_all_settings() ps WHERE ps.name = 'wal_segment_size'; lsn --------------- C001/AB000100 (1 row)

Back