9.27. System Administration Functions
- 9.27.1. Configuration Settings Functions
- 9.27.2. Server Signaling Functions
- 9.27.3. Backup Control Functions
- 9.27.4. Recovery Control Functions
- 9.27.5. Snapshot Synchronization Functions
- 9.27.6. Replication Management Functions
- 9.27.7. Database Object Management Functions
- 9.27.8. Index Maintenance Functions
- 9.27.9. Generic File Access Functions
- 9.27.10. Advisory Lock Functions
- 9.27.11. Compression Control Functions
- 9.27.12. Operation Log Support Functions
- 9.27.13. Debugging Functions
- 9.27.2. Server Signaling Functions
The functions described in this section are used to control and monitor aPostgres Pro installation.
9.27.1. Configuration Settings Functions
Table 9.83 shows the functions available to query and alter run-time configuration parameters.
Table 9.83. Configuration Settings Functions
Function Description Example(s) |
---|
Returns the current value of the setting
|
Sets the parameter
|
Sets one or more parameters provided in the |
Loads the library with the provided name for the backend with the specified PID. You can load only one library at a time. When run without superuser privileges, |
Functionspg_backend_set_config
andpg_backend_load_library
are designed to set configuration parameters and load shared libraries in other sessions, which may be useful for tracing sessions with unexpected behavior. To avoid potential security issues, these functions can only be called by a superuser.
Note
This functionality cannot be used together with the built-in connection pooler described inChapter 33.
By default,pg_backend_set_config
andpg_backend_load_library
returntrue
, without waiting for response from the other backend. You can use the optionalwait
parameter to specify the time interval, in seconds, for which to wait for the confirmation that the target backend has received the command. If the confirmation is received, the functions returntrue
. Otherwise,false
is returned. Note that the returned value does not reflect the actual result of the operation. If a configuration update fails, an error occurs on the target backend, and the current command on this backend is aborted.
Consider the following examples:
SELECT pg_backend_set_config(pg_backend_pid(), 'statement_timeout=10000'); pg_backend_set_config----------------------- t(1 row)SELECT pg_backend_set_config(pg_backend_pid(), 'statement_timeout=20000 lock_timeout=10000'); pg_backend_set_config----------------------- t(1 row)SELECT pg_backend_set_config(pg_backend_pid(), 'fsync=on');ERROR: parameter "fsync" cannot be changed nowSELECT pg_backend_set_config(pg_backend_pid(), 'log_min_messages=INFO');ERROR: permission denied to set parameter "log_min_messages"
SELECT pg_backend_load_library(pg_backend_pid(), 'pgoutput'); pg_backend_load_library------------------------- t(1 row)
9.27.2. Server Signaling Functions
The functions shown inTable 9.84 send control signals to other server processes. Use of these functions is restricted to superusers by default but access may be granted to others usingGRANT
, with noted exceptions.
Each of these functions returnstrue
if successful andfalse
otherwise.
Table 9.84. Server Signaling Functions
pg_cancel_backend
andpg_terminate_backend
send signals (SIGINT orSIGTERM respectively) to backend processes identified by process ID. The process ID of an active backend can be found from thepid
column of thepg_stat_activity
view, or by listing thepostgres
processes on the server (usingps on Unix or theTask Manager onWindows). The role of an active backend can be found from theusename
column of thepg_stat_activity
view.
9.27.3. Backup Control Functions
The functions shown inTable 9.85 assist in making on-line backups. These functions cannot be executed during recovery (except non-exclusivepg_start_backup
, non-exclusivepg_stop_backup
,pg_is_in_backup
,pg_backup_start_time
andpg_wal_lsn_diff
).
For details about proper usage of these functions, seeSection 25.3.
Table 9.85. Backup Control Functions
Function Description |
---|
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 withrecovery_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. |
Returns the current write-ahead log flush location (see notes below). |
Returns the current write-ahead log insert location (see notes below). |
Returns the current write-ahead log write location (see notes below). |
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 When used in exclusive mode, this function writes a backup label file ( This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. |
Finishes performing an exclusive or non-exclusive on-line backup. The There is an optional second parameter of type 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 The result of the function is a single record. The This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. |
Finishes performing an exclusive on-line backup. This simplified version is equivalent to This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. |
Returns true if an on-line exclusive backup is in progress. |
Returns the start time of the current on-line exclusive backup if one is in progress, otherwise |
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, This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. |
Converts a write-ahead log location to the name of the WAL file holding that location. |
Converts a write-ahead log location to a WAL file name and byte offset within that file. |
Calculates the difference in bytes ( |
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 andpg_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 usepg_walfile_name_offset
to extract the corresponding write-ahead log file name and byte offset from apg_lsn
value. For example:
postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup()); 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.
9.27.4. Recovery Control Functions
The functions shown inTable 9.86 provide information about the current status of a standby server. These functions may be executed both during recovery and in normal running.
Table 9.86. Recovery Information Functions
The functions shown inTable 9.87 control the progress of recovery. These functions may be executed only during recovery.
Table 9.87. Recovery Control Functions
pg_wal_replay_pause
andpg_wal_replay_resume
cannot be executed while a promotion is ongoing. If a promotion is triggered while recovery is paused, the paused state ends and promotion continues.
If streaming replication is disabled, the paused state may continue indefinitely without a problem. If streaming replication is in progress then WAL records will continue to be received, which will eventually fill available disk space, depending upon the duration of the pause, the rate of WAL generation and available disk space.
9.27.5. Snapshot Synchronization Functions
Postgres Pro allows database sessions to synchronize their snapshots. Asnapshot determines which data is visible to the transaction that is using the snapshot. Synchronized snapshots are necessary when two or more sessions need to see identical content in the database. If two sessions just start their transactions independently, there is always a possibility that some third transaction commits between the executions of the twoSTART TRANSACTION
commands, so that one session sees the effects of that transaction and the other does not.
To solve this problem,Postgres Pro allows a transaction toexport the snapshot it is using. As long as the exporting transaction remains open, other transactions canimport its snapshot, and thereby be guaranteed that they see exactly the same view of the database that the first transaction sees. But note that any database changes made by any one of these transactions remain invisible to the other transactions, as is usual for changes made by uncommitted transactions. So the transactions are synchronized with respect to pre-existing data, but act normally for changes they make themselves.
Snapshots are exported with thepg_export_snapshot
function, shown inTable 9.88, and imported with theSET TRANSACTION command.
Table 9.88. Snapshot Synchronization Functions
Function Description |
---|
Saves the transaction's current snapshot and returns a A transaction can export more than one snapshot, if needed. Note that doing so is only useful in |
9.27.6. Replication Management Functions
The functions shown inTable 9.89 are for controlling and interacting with replication features. SeeSection 26.2.5,Section 26.2.6, andChapter 52 for information about the underlying features. Use of functions for replication origin is restricted to superusers. Use of functions for replication slots is restricted to superusers and users havingREPLICATION
privilege.
Many of these functions have equivalent commands in the replication protocol; seeSection 55.4.
The functions described inSection 9.27.3,Section 9.27.4, andSection 9.27.5 are also relevant for replication.
Table 9.89. Replication Management Functions
Function Description |
---|
Creates a new physical replication slot named |
Drops the physical or logical replication slot named |
Creates a new logical (decoding) replication slot named |
Copies an existing physical replication slot named |
Copies an existing logical replication slot named |
Returns changes in the slot |
Behaves just like the |
Behaves just like the |
Behaves just like the |
Advances the current confirmed position of a replication slot named |
Creates a replication origin with the given external name, and returns the internal ID assigned to it. |
Deletes a previously-created replication origin, including any associated replay progress. |
Looks up a replication origin by name and returns the internal ID. If no such replication origin is found, |
Marks the current session as replaying from the given origin, allowing replay progress to be tracked. Can only be used if no origin is currently selected. Use |
Cancels the effects of |
Returns true if a replication origin has been selected in the current session. |
Returns the replay location for the replication origin selected in the current session. The parameter |
Marks the current transaction as replaying a transaction that has committed at the givenLSN and timestamp. Can only be called when a replication origin has been selected using |
Cancels the effects of |
Sets replication progress for the given node to the given location. This is primarily useful for setting up the initial location, or setting a new location after configuration changes and similar. Be aware that careless use of this function can lead to inconsistently replicated data. |
Returns the replay location for the given replication origin. The parameter |
Emits a logical decoding message. This can be used to pass generic messages to logical decoding plugins through WAL. The |
9.27.7. Database Object Management Functions
The functions shown inTable 9.90 calculate the disk space usage of database objects, or assist in presentation of usage results. All these functions return sizes measured in bytes. If an OID that does not represent an existing object is passed to one of these functions,NULL
is returned.
Table 9.90. Database Object Size Functions
Function Description |
---|
Shows the number of bytes used to store any individual data value. If applied directly to a table column value, this reflects any compression that was done. |
Computes the total disk space used by the database with the specified name or OID. To use this function, you must have |
Computes the total disk space used by indexes attached to the specified table. |
Computes the disk space used by one“fork” of the specified relation. (Note that for most purposes it is more convenient to use the higher-level functions
|
Accepts the OID or name of a temporary table and returns the total size in bytes of |
Converts a size in human-readable format (as returned by |
Converts a size in bytes into a more easily human-readable format with size units (bytes, kB, MB, GB or TB as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 10242 = 1048576 bytes, and so on. |
Computes the disk space used by the specified table, excluding indexes (but including its TOAST table if any, free space map, and visibility map). |
Computes the total disk space used in the tablespace with the specified name or OID. To use this function, you must have |
Computes the total disk space used by the specified table, including all indexes andTOAST data. The result is equivalent to |
The functions above that operate on tables or indexes accept a The functions shown inTable 9.91 assist in identifying the specific disk files associated with database objects. Table 9.91. Database Object Location Functions Function Description Returns the“filenode” number currently assigned to the specified relation. The filenode is the base component of the file name(s) used for the relation (seeSection 70.1 for more information). For most relations the result is the same as Returns the entire file path name (relative to the database cluster's data directory, Returns a relation's OID given the tablespace OID and filenode it is stored under. This is essentially the inverse mapping of Table 9.92 lists functions used to manage collations. Table 9.92. Collation Management Functions Function Description Returns the actual version of the collation object as it is currently installed in the operating system. If this is different from the value in Adds collations to the system catalog Table 9.93 lists functions that provide information about the structure of partitioned tables. Table 9.93. Partitioning Information Functions For example, to check the total size of the data contained in a partitioned tableregclass
argument, which is simply the OID of the table or index in thepg_class
system catalog. You do not have to look up the OID by hand, however, since theregclass
data type's input converter will do the work for you. Just write the table name enclosed in single quotes so that it looks like a literal constant. For compatibility with the handling of ordinarySQL names, the string will be converted to lower case unless it contains double quotes around the table name.pg_relation_filenode
(relation
regclass
) →oid
pg_class
.relfilenode
, but for certain system catalogsrelfilenode
is zero and this function must be used to get the correct value. The function returns NULL if passed a relation that does not have storage, such as a view.pg_relation_filepath
(relation
regclass
) →text
PGDATA
) of the relation.pg_filenode_relation
(tablespace
oid
,filenode
oid
) →regclass
pg_relation_filepath
. For a relation in the database's default tablespace, the tablespace can be specified as zero. ReturnsNULL
if no relation in the current database is associated with the given values.pg_collation_actual_version
(oid
) →text
pg_collation
.collversion
, then objects depending on the collation might need to be rebuilt. See alsoALTER COLLATION.pg_import_system_collations
(schema
regnamespace
) →integer
pg_collation
based on all the locales it finds in the operating system. This is whatinitdb
uses; seeSection 23.2.2 for more details. If additional locales are installed into the operating system later on, this function can be run again to add collations for the new locales. Locales that match existing entries inpg_collation
will be skipped. (But collation objects based on locales that are no longer present in the operating system are not removed by this function.) Theschema
parameter would typically bepg_catalog
, but that is not a requirement; the collations could be installed into some other schema as well. The function returns the number of new collation objects it created. Use of this function is restricted to superusers.measurement
, one could use the following query:SELECT pg_size_pretty(sum(pg_relation_size(relid))) AS total_size FROM pg_partition_tree('measurement');
9.27.8. Index Maintenance Functions
Table 9.94 shows the functions available for index maintenance tasks. (Note that these maintenance tasks are normally done automatically by autovacuum; use of these functions is only required in special cases.) These functions cannot be executed during recovery. Use of these functions is restricted to superusers and the owner of the given index.
Table 9.94. Index Maintenance Functions
Function Description |
---|
Scans the specified BRIN index to find page ranges in the base table that are not currently summarized by the index; for any such range it creates a new summary index tuple by scanning those table pages. Returns the number of new page range summaries that were inserted into the index. |
Summarizes the page range covering the given block, if not already summarized. This is like |
Removes the BRIN index tuple that summarizes the page range covering the given table block, if there is one. |
Cleans up the“pending” list of the specified GIN index by moving entries in it, in bulk, to the main GIN data structure. Returns the number of pages removed from the pending list. If the argument is a GIN index built with the |
9.27.9. Generic File Access Functions
The functions shown inTable 9.95 provide native access to files on the machine hosting the server. Only files within the database cluster directory and thelog_directory
can be accessed, unless the user is a superuser or is granted the rolepg_read_server_files
. Use a relative path for files in the cluster directory, and a path matching thelog_directory
configuration setting for log files.
Note that granting users the EXECUTE privilege onpg_read_file()
, or related functions, allows them the ability to read any file on the server that the database server process can read; these functions bypass all in-database privilege checks. This means that, for example, a user with such access is able to read the contents of thepg_authid
table where authentication information is stored, as well as read any table data in the database. Therefore, granting access to these functions should be carefully considered.
Some of these functions take an optionalmissing_ok
parameter, which specifies the behavior when the file or directory does not exist. Iftrue
, the function returnsNULL
or an empty result set, as appropriate. Iffalse
, an error is raised. The default isfalse
.
Table 9.95. Generic File Access Functions
9.27.10. Advisory Lock Functions
The functions shown inTable 9.96 manage advisory locks. For details about proper use of these functions, seeSection 13.3.5.
All these functions are intended to be used to lock application-defined resources, which can be identified either by a single 64-bit key value or two 32-bit key values (note that these two key spaces do not overlap). If another session already holds a conflicting lock on the same resource identifier, the functions will either wait until the resource becomes available, or return afalse
result, as appropriate for the function. Locks can be either shared or exclusive: a shared lock does not conflict with other shared locks on the same resource, only with exclusive locks. Locks can be taken at session level (so that they are held until released or the session ends) or at transaction level (so that they are held until the current transaction ends; there is no provision for manual release). Multiple session-level lock requests stack, so that if the same resource identifier is locked three times there must then be three unlock requests to release the resource in advance of session end.
Table 9.96. Advisory Lock Functions
9.27.11. Compression Control Functions
The functions shown inTable 9.97 provide information about CFS state and activity and control CFS garbage collection.
Table 9.97. Compression Control Functions
Name | Return Type | Description |
---|---|---|
| integer | Starts garbage collection using the specified number of workers. You can only run this function to start the garbage collection manually if background garbage collection is disabled. |
| boolean | Enables/disables background garbage collection. Alternatively, you can use thecfs_gc configuration variable. |
| integer | Performs garbage collection for a particular table. This function returns the number of processed segments. |
| text | Displays the CFS version and the specific compression algorithm used. |
| float8 | Estimates the effect of table compression. Returns the average compression ratio for the first ten blocks of the relation. |
| float8 | Returns the actual compression ratio for all segments of the compressed relation. |
| float8 | Returns the average fragmentation ratio of the relation files. |
| int64 | Returns the total size of pages processed by CFS during garbage collection. |
| int64 | Returns the number of pages processed by CFS during garbage collection. |
| int64 | Returns the number of files compacted by CFS during garbage collection. |
| int64 | Returns the number of files scanned by CFS during garbage collection. |
9.27.12. Operation Log Support Functions
An operation log stores information about system events of critical importance, such as an upgrade, execution ofpg_resetwal and so on. This information is not interesting to regular users, but is highly useful for vendor's technical support. Recording to the operation log is only done at the system level (without any actions on the part of the user), and SQL functions are used to read the operation log. The function shown inTable 9.98 reads the operation log.
Table 9.98. Operation Log Support Functions
Function Description |
---|
Where:
This system function is created automatically for new databases and needs to be created for existing databases, as follows: CREATE OR REPLACE FUNCTION pg_operation_log( OUT event text, OUT edition text, OUT version text, OUT lsn pg_lsn, OUT last timestamptz, OUT count int4) RETURNS SETOF record LANGUAGE INTERNAL STABLE STRICT PARALLEL SAFEAS 'pg_operation_log'; |
This is an example of what thepg_operation_log
function returns:
select * from pg_operation_log(); event | edition | version | lsn | last | count------------+---------+---------+-----------+------------------------+------- startup | vanilla | 10.22.0 | 0/8000028 | 2022-10-27 23:06:27+03 | 1 pg_upgrade | 1c | 15.0.0 | 0/8000028 | 2022-10-27 23:06:27+03 | 1 startup | 1c | 15.0.0 | 0/80001F8 | 2022-10-27 23:06:53+03 | 2(3 rows)
Note
If an operation log is empty, before a record forpg_upgrade
, a record is added forstartup
with the previousversion
having the following specifics: as there is no information inpg_upgrade on the patch version number of the previous version for each edition (1c
,std
,ent
), the patch version number (third number in the version number) is set to zero. For an upgrade from1c
orvanilla
, it cannot be distinguished whether the edition of the database before upgrade isvanilla
or1c
, andvanilla
edition is used for thestartup
record.
9.27.13. Debugging Functions
The function shown inTable 9.99 can assist you in low-level activities, such as debugging or exploring corruptedPostgres Pro databases.
Table 9.99. Snapshot Synchronization Functions
Name | Return Type | Description |
---|---|---|
| void | Sets the current transaction to ignore MVCC rules and see all versions of data. |
Usepg_snapshot_any
with care. Run it in a transaction with isolation levelREPEATABLE READ
or higher, otherwise the specific snapshot will be replaced by a new one by the next query. Only superusers can run this function.
Note
If you created the database cluster using the server version that did not provide this function, execute the command:
CREATE FUNCTION pg_snapshot_any() RETURNS void AS 'pg_snapshot_any' LANGUAGE internal;