Documentation
 
 
 

8.17. System Administration Functions

Table 8-38 shows the functions available to query and alter run-time configuration parameters.

Table 8-38. Configuration Settings Functions

NameReturn TypeDescription
current_setting(setting_name) TEXTcurrent value of setting
set_config(setting_name, new_value, is_local) TEXTset parameter and return new value

The function current_setting yields the current value of the setting setting_name. It corresponds to the SQL command SHOW. An example:

SELECT current_setting('datestyle');

 current_setting
-----------------
 ISO, MDY
(1 row)

set_config sets the parameter setting_name to new_value. If is_local is true, the new value will only apply to the current transaction. If you want the new value to apply for the current session, use false instead. The function corresponds to the SQL command SET. An example:

SELECT set_config('log_statement_stats', 'off', false);

 set_config
------------
 off
(1 row)

The function shown in Table 8-39 sends control signals to other server processes. Use of this function is restricted to superusers.

Table 8-39. Backend Signalling Functions

NameReturn TypeDescription
pg_cancel_backend(pid) INTCancel a backend's current query

This function returns 1 if successful, 0 if not successful. The process ID (pid) of an active backend can be found from the procpid column in the pg_stat_activity view, or by listing the edb-postgres processes on the server with ps.

The functions shown in Table 8-40 assist in making on-line backups. Use of the first three functions is restricted to superusers.

Table 8-40. Backup Control Functions

NameReturn TypeDescription
pg_start_backup(label_text) TEXTSet up for performing on-line backup
pg_stop_backup() TEXTFinish performing on-line backup
pg_switch_xlog() textForce switch to a new transaction log file
pg_current_xlog_location() textGet current transaction log write location
pg_current_xlog_insert_location() textGet current transaction log insert location
pg_xlogfile_name_offset(location text) text, integerConvert transaction log location string to file name and decimal byte offset within file
pg_xlogfile_name(location text) textConvert transaction log location string to file name

pg_start_backup accepts a single parameter which is an arbitrary user-defined label for the backup. (Typically this would be the name under which the backup dump file will be stored.) The function writes a backup label file into the database cluster's data directory, and then returns the backup's starting transaction log location as text. The user need not pay any attention to this result value, but it is provided in case it is of use.

postgres=# select pg_start_backup('label_goes_here');
 pg_start_backup
-----------------
 0/D4445B8
(1 row)

pg_stop_backup removes the label file created by pg_start_backup, and instead creates a backup history file in the transaction archive area. The history file includes the label given to pg_start_backup, the starting and ending transaction log location for the backup, and the starting and ending times of the backup. The return value is the backup's ending transaction log location (which again may be of little interest). After noting the ending location, the current transaction log insertion point is automatically advanced to the next transaction log file, so that the ending transaction log file can be archived immediately to complete the backup.

pg_switch_xlog moves to the next transaction log file, allowing the current file to be archived (assuming you are using continuous archiving). The result is the ending transaction log location within the just-completed transaction log file. If there has been no transaction log activity since the last transaction log switch, pg_switch_xlog does nothing and returns the end location of the previous transaction log file.

pg_current_xlog_location displays the current transaction log write location in the same format used by the above functions. Similarly pg_current_xlog_insert_location displays the current transaction log insertion point. The insertion point is the "logical" end of transaction log at any instant, while the write location is the end of what has actually been written out from the server's internal buffers. 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 transaction log files. The insertion point is made available primarily for server debugging purposes. These are both read-only operations and do not require superuser permissions.

You can use pg_xlogfile_name_offset to extract the corresponding transaction log file name and byte offset from the results of any of the above functions. For example:

postgres=# select * from pg_xlogfile_name_offset(pg_stop_backup());
        file_name         | file_offset 
--------------------------+-------------
 00000001000000000000000D |     4039624
(1 row)

Similarly, pg_xlogfile_name extracts just the transaction log file name. When the given transction log location is exactly at an transaction log file boundary, both these functions return the name of the preceding transaction log file. This is usually the desired behavior for managing transaction log archiving behavior, since the preceding file is the last one that currently needs to be archived.

For details about proper usage of these functions, see Section 36.3.

The functions shown in Table 8-41 manage advisory locks. For details about proper usage of these functions, see Section 11.3.4.

Table 8-41. Advisory Lock Functions

NameReturn TypeDescription
pg_advisory_lock(key bigint) voidObtain exclusive advisory lock
pg_advisory_lock(key1 int, key2 int) voidObtain exclusive advisory lock
pg_advisory_lock_shared(key bigint) voidObtain shared advisory lock
pg_advisory_lock_shared(key1 int, key2 int) voidObtain shared advisory lock
pg_try_advisory_lock(key bigint) booleanObtain exclusive advisory lock if available
pg_try_advisory_lock(key1 int, key2 int) booleanObtain exclusive advisory lock if available
pg_try_advisory_lock_shared(key bigint) booleanObtain shared advisory lock if available
pg_try_advisory_lock_shared(key1 int, key2 int) booleanObtain shared advisory lock if available
pg_advisory_unlock(key bigint) booleanRelease an exclusive advisory lock
pg_advisory_unlock(key1 int, key2 int) booleanRelease an exclusive advisory lock
pg_advisory_unlock_shared(key bigint) booleanRelease a shared advisory lock
pg_advisory_unlock_shared(key1 int, key2 int) booleanRelease a shared advisory lock
pg_advisory_unlock_all() voidRelease all advisory locks held by the current session

pg_advisory_lock locks an application-defined resource, which may 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 lock on the same resource, the function will wait until the resource becomes available. The lock is exclusive. Multiple lock requests stack, so that if the same resource is locked three times it must be also unlocked three times to be released for other sessions' use.

pg_advisory_lock_shared works the same as pg_advisory_lock, except the lock can be shared with other sessions requesting shared locks. Only would-be exclusive lockers are locked out.

pg_try_advisory_lock is similar to pg_advisory_lock, except the function will not wait for the lock to become available. It will either obtain the lock immediately and return true, or return false if the lock cannot be acquired now.

pg_try_advisory_lock_shared works the same as pg_try_advisory_lock, except it attempts to acquire shared rather than exclusive lock.

pg_advisory_unlock will release a previously-acquired exclusive advisory lock. It will return true if the lock is successfully released. If the lock was in fact not held, it will return false, and in addition, an SQL warning will be raised by the server.

pg_advisory_unlock_shared works the same as pg_advisory_unlock, except to release a shared advisory lock.

pg_advisory_unlock_all will release all advisory locks held by the current session. (This function is implicitly invoked at session end, even if the client disconnects ungracefully.)

 
 ©2004-2007 EnterpriseDB All Rights Reserved