|
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.26. System Administration Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="functions-info.html" title="9.25. System Information Functions and Operators" /><link rel="next" href="functions-trigger.html" title="9.27. Trigger Functions" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.26. System Administration Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-info.html" title="9.25. System Information Functions and Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-trigger.html" title="9.27. Trigger Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-ADMIN"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.26. System Administration Functions</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-ADMIN-SET">9.26.1. Configuration Settings Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-ADMIN-SIGNAL">9.26.2. Server Signaling Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-ADMIN-BACKUP">9.26.3. Backup Control Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-RECOVERY-CONTROL">9.26.4. Recovery Control Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-SNAPSHOT-SYNCHRONIZATION">9.26.5. Snapshot Synchronization Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-REPLICATION">9.26.6. Replication Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-ADMIN-DBOBJECT">9.26.7. Database Object Management Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-ADMIN-INDEX">9.26.8. Index Maintenance Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-ADMIN-GENFILE">9.26.9. Generic File Access Functions</a></span></dt><dt><span class="sect2"><a href="functions-admin.html#FUNCTIONS-ADVISORY-LOCKS">9.26.10. Advisory Lock Functions</a></span></dt></dl></div><p>
- The functions described in this section are used to control and
- monitor a <span class="productname">PostgreSQL</span> installation.
- </p><div class="sect2" id="FUNCTIONS-ADMIN-SET"><div class="titlepage"><div><div><h3 class="title">9.26.1. Configuration Settings Functions</h3></div></div></div><p>
- <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-SET-TABLE" title="Table 9.82. Configuration Settings Functions">Table 9.82</a> shows the functions
- available to query and alter run-time configuration parameters.
- </p><div class="table" id="FUNCTIONS-ADMIN-SET-TABLE"><p class="title"><strong>Table 9.82. Configuration Settings Functions</strong></p><div class="table-contents"><table class="table" summary="Configuration Settings Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <a id="id-1.5.8.31.3.3.2.2.1.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">current_setting(<em class="parameter"><code>setting_name</code></em> [, <em class="parameter"><code>missing_ok</code></em> ])</code></code>
- </td><td><code class="type">text</code></td><td>get current value of setting</td></tr><tr><td>
- <a id="id-1.5.8.31.3.3.2.2.2.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">set_config(<em class="parameter"><code>setting_name</code></em>,
- <em class="parameter"><code>new_value</code></em>,
- <em class="parameter"><code>is_local</code></em>)</code></code>
- </td><td><code class="type">text</code></td><td>set parameter and return new value</td></tr></tbody></table></div></div><br class="table-break" /><a id="id-1.5.8.31.3.4" class="indexterm"></a><a id="id-1.5.8.31.3.5" class="indexterm"></a><a id="id-1.5.8.31.3.6" class="indexterm"></a><p>
- The function <code class="function">current_setting</code> yields the
- current value of the setting <em class="parameter"><code>setting_name</code></em>.
- It corresponds to the <acronym class="acronym">SQL</acronym> command
- <code class="command">SHOW</code>. An example:
- </p><pre class="programlisting">
- SELECT current_setting('datestyle');
-
- current_setting
- -----------------
- ISO, MDY
- (1 row)
- </pre><p>
-
- If there is no setting named <em class="parameter"><code>setting_name</code></em>,
- <code class="function">current_setting</code> throws an error
- unless <em class="parameter"><code>missing_ok</code></em> is supplied and is
- <code class="literal">true</code>.
- </p><p>
- <code class="function">set_config</code> sets the parameter
- <em class="parameter"><code>setting_name</code></em> to
- <em class="parameter"><code>new_value</code></em>. If
- <em class="parameter"><code>is_local</code></em> is <code class="literal">true</code>, the
- new value will only apply to the current transaction. If you want
- the new value to apply for the current session, use
- <code class="literal">false</code> instead. The function corresponds to the
- SQL command <code class="command">SET</code>. An example:
- </p><pre class="programlisting">
- SELECT set_config('log_statement_stats', 'off', false);
-
- set_config
- ------------
- off
- (1 row)
- </pre><p>
- </p></div><div class="sect2" id="FUNCTIONS-ADMIN-SIGNAL"><div class="titlepage"><div><div><h3 class="title">9.26.2. Server Signaling Functions</h3></div></div></div><a id="id-1.5.8.31.4.2" class="indexterm"></a><a id="id-1.5.8.31.4.3" class="indexterm"></a><a id="id-1.5.8.31.4.4" class="indexterm"></a><a id="id-1.5.8.31.4.5" class="indexterm"></a><a id="id-1.5.8.31.4.6" class="indexterm"></a><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-SIGNAL-TABLE" title="Table 9.83. Server Signaling Functions">Table 9.83</a> send control signals to
- other server processes. Use of these functions is restricted to
- superusers by default but access may be granted to others using
- <code class="command">GRANT</code>, with noted exceptions.
- </p><div class="table" id="FUNCTIONS-ADMIN-SIGNAL-TABLE"><p class="title"><strong>Table 9.83. Server Signaling Functions</strong></p><div class="table-contents"><table class="table" summary="Server Signaling Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_cancel_backend(<em class="parameter"><code>pid</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Cancel a backend's current query. This is also allowed if the
- calling role is a member of the role whose backend is being canceled or
- the calling role has been granted <code class="literal">pg_signal_backend</code>,
- however only superusers can cancel superuser backends.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_reload_conf()</code></code>
- </td><td><code class="type">boolean</code></td><td>Cause server processes to reload their configuration files</td></tr><tr><td>
- <code class="literal"><code class="function">pg_rotate_logfile()</code></code>
- </td><td><code class="type">boolean</code></td><td>Rotate server's log file</td></tr><tr><td>
- <code class="literal"><code class="function">pg_terminate_backend(<em class="parameter"><code>pid</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Terminate a backend. This is also allowed if the calling role
- is a member of the role whose backend is being terminated or the
- calling role has been granted <code class="literal">pg_signal_backend</code>,
- however only superusers can terminate superuser backends.
- </td></tr></tbody></table></div></div><br class="table-break" /><p>
- Each of these functions returns <code class="literal">true</code> if
- successful and <code class="literal">false</code> otherwise.
- </p><p>
- <code class="function">pg_cancel_backend</code> and <code class="function">pg_terminate_backend</code>
- send signals (<span class="systemitem">SIGINT</span> or <span class="systemitem">SIGTERM</span>
- respectively) to backend processes identified by process ID.
- The process ID of an active backend can be found from
- the <code class="structfield">pid</code> column of the
- <code class="structname">pg_stat_activity</code> view, or by listing the
- <code class="command">postgres</code> processes on the server (using
- <span class="application">ps</span> on Unix or the <span class="application">Task
- Manager</span> on <span class="productname">Windows</span>).
- The role of an active backend can be found from the
- <code class="structfield">usename</code> column of the
- <code class="structname">pg_stat_activity</code> view.
- </p><p>
- <code class="function">pg_reload_conf</code> sends a <span class="systemitem">SIGHUP</span> signal
- to the server, causing configuration files
- to be reloaded by all server processes.
- </p><p>
- <code class="function">pg_rotate_logfile</code> signals the log-file manager to switch
- to a new output file immediately. This works only when the built-in
- log collector is running, since otherwise there is no log-file manager
- subprocess.
- </p></div><div class="sect2" id="FUNCTIONS-ADMIN-BACKUP"><div class="titlepage"><div><div><h3 class="title">9.26.3. Backup Control Functions</h3></div></div></div><a id="id-1.5.8.31.5.2" class="indexterm"></a><a id="id-1.5.8.31.5.3" class="indexterm"></a><a id="id-1.5.8.31.5.4" class="indexterm"></a><a id="id-1.5.8.31.5.5" class="indexterm"></a><a id="id-1.5.8.31.5.6" class="indexterm"></a><a id="id-1.5.8.31.5.7" class="indexterm"></a><a id="id-1.5.8.31.5.8" class="indexterm"></a><a id="id-1.5.8.31.5.9" class="indexterm"></a><a id="id-1.5.8.31.5.10" class="indexterm"></a><a id="id-1.5.8.31.5.11" class="indexterm"></a><a id="id-1.5.8.31.5.12" class="indexterm"></a><a id="id-1.5.8.31.5.13" class="indexterm"></a><a id="id-1.5.8.31.5.14" class="indexterm"></a><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-BACKUP-TABLE" title="Table 9.84. Backup Control Functions">Table 9.84</a> assist in making on-line backups.
- These functions cannot be executed during recovery (except
- non-exclusive <code class="function">pg_start_backup</code>,
- non-exclusive <code class="function">pg_stop_backup</code>,
- <code class="function">pg_is_in_backup</code>, <code class="function">pg_backup_start_time</code>
- and <code class="function">pg_wal_lsn_diff</code>).
- </p><div class="table" id="FUNCTIONS-ADMIN-BACKUP-TABLE"><p class="title"><strong>Table 9.84. Backup Control Functions</strong></p><div class="table-contents"><table class="table" summary="Backup Control Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_create_restore_point(<em class="parameter"><code>name</code></em> <code class="type">text</code>)</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Create a named point for performing restore (restricted to superusers by default, but other users can be granted EXECUTE to run the function)</td></tr><tr><td>
- <code class="literal"><code class="function">pg_current_wal_flush_lsn()</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Get current write-ahead log flush location</td></tr><tr><td>
- <code class="literal"><code class="function">pg_current_wal_insert_lsn()</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Get current write-ahead log insert location</td></tr><tr><td>
- <code class="literal"><code class="function">pg_current_wal_lsn()</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Get current write-ahead log write location</td></tr><tr><td>
- <code class="literal"><code class="function">pg_start_backup(<em class="parameter"><code>label</code></em> <code class="type">text</code> [<span class="optional">, <em class="parameter"><code>fast</code></em> <code class="type">boolean</code> [<span class="optional">, <em class="parameter"><code>exclusive</code></em> <code class="type">boolean</code> </span>]</span>])</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Prepare for performing on-line backup (restricted to superusers by default, but other users can be granted EXECUTE to run the function)</td></tr><tr><td>
- <code class="literal"><code class="function">pg_stop_backup()</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Finish performing exclusive on-line backup (restricted to superusers by default, but other users can be granted EXECUTE to run the function)</td></tr><tr><td>
- <code class="literal"><code class="function">pg_stop_backup(<em class="parameter"><code>exclusive</code></em> <code class="type">boolean</code> [<span class="optional">, <em class="parameter"><code>wait_for_archive</code></em> <code class="type">boolean</code> </span>])</code></code>
- </td><td><code class="type">setof record</code></td><td>Finish performing exclusive or non-exclusive on-line backup (restricted to superusers by default, but other users can be granted EXECUTE to run the function)</td></tr><tr><td>
- <code class="literal"><code class="function">pg_is_in_backup()</code></code>
- </td><td><code class="type">bool</code></td><td>True if an on-line exclusive backup is still in progress.</td></tr><tr><td>
- <code class="literal"><code class="function">pg_backup_start_time()</code></code>
- </td><td><code class="type">timestamp with time zone</code></td><td>Get start time of an on-line exclusive backup in progress.</td></tr><tr><td>
- <code class="literal"><code class="function">pg_switch_wal()</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Force switch to a new write-ahead log file (restricted to superusers by default, but other users can be granted EXECUTE to run the function)</td></tr><tr><td>
- <code class="literal"><code class="function">pg_walfile_name(<em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)</code></code>
- </td><td><code class="type">text</code></td><td>Convert write-ahead log location to file name</td></tr><tr><td>
- <code class="literal"><code class="function">pg_walfile_name_offset(<em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)</code></code>
- </td><td><code class="type">text</code>, <code class="type">integer</code></td><td>Convert write-ahead log location to file name and decimal byte offset within file</td></tr><tr><td>
- <code class="literal"><code class="function">pg_wal_lsn_diff(<em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)</code></code>
- </td><td><code class="type">numeric</code></td><td>Calculate the difference between two write-ahead log locations</td></tr></tbody></table></div></div><br class="table-break" /><p>
- <code class="function">pg_start_backup</code> accepts an arbitrary user-defined label for
- the backup. (Typically this would be the name under which the backup dump
- file will be stored.) When used in exclusive mode, the function writes a
- backup label file (<code class="filename">backup_label</code>) and, if there are any links
- in the <code class="filename">pg_tblspc/</code> directory, a tablespace map file
- (<code class="filename">tablespace_map</code>) into the database cluster's data directory,
- performs a checkpoint, and then returns the backup's starting write-ahead
- log location as text. The user can ignore this result value, but it is
- provided in case it is useful. When used in non-exclusive mode, the
- contents of these files are instead returned by the
- <code class="function">pg_stop_backup</code> function, and should be written to the backup
- by the caller.
-
- </p><pre class="programlisting">
- postgres=# select pg_start_backup('label_goes_here');
- pg_start_backup
- -----------------
- 0/D4445B8
- (1 row)
- </pre><p>
- There is an optional second parameter of type <code class="type">boolean</code>. If <code class="literal">true</code>,
- it specifies executing <code class="function">pg_start_backup</code> as quickly as
- possible. This forces an immediate checkpoint which will cause a
- spike in I/O operations, slowing any concurrently executing queries.
- </p><p>
- In an exclusive backup, <code class="function">pg_stop_backup</code> removes the label file
- and, if it exists, the <code class="filename">tablespace_map</code> file created by
- <code class="function">pg_start_backup</code>. In a non-exclusive backup, the contents of
- the <code class="filename">backup_label</code> and <code class="filename">tablespace_map</code> are returned
- in the result of the function, and should be written to files in the
- backup (and not in the data directory). There is an optional second
- parameter of type <code class="type">boolean</code>. If false, the <code class="function">pg_stop_backup</code>
- will return immediately after the backup is completed without waiting for
- WAL to be archived. This behavior is only useful for backup
- software which independently monitors WAL archiving. Otherwise, WAL
- required to make the backup consistent might be missing and make the backup
- useless. When this parameter is set to true, <code class="function">pg_stop_backup</code>
- will wait for WAL to be archived when archiving is enabled; on the standby,
- this means that it will wait only when <code class="varname">archive_mode = always</code>.
- If write activity on the primary is low, it may be useful to run
- <code class="function">pg_switch_wal</code> on the primary in order to trigger
- an immediate segment switch.
- </p><p>
- When executed on a primary, the function also creates a backup history file
- in the write-ahead log
- archive area. The history file includes the label given to
- <code class="function">pg_start_backup</code>, the starting and ending write-ahead log locations for
- the backup, and the starting and ending times of the backup. The return
- value is the backup's ending write-ahead log location (which again
- can be ignored). 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.
- </p><p>
- <code class="function">pg_switch_wal</code> moves to the next write-ahead log file, allowing the
- current file to be archived (assuming you are using continuous archiving).
- The return value is the ending write-ahead log location + 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,
- <code class="function">pg_switch_wal</code> does nothing and returns the start location
- of the write-ahead log file currently in use.
- </p><p>
- <code class="function">pg_create_restore_point</code> creates a named write-ahead log
- record that can be used as recovery target, and returns the corresponding
- write-ahead log location. The given name can then be used with
- <a class="xref" href="runtime-config-wal.html#GUC-RECOVERY-TARGET-NAME">recovery_target_name</a> 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.
- </p><p>
- <code class="function">pg_current_wal_lsn</code> displays the current write-ahead log write
- location in the same format used by the above functions. Similarly,
- <code class="function">pg_current_wal_insert_lsn</code> displays the current write-ahead log
- insertion location and <code class="function">pg_current_wal_flush_lsn</code> displays the
- current write-ahead log flush location. The insertion location is the <span class="quote">“<span class="quote">logical</span>”</span>
- 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 flush
- location is the location guaranteed 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 both read-only operations and do not
- require superuser permissions.
- </p><p>
- You can use <code class="function">pg_walfile_name_offset</code> to extract the
- corresponding write-ahead log file name and byte offset from the results of any of the
- above functions. For example:
- </p><pre class="programlisting">
- postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
- file_name | file_offset
- --------------------------+-------------
- 00000001000000000000000D | 4039624
- (1 row)
- </pre><p>
- Similarly, <code class="function">pg_walfile_name</code> 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.
- </p><p>
- <code class="function">pg_wal_lsn_diff</code> calculates the difference in bytes
- between two write-ahead log locations. It can be used with
- <code class="structname">pg_stat_replication</code> or some functions shown in
- <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-BACKUP-TABLE" title="Table 9.84. Backup Control Functions">Table 9.84</a> to get the replication lag.
- </p><p>
- For details about proper usage of these functions, see
- <a class="xref" href="continuous-archiving.html" title="25.3. Continuous Archiving and Point-in-Time Recovery (PITR)">Section 25.3</a>.
- </p></div><div class="sect2" id="FUNCTIONS-RECOVERY-CONTROL"><div class="titlepage"><div><div><h3 class="title">9.26.4. Recovery Control Functions</h3></div></div></div><a id="id-1.5.8.31.6.2" class="indexterm"></a><a id="id-1.5.8.31.6.3" class="indexterm"></a><a id="id-1.5.8.31.6.4" class="indexterm"></a><a id="id-1.5.8.31.6.5" class="indexterm"></a><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-RECOVERY-INFO-TABLE" title="Table 9.85. Recovery Information Functions">Table 9.85</a> provide information
- about the current status of the standby.
- These functions may be executed both during recovery and in normal running.
- </p><div class="table" id="FUNCTIONS-RECOVERY-INFO-TABLE"><p class="title"><strong>Table 9.85. Recovery Information Functions</strong></p><div class="table-contents"><table class="table" summary="Recovery Information Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_is_in_recovery()</code></code>
- </td><td><code class="type">bool</code></td><td>True if recovery is still in progress.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_last_wal_receive_lsn()</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Get last write-ahead log location received and synced to disk by
- streaming replication. While streaming replication is in progress
- this will increase monotonically. If recovery has completed this will
- remain static at
- the value of the last WAL record received and synced to disk during
- recovery. If streaming replication is disabled, or if it has not yet
- started, the function returns NULL.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_last_wal_replay_lsn()</code></code>
- </td><td><code class="type">pg_lsn</code></td><td>Get last write-ahead log location replayed during recovery.
- If recovery is still in progress this will increase monotonically.
- If recovery has completed then this value will remain static at
- the value of the last WAL record applied during that recovery.
- When the server has been started normally without recovery
- the function returns NULL.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_last_xact_replay_timestamp()</code></code>
- </td><td><code class="type">timestamp with time zone</code></td><td>Get time stamp of last transaction replayed during recovery.
- This is the time at which the commit or abort WAL record for that
- transaction was generated on the primary.
- If no transactions have been replayed during recovery, this function
- returns NULL. Otherwise, if recovery is still in progress this will
- increase monotonically. If recovery has completed then this value will
- remain static at the value of the last transaction applied during that
- recovery. When the server has been started normally without recovery
- the function returns NULL.
- </td></tr></tbody></table></div></div><br class="table-break" /><a id="id-1.5.8.31.6.8" class="indexterm"></a><a id="id-1.5.8.31.6.9" class="indexterm"></a><a id="id-1.5.8.31.6.10" class="indexterm"></a><a id="id-1.5.8.31.6.11" class="indexterm"></a><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-RECOVERY-CONTROL-TABLE" title="Table 9.86. Recovery Control Functions">Table 9.86</a> control the progress of recovery.
- These functions may be executed only during recovery.
- </p><div class="table" id="FUNCTIONS-RECOVERY-CONTROL-TABLE"><p class="title"><strong>Table 9.86. Recovery Control Functions</strong></p><div class="table-contents"><table class="table" summary="Recovery Control Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_is_wal_replay_paused()</code></code>
- </td><td><code class="type">bool</code></td><td>True if recovery is paused.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_promote(<em class="parameter"><code>wait</code></em> <code class="type">boolean</code> DEFAULT true, <em class="parameter"><code>wait_seconds</code></em> <code class="type">integer</code> DEFAULT 60)</code></code>
- </td><td><code class="type">boolean</code></td><td>
- Promotes a physical standby server. With <em class="parameter"><code>wait</code></em>
- set to <code class="literal">true</code> (the default), the function waits until
- promotion is completed or <em class="parameter"><code>wait_seconds</code></em> seconds
- have passed, and returns <code class="literal">true</code> if promotion is
- successful and <code class="literal">false</code> otherwise.
- If <em class="parameter"><code>wait</code></em> is set to <code class="literal">false</code>, the
- function returns <code class="literal">true</code> immediately after sending
- <code class="literal">SIGUSR1</code> to the postmaster to trigger the promotion.
- This function is restricted to superusers by default, but other users
- can be granted EXECUTE to run the function.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_wal_replay_pause()</code></code>
- </td><td><code class="type">void</code></td><td>Pauses recovery immediately (restricted to superusers by default, but other users can be granted EXECUTE to run the function).
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_wal_replay_resume()</code></code>
- </td><td><code class="type">void</code></td><td>Restarts recovery if it was paused (restricted to superusers by default, but other users can be granted EXECUTE to run the function).
- </td></tr></tbody></table></div></div><br class="table-break" /><p>
- While recovery is paused no further database changes are applied.
- If in hot standby, all new queries will see the same consistent snapshot
- of the database, and no further query conflicts will be generated until
- recovery is resumed.
- </p><p>
- If streaming replication is disabled, the paused state may continue
- indefinitely without problem. While streaming replication is in
- progress 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.
- </p></div><div class="sect2" id="FUNCTIONS-SNAPSHOT-SYNCHRONIZATION"><div class="titlepage"><div><div><h3 class="title">9.26.5. Snapshot Synchronization Functions</h3></div></div></div><a id="id-1.5.8.31.7.2" class="indexterm"></a><p>
- <span class="productname">PostgreSQL</span> allows database sessions to synchronize their
- snapshots. A <em class="firstterm">snapshot</em> 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 two <code class="command">START TRANSACTION</code> commands,
- so that one session sees the effects of that transaction and the other
- does not.
- </p><p>
- To solve this problem, <span class="productname">PostgreSQL</span> allows a transaction to
- <em class="firstterm">export</em> the snapshot it is using. As long as the exporting
- transaction remains open, other transactions can <em class="firstterm">import</em> 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.
- </p><p>
- Snapshots are exported with the <code class="function">pg_export_snapshot</code> function,
- shown in <a class="xref" href="functions-admin.html#FUNCTIONS-SNAPSHOT-SYNCHRONIZATION-TABLE" title="Table 9.87. Snapshot Synchronization Functions">Table 9.87</a>, and
- imported with the <a class="xref" href="sql-set-transaction.html" title="SET TRANSACTION"><span class="refentrytitle">SET TRANSACTION</span></a> command.
- </p><div class="table" id="FUNCTIONS-SNAPSHOT-SYNCHRONIZATION-TABLE"><p class="title"><strong>Table 9.87. Snapshot Synchronization Functions</strong></p><div class="table-contents"><table class="table" summary="Snapshot Synchronization Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_export_snapshot()</code></code>
- </td><td><code class="type">text</code></td><td>Save the current snapshot and return its identifier</td></tr></tbody></table></div></div><br class="table-break" /><p>
- The function <code class="function">pg_export_snapshot</code> saves the current snapshot
- and returns a <code class="type">text</code> string identifying the snapshot. This string
- must be passed (outside the database) to clients that want to import the
- snapshot. The snapshot is available for import only until the end of the
- transaction that exported it. A transaction can export more than one
- snapshot, if needed. Note that doing so is only useful in <code class="literal">READ
- COMMITTED</code> transactions, since in <code class="literal">REPEATABLE READ</code> and
- higher isolation levels, transactions use the same snapshot throughout
- their lifetime. Once a transaction has exported any snapshots, it cannot
- be prepared with <a class="xref" href="sql-prepare-transaction.html" title="PREPARE TRANSACTION"><span class="refentrytitle">PREPARE TRANSACTION</span></a>.
- </p><p>
- See <a class="xref" href="sql-set-transaction.html" title="SET TRANSACTION"><span class="refentrytitle">SET TRANSACTION</span></a> for details of how to use an
- exported snapshot.
- </p></div><div class="sect2" id="FUNCTIONS-REPLICATION"><div class="titlepage"><div><div><h3 class="title">9.26.6. Replication Functions</h3></div></div></div><p>
- The functions shown
- in <a class="xref" href="functions-admin.html#FUNCTIONS-REPLICATION-TABLE" title="Table 9.88. Replication SQL Functions">Table 9.88</a> are for
- controlling and interacting with replication features.
- See <a class="xref" href="warm-standby.html#STREAMING-REPLICATION" title="26.2.5. Streaming Replication">Section 26.2.5</a>,
- <a class="xref" href="warm-standby.html#STREAMING-REPLICATION-SLOTS" title="26.2.6. Replication Slots">Section 26.2.6</a>, and
- <a class="xref" href="replication-origins.html" title="Chapter 49. Replication Progress Tracking">Chapter 49</a>
- for information about the underlying features.
- Use of functions for replication origin is restricted to superusers.
- Use of functions for replication slot is restricted to superusers
- and users having <code class="literal">REPLICATION</code> privilege.
- </p><p>
- Many of these functions have equivalent commands in the replication
- protocol; see <a class="xref" href="protocol-replication.html" title="52.4. Streaming Replication Protocol">Section 52.4</a>.
- </p><p>
- The functions described in
- <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-BACKUP" title="9.26.3. Backup Control Functions">Section 9.26.3</a>,
- <a class="xref" href="functions-admin.html#FUNCTIONS-RECOVERY-CONTROL" title="9.26.4. Recovery Control Functions">Section 9.26.4</a>, and
- <a class="xref" href="functions-admin.html#FUNCTIONS-SNAPSHOT-SYNCHRONIZATION" title="9.26.5. Snapshot Synchronization Functions">Section 9.26.5</a>
- are also relevant for replication.
- </p><div class="table" id="FUNCTIONS-REPLICATION-TABLE"><p class="title"><strong>Table 9.88. Replication <acronym class="acronym">SQL</acronym> Functions</strong></p><div class="table-contents"><table class="table" summary="Replication SQL Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.1.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_create_physical_replication_slot(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code> [<span class="optional">, <em class="parameter"><code>immediately_reserve</code></em> <code class="type">boolean</code>, <em class="parameter"><code>temporary</code></em> <code class="type">boolean</code></span>])</code></code>
- </td><td>
- (<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)
- </td><td>
- Creates a new physical replication slot named
- <em class="parameter"><code>slot_name</code></em>. The optional second parameter,
- when <code class="literal">true</code>, specifies that the <acronym class="acronym">LSN</acronym> for this
- replication slot be reserved immediately; otherwise
- the <acronym class="acronym">LSN</acronym> is reserved on first connection from a streaming
- replication client. Streaming changes from a physical slot is only
- possible with the streaming-replication protocol —
- see <a class="xref" href="protocol-replication.html" title="52.4. Streaming Replication Protocol">Section 52.4</a>. The optional third
- parameter, <em class="parameter"><code>temporary</code></em>, when set to true, specifies that
- the slot should not be permanently stored to disk and is only meant
- for use by current session. Temporary slots are also
- released upon any error. This function corresponds
- to the replication protocol command <code class="literal">CREATE_REPLICATION_SLOT
- ... PHYSICAL</code>.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.2.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_drop_replication_slot(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>)</code></code>
- </td><td>
- <code class="type">void</code>
- </td><td>
- Drops the physical or logical replication slot
- named <em class="parameter"><code>slot_name</code></em>. Same as replication protocol
- command <code class="literal">DROP_REPLICATION_SLOT</code>. For logical slots, this must
- be called when connected to the same database the slot was created on.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.3.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_create_logical_replication_slot(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>plugin</code></em> <code class="type">name</code> [<span class="optional">, <em class="parameter"><code>temporary</code></em> <code class="type">boolean</code></span>])</code></code>
- </td><td>
- (<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)
- </td><td>
- Creates a new logical (decoding) replication slot named
- <em class="parameter"><code>slot_name</code></em> using the output plugin
- <em class="parameter"><code>plugin</code></em>. The optional third
- parameter, <em class="parameter"><code>temporary</code></em>, when set to true, specifies that
- the slot should not be permanently stored to disk and is only meant
- for use by current session. Temporary slots are also
- released upon any error. A call to this function has the same
- effect as the replication protocol command
- <code class="literal">CREATE_REPLICATION_SLOT ... LOGICAL</code>.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.4.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_copy_physical_replication_slot(<em class="parameter"><code>src_slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>dst_slot_name</code></em> <code class="type">name</code> [<span class="optional">, <em class="parameter"><code>temporary</code></em> <code class="type">boolean</code></span>])</code></code>
- </td><td>
- (<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)
- </td><td>
- Copies an existing physical replication slot named <em class="parameter"><code>src_slot_name</code></em>
- to a physical replication slot named <em class="parameter"><code>dst_slot_name</code></em>.
- The copied physical slot starts to reserve WAL from the same <acronym class="acronym">LSN</acronym> as the
- source slot.
- <em class="parameter"><code>temporary</code></em> is optional. If <em class="parameter"><code>temporary</code></em>
- is omitted, the same value as the source slot is used.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.5.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_copy_logical_replication_slot(<em class="parameter"><code>src_slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>dst_slot_name</code></em> <code class="type">name</code> [<span class="optional">, <em class="parameter"><code>temporary</code></em> <code class="type">boolean</code> [<span class="optional">, <em class="parameter"><code>plugin</code></em> <code class="type">name</code></span>]</span>])</code></code>
- </td><td>
- (<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)
- </td><td>
- Copies an existing logical replication slot named <em class="parameter"><code>src_slot_name</code></em>
- to a logical replication slot named <em class="parameter"><code>dst_slot_name</code></em>
- while changing the output plugin and persistence. The copied logical slot starts
- from the same <acronym class="acronym">LSN</acronym> as the source logical slot. Both
- <em class="parameter"><code>temporary</code></em> and <em class="parameter"><code>plugin</code></em> are optional.
- If <em class="parameter"><code>temporary</code></em> or <em class="parameter"><code>plugin</code></em> are omitted,
- the same values as the source logical slot are used.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.6.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_logical_slot_get_changes(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>upto_lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>upto_nchanges</code></em> <code class="type">int</code>, VARIADIC <em class="parameter"><code>options</code></em> <code class="type">text[]</code>)</code></code>
- </td><td>
- (<em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>xid</code></em> <code class="type">xid</code>, <em class="parameter"><code>data</code></em> <code class="type">text</code>)
- </td><td>
- Returns changes in the slot <em class="parameter"><code>slot_name</code></em>, starting
- from the point at which since changes have been consumed last. If
- <em class="parameter"><code>upto_lsn</code></em> and <em class="parameter"><code>upto_nchanges</code></em> are NULL,
- logical decoding will continue until end of WAL. If
- <em class="parameter"><code>upto_lsn</code></em> is non-NULL, decoding will include only
- those transactions which commit prior to the specified LSN. If
- <em class="parameter"><code>upto_nchanges</code></em> is non-NULL, decoding will
- stop when the number of rows produced by decoding exceeds
- the specified value. Note, however, that the actual number of
- rows returned may be larger, since this limit is only checked after
- adding the rows produced when decoding each new transaction commit.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.7.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_logical_slot_peek_changes(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>upto_lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>upto_nchanges</code></em> <code class="type">int</code>, VARIADIC <em class="parameter"><code>options</code></em> <code class="type">text[]</code>)</code></code>
- </td><td>
- (<em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>xid</code></em> <code class="type">xid</code>, <em class="parameter"><code>data</code></em> <code class="type">text</code>)
- </td><td>
- Behaves just like
- the <code class="function">pg_logical_slot_get_changes()</code> function,
- except that changes are not consumed; that is, they will be returned
- again on future calls.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.8.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_logical_slot_get_binary_changes(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>upto_lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>upto_nchanges</code></em> <code class="type">int</code>, VARIADIC <em class="parameter"><code>options</code></em> <code class="type">text[]</code>)</code></code>
- </td><td>
- (<em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>xid</code></em> <code class="type">xid</code>, <em class="parameter"><code>data</code></em> <code class="type">bytea</code>)
- </td><td>
- Behaves just like
- the <code class="function">pg_logical_slot_get_changes()</code> function,
- except that changes are returned as <code class="type">bytea</code>.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.9.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_logical_slot_peek_binary_changes(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>upto_lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>upto_nchanges</code></em> <code class="type">int</code>, VARIADIC <em class="parameter"><code>options</code></em> <code class="type">text[]</code>)</code></code>
- </td><td>
- (<em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>xid</code></em> <code class="type">xid</code>, <em class="parameter"><code>data</code></em> <code class="type">bytea</code>)
- </td><td>
- Behaves just like
- the <code class="function">pg_logical_slot_get_changes()</code> function,
- except that changes are returned as <code class="type">bytea</code> and that
- changes are not consumed; that is, they will be returned again
- on future calls.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.10.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_slot_advance(<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>upto_lsn</code></em> <code class="type">pg_lsn</code>)</code></code>
- </td><td>
- (<em class="parameter"><code>slot_name</code></em> <code class="type">name</code>, <em class="parameter"><code>end_lsn</code></em> <code class="type">pg_lsn</code>)
- <code class="type">bool</code>
- </td><td>
- Advances the current confirmed position of a replication slot named
- <em class="parameter"><code>slot_name</code></em>. The slot will not be moved backwards,
- and it will not be moved beyond the current insert location. Returns
- the name of the slot and the real position to which it was advanced to.
- The information of the updated slot is written out at the follow-up
- checkpoint if any advancing is done. In the event of a crash, the
- slot may return to an earlier position.
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-CREATE">
- <a id="id-1.5.8.31.8.5.2.2.11.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_create(<em class="parameter"><code>node_name</code></em> <code class="type">text</code>)</code></code>
- </td><td>
- <code class="type">oid</code>
- </td><td>
- Create a replication origin with the given external
- name, and return the internal id assigned to it.
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-DROP">
- <a id="id-1.5.8.31.8.5.2.2.12.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_drop(<em class="parameter"><code>node_name</code></em> <code class="type">text</code>)</code></code>
- </td><td>
- <code class="type">void</code>
- </td><td>
- Delete a previously created replication origin, including any
- associated replay progress.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.13.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_oid(<em class="parameter"><code>node_name</code></em> <code class="type">text</code>)</code></code>
- </td><td>
- <code class="type">oid</code>
- </td><td>
- Lookup a replication origin by name and return the internal id. If no
- corresponding replication origin is found an error is thrown.
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-SESSION-SETUP">
- <a id="id-1.5.8.31.8.5.2.2.14.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_session_setup(<em class="parameter"><code>node_name</code></em> <code class="type">text</code>)</code></code>
- </td><td>
- <code class="type">void</code>
- </td><td>
- Mark the current session as replaying from the given
- origin, allowing replay progress to be tracked. Use
- <code class="function">pg_replication_origin_session_reset</code> to revert.
- Can only be used if no previous origin is configured.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.15.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_session_reset()</code></code>
- </td><td>
- <code class="type">void</code>
- </td><td>
- Cancel the effects
- of <code class="function">pg_replication_origin_session_setup()</code>.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.8.5.2.2.16.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_session_is_setup()</code></code>
- </td><td>
- <code class="type">bool</code>
- </td><td>
- Has a replication origin been configured in the current session?
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-SESSION-PROGRESS">
- <a id="id-1.5.8.31.8.5.2.2.17.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_session_progress(<em class="parameter"><code>flush</code></em> <code class="type">bool</code>)</code></code>
- </td><td>
- <code class="type">pg_lsn</code>
- </td><td>
- Return the replay location for the replication origin configured in
- the current session. The parameter <em class="parameter"><code>flush</code></em>
- determines whether the corresponding local transaction will be
- guaranteed to have been flushed to disk or not.
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-XACT-SETUP">
- <a id="id-1.5.8.31.8.5.2.2.18.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_xact_setup(<em class="parameter"><code>origin_lsn</code></em> <code class="type">pg_lsn</code>, <em class="parameter"><code>origin_timestamp</code></em> <code class="type">timestamptz</code>)</code></code>
- </td><td>
- <code class="type">void</code>
- </td><td>
- Mark the current transaction as replaying a transaction that has
- committed at the given <acronym class="acronym">LSN</acronym> and timestamp. Can
- only be called when a replication origin has previously been
- configured using
- <code class="function">pg_replication_origin_session_setup()</code>.
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-XACT-RESET">
- <a id="id-1.5.8.31.8.5.2.2.19.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_xact_reset()</code></code>
- </td><td>
- <code class="type">void</code>
- </td><td>
- Cancel the effects of
- <code class="function">pg_replication_origin_xact_setup()</code>.
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-ADVANCE">
- <a id="id-1.5.8.31.8.5.2.2.20.1.1" class="indexterm"></a>
- <code class="literal">pg_replication_origin_advance<code class="function">(<em class="parameter"><code>node_name</code></em> <code class="type">text</code>, <em class="parameter"><code>lsn</code></em> <code class="type">pg_lsn</code>)</code></code>
- </td><td>
- <code class="type">void</code>
- </td><td>
- Set replication progress for the given node to the given
- location. This primarily is useful for setting up the initial location
- or a new location after configuration changes and similar. Be aware
- that careless use of this function can lead to inconsistently
- replicated data.
- </td></tr><tr><td id="PG-REPLICATION-ORIGIN-PROGRESS">
- <a id="id-1.5.8.31.8.5.2.2.21.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_replication_origin_progress(<em class="parameter"><code>node_name</code></em> <code class="type">text</code>, <em class="parameter"><code>flush</code></em> <code class="type">bool</code>)</code></code>
- </td><td>
- <code class="type">pg_lsn</code>
- </td><td>
- Return the replay location for the given replication origin. The
- parameter <em class="parameter"><code>flush</code></em> determines whether the
- corresponding local transaction will be guaranteed to have been
- flushed to disk or not.
- </td></tr><tr><td id="PG-LOGICAL-EMIT-MESSAGE-TEXT">
- <a id="id-1.5.8.31.8.5.2.2.22.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_logical_emit_message(<em class="parameter"><code>transactional</code></em> <code class="type">bool</code>, <em class="parameter"><code>prefix</code></em> <code class="type">text</code>, <em class="parameter"><code>content</code></em> <code class="type">text</code>)</code></code>
- </td><td>
- <code class="type">pg_lsn</code>
- </td><td>
- Emit text logical decoding message. This can be used to pass generic
- messages to logical decoding plugins through WAL. The parameter
- <em class="parameter"><code>transactional</code></em> specifies if the message should
- be part of current transaction or if it should be written immediately
- and decoded as soon as the logical decoding reads the record. The
- <em class="parameter"><code>prefix</code></em> is textual prefix used by the logical
- decoding plugins to easily recognize interesting messages for them.
- The <em class="parameter"><code>content</code></em> is the text of the message.
- </td></tr><tr><td id="PG-LOGICAL-EMIT-MESSAGE-BYTEA">
- <code class="literal"><code class="function">pg_logical_emit_message(<em class="parameter"><code>transactional</code></em> <code class="type">bool</code>, <em class="parameter"><code>prefix</code></em> <code class="type">text</code>, <em class="parameter"><code>content</code></em> <code class="type">bytea</code>)</code></code>
- </td><td>
- <code class="type">pg_lsn</code>
- </td><td>
- Emit binary logical decoding message. This can be used to pass generic
- messages to logical decoding plugins through WAL. The parameter
- <em class="parameter"><code>transactional</code></em> specifies if the message should
- be part of current transaction or if it should be written immediately
- and decoded as soon as the logical decoding reads the record. The
- <em class="parameter"><code>prefix</code></em> is textual prefix used by the logical
- decoding plugins to easily recognize interesting messages for them.
- The <em class="parameter"><code>content</code></em> is the binary content of the
- message.
- </td></tr></tbody></table></div></div><br class="table-break" /></div><div class="sect2" id="FUNCTIONS-ADMIN-DBOBJECT"><div class="titlepage"><div><div><h3 class="title">9.26.7. Database Object Management Functions</h3></div></div></div><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-DBSIZE" title="Table 9.89. Database Object Size Functions">Table 9.89</a> calculate
- the disk space usage of database objects.
- </p><a id="id-1.5.8.31.9.3" class="indexterm"></a><a id="id-1.5.8.31.9.4" class="indexterm"></a><a id="id-1.5.8.31.9.5" class="indexterm"></a><a id="id-1.5.8.31.9.6" class="indexterm"></a><a id="id-1.5.8.31.9.7" class="indexterm"></a><a id="id-1.5.8.31.9.8" class="indexterm"></a><a id="id-1.5.8.31.9.9" class="indexterm"></a><a id="id-1.5.8.31.9.10" class="indexterm"></a><a id="id-1.5.8.31.9.11" class="indexterm"></a><div class="table" id="FUNCTIONS-ADMIN-DBSIZE"><p class="title"><strong>Table 9.89. Database Object Size Functions</strong></p><div class="table-contents"><table class="table" summary="Database Object Size Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal"><code class="function">pg_column_size(<code class="type">any</code>)</code></code></td><td><code class="type">int</code></td><td>Number of bytes used to store a particular value (possibly compressed)</td></tr><tr><td>
- <code class="literal"><code class="function">pg_database_size(<code class="type">oid</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>Disk space used by the database with the specified OID</td></tr><tr><td>
- <code class="literal"><code class="function">pg_database_size(<code class="type">name</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>Disk space used by the database with the specified name</td></tr><tr><td>
- <code class="literal"><code class="function">pg_indexes_size(<code class="type">regclass</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>
- Total disk space used by indexes attached to the specified table
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_relation_size(<em class="parameter"><code>relation</code></em> <code class="type">regclass</code>, <em class="parameter"><code>fork</code></em> <code class="type">text</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>
- Disk space used by the specified fork (<code class="literal">'main'</code>,
- <code class="literal">'fsm'</code>, <code class="literal">'vm'</code>, or <code class="literal">'init'</code>)
- of the specified table or index
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_relation_size(<em class="parameter"><code>relation</code></em> <code class="type">regclass</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>
- Shorthand for <code class="literal">pg_relation_size(..., 'main')</code>
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_size_bytes(<code class="type">text</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>
- Converts a size in human-readable format with size units into bytes
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_size_pretty(<code class="type">bigint</code>)</code></code>
- </td><td><code class="type">text</code></td><td>
- Converts a size in bytes expressed as a 64-bit integer into a
- human-readable format with size units
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_size_pretty(<code class="type">numeric</code>)</code></code>
- </td><td><code class="type">text</code></td><td>
- Converts a size in bytes expressed as a numeric value into a
- human-readable format with size units
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_table_size(<code class="type">regclass</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>
- Disk space used by the specified table, excluding indexes
- (but including TOAST, free space map, and visibility map)
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_tablespace_size(<code class="type">oid</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>Disk space used by the tablespace with the specified OID</td></tr><tr><td>
- <code class="literal"><code class="function">pg_tablespace_size(<code class="type">name</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>Disk space used by the tablespace with the specified name</td></tr><tr><td>
- <code class="literal"><code class="function">pg_total_relation_size(<code class="type">regclass</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>
- Total disk space used by the specified table,
- including all indexes and <acronym class="acronym">TOAST</acronym> data
- </td></tr></tbody></table></div></div><br class="table-break" /><p>
- <code class="function">pg_column_size</code> shows the space used to store any individual
- data value.
- </p><p>
- <code class="function">pg_total_relation_size</code> accepts the OID or name of a
- table or toast table, and returns the total on-disk space used for
- that table, including all associated indexes. This function is
- equivalent to <code class="function">pg_table_size</code>
- <code class="literal">+</code> <code class="function">pg_indexes_size</code>.
- </p><p>
- <code class="function">pg_table_size</code> accepts the OID or name of a table and
- returns the disk space needed for that table, exclusive of indexes.
- (TOAST space, free space map, and visibility map are included.)
- </p><p>
- <code class="function">pg_indexes_size</code> accepts the OID or name of a table and
- returns the total disk space used by all the indexes attached to that
- table.
- </p><p>
- <code class="function">pg_database_size</code> and <code class="function">pg_tablespace_size</code>
- accept the OID or name of a database or tablespace, and return the total
- disk space used therein. To use <code class="function">pg_database_size</code>,
- you must have <code class="literal">CONNECT</code> permission on the specified database
- (which is granted by default), or be a member of the <code class="literal">pg_read_all_stats</code>
- role. To use <code class="function">pg_tablespace_size</code>, you must have
- <code class="literal">CREATE</code> permission on the specified tablespace, or be a member
- of the <code class="literal">pg_read_all_stats</code> role unless it is the default tablespace for
- the current database.
- </p><p>
- <code class="function">pg_relation_size</code> accepts the OID or name of a table, index
- or toast table, and returns the on-disk size in bytes of one fork of
- that relation. (Note that for most purposes it is more convenient to
- use the higher-level functions <code class="function">pg_total_relation_size</code>
- or <code class="function">pg_table_size</code>, which sum the sizes of all forks.)
- With one argument, it returns the size of the main data fork of the
- relation. The second argument can be provided to specify which fork
- to examine:
- </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: disc; "><li class="listitem"><p>
- <code class="literal">'main'</code> returns the size of the main
- data fork of the relation.
- </p></li><li class="listitem"><p>
- <code class="literal">'fsm'</code> returns the size of the Free Space Map
- (see <a class="xref" href="storage-fsm.html" title="68.3. Free Space Map">Section 68.3</a>) associated with the relation.
- </p></li><li class="listitem"><p>
- <code class="literal">'vm'</code> returns the size of the Visibility Map
- (see <a class="xref" href="storage-vm.html" title="68.4. Visibility Map">Section 68.4</a>) associated with the relation.
- </p></li><li class="listitem"><p>
- <code class="literal">'init'</code> returns the size of the initialization
- fork, if any, associated with the relation.
- </p></li></ul></div><p>
- </p><p>
- <code class="function">pg_size_pretty</code> can be used to format the result of one of
- the other functions in a human-readable way, using bytes, kB, MB, GB or TB
- as appropriate.
- </p><p>
- <code class="function">pg_size_bytes</code> can be used to get the size in bytes from a
- string in human-readable format. The input may have units of bytes, kB,
- MB, GB or TB, and is parsed case-insensitively. If no units are specified,
- bytes are assumed.
- </p><div class="note"><h3 class="title">Note</h3><p>
- The units kB, MB, GB and TB used by the functions
- <code class="function">pg_size_pretty</code> and <code class="function">pg_size_bytes</code> are defined
- using powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is
- 1024<sup>2</sup> = 1048576 bytes, and so on.
- </p></div><p>
- The functions above that operate on tables or indexes accept a
- <code class="type">regclass</code> argument, which is simply the OID of the table or index
- in the <code class="structname">pg_class</code> system catalog. You do not have to look up
- the OID by hand, however, since the <code class="type">regclass</code> 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 ordinary <acronym class="acronym">SQL</acronym> names, the string
- will be converted to lower case unless it contains double quotes around
- the table name.
- </p><p>
- If an OID that does not represent an existing object is passed as
- argument to one of the above functions, NULL is returned.
- </p><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-DBLOCATION" title="Table 9.90. Database Object Location Functions">Table 9.90</a> assist
- in identifying the specific disk files associated with database objects.
- </p><a id="id-1.5.8.31.9.25" class="indexterm"></a><a id="id-1.5.8.31.9.26" class="indexterm"></a><a id="id-1.5.8.31.9.27" class="indexterm"></a><div class="table" id="FUNCTIONS-ADMIN-DBLOCATION"><p class="title"><strong>Table 9.90. Database Object Location Functions</strong></p><div class="table-contents"><table class="table" summary="Database Object Location Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_relation_filenode(<em class="parameter"><code>relation</code></em> <code class="type">regclass</code>)</code></code>
- </td><td><code class="type">oid</code></td><td>
- Filenode number of the specified relation
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_relation_filepath(<em class="parameter"><code>relation</code></em> <code class="type">regclass</code>)</code></code>
- </td><td><code class="type">text</code></td><td>
- File path name of the specified relation
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_filenode_relation(<em class="parameter"><code>tablespace</code></em> <code class="type">oid</code>, <em class="parameter"><code>filenode</code></em> <code class="type">oid</code>)</code></code>
- </td><td><code class="type">regclass</code></td><td>
- Find the relation associated with a given tablespace and filenode
- </td></tr></tbody></table></div></div><br class="table-break" /><p>
- <code class="function">pg_relation_filenode</code> accepts the OID or name of a table,
- index, sequence, or toast table, and returns the <span class="quote">“<span class="quote">filenode</span>”</span> number
- currently assigned to it. The filenode is the base component of the file
- name(s) used for the relation (see <a class="xref" href="storage-file-layout.html" title="68.1. Database File Layout">Section 68.1</a>
- for more information). For most tables the result is the same as
- <code class="structname">pg_class</code>.<code class="structfield">relfilenode</code>, but for certain
- system catalogs <code class="structfield">relfilenode</code> 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.
- </p><p>
- <code class="function">pg_relation_filepath</code> is similar to
- <code class="function">pg_relation_filenode</code>, but it returns the entire file path name
- (relative to the database cluster's data directory <code class="varname">PGDATA</code>) of
- the relation.
- </p><p>
- <code class="function">pg_filenode_relation</code> is the reverse of
- <code class="function">pg_relation_filenode</code>. Given a <span class="quote">“<span class="quote">tablespace</span>”</span> OID and
- a <span class="quote">“<span class="quote">filenode</span>”</span>, it returns the associated relation's OID. For a table
- in the database's default tablespace, the tablespace can be specified as 0.
- </p><p>
- <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-COLLATION" title="Table 9.91. Collation Management Functions">Table 9.91</a> lists functions used to manage
- collations.
- </p><div class="table" id="FUNCTIONS-ADMIN-COLLATION"><p class="title"><strong>Table 9.91. Collation Management Functions</strong></p><div class="table-contents"><table class="table" summary="Collation Management Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <a id="id-1.5.8.31.9.33.2.2.1.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_collation_actual_version(<code class="type">oid</code>)</code></code>
- </td><td><code class="type">text</code></td><td>Return actual version of collation from operating system</td></tr><tr><td>
- <a id="id-1.5.8.31.9.33.2.2.2.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_import_system_collations(<em class="parameter"><code>schema</code></em> <code class="type">regnamespace</code>)</code></code>
- </td><td><code class="type">integer</code></td><td>Import operating system collations</td></tr></tbody></table></div></div><br class="table-break" /><p>
- <code class="function">pg_collation_actual_version</code> 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 <code class="literal">pg_collation.collversion</code>, then objects depending on
- the collation might need to be rebuilt. See also
- <a class="xref" href="sql-altercollation.html" title="ALTER COLLATION"><span class="refentrytitle">ALTER COLLATION</span></a>.
- </p><p>
- <code class="function">pg_import_system_collations</code> adds collations to the system
- catalog <code class="literal">pg_collation</code> based on all the
- locales it finds in the operating system. This is
- what <code class="command">initdb</code> uses;
- see <a class="xref" href="collation.html#COLLATION-MANAGING" title="23.2.2. Managing Collations">Section 23.2.2</a> 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 in <code class="literal">pg_collation</code> will be skipped.
- (But collation objects based on locales that are no longer
- present in the operating system are not removed by this function.)
- The <em class="parameter"><code>schema</code></em> parameter would typically
- be <code class="literal">pg_catalog</code>, 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.
- </p><div class="table" id="FUNCTIONS-INFO-PARTITION"><p class="title"><strong>Table 9.92. Partitioning Information Functions</strong></p><div class="table-contents"><table class="table" summary="Partitioning Information Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <a id="id-1.5.8.31.9.36.2.2.1.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_partition_tree(<code class="type">regclass</code>)</code></code>
- </td><td><code class="type">setof record</code></td><td>
- List information about tables or indexes in a partition tree for a
- given partitioned table or partitioned index, with one row for each
- partition. Information provided includes the name of the partition,
- the name of its immediate parent, a boolean value telling if the
- partition is a leaf, and an integer telling its level in the hierarchy.
- The value of level begins at <code class="literal">0</code> for the input table
- or index in its role as the root of the partition tree,
- <code class="literal">1</code> for its partitions, <code class="literal">2</code> for
- their partitions, and so on.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.9.36.2.2.2.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_partition_ancestors(<code class="type">regclass</code>)</code></code>
- </td><td><code class="type">setof regclass</code></td><td>
- List the ancestor relations of the given partition,
- including the partition itself.
- </td></tr><tr><td>
- <a id="id-1.5.8.31.9.36.2.2.3.1.1" class="indexterm"></a>
- <code class="literal"><code class="function">pg_partition_root(<code class="type">regclass</code>)</code></code>
- </td><td><code class="type">regclass</code></td><td>
- Return the top-most parent of a partition tree to which the given
- relation belongs.
- </td></tr></tbody></table></div></div><br class="table-break" /><p>
- To check the total size of the data contained in
- <code class="structname">measurement</code> table described in
- <a class="xref" href="ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-EXAMPLE" title="5.11.2.1. Example">Section 5.11.2.1</a>, one could use the
- following query:
- </p><pre class="programlisting">
- =# SELECT pg_size_pretty(sum(pg_relation_size(relid))) AS total_size
- FROM pg_partition_tree('measurement');
- total_size
- ------------
- 24 kB
- (1 row)
- </pre></div><div class="sect2" id="FUNCTIONS-ADMIN-INDEX"><div class="titlepage"><div><div><h3 class="title">9.26.8. Index Maintenance Functions</h3></div></div></div><a id="id-1.5.8.31.10.2" class="indexterm"></a><a id="id-1.5.8.31.10.3" class="indexterm"></a><a id="id-1.5.8.31.10.4" class="indexterm"></a><a id="id-1.5.8.31.10.5" class="indexterm"></a><p>
- <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-INDEX-TABLE" title="Table 9.93. Index Maintenance Functions">Table 9.93</a> shows the functions
- available for index maintenance tasks.
- These functions cannot be executed during recovery.
- Use of these functions is restricted to superusers and the owner
- of the given index.
- </p><div class="table" id="FUNCTIONS-ADMIN-INDEX-TABLE"><p class="title"><strong>Table 9.93. Index Maintenance Functions</strong></p><div class="table-contents"><table class="table" summary="Index Maintenance Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">brin_summarize_new_values(<em class="parameter"><code>index</code></em> <code class="type">regclass</code>)</code></code>
- </td><td><code class="type">integer</code></td><td>summarize page ranges not already summarized</td></tr><tr><td>
- <code class="literal"><code class="function">brin_summarize_range(<em class="parameter"><code>index</code></em> <code class="type">regclass</code>, <em class="parameter"><code>blockNumber</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">integer</code></td><td>summarize the page range covering the given block, if not already summarized</td></tr><tr><td>
- <code class="literal"><code class="function">brin_desummarize_range(<em class="parameter"><code>index</code></em> <code class="type">regclass</code>, <em class="parameter"><code>blockNumber</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">integer</code></td><td>de-summarize the page range covering the given block, if summarized</td></tr><tr><td>
- <code class="literal"><code class="function">gin_clean_pending_list(<em class="parameter"><code>index</code></em> <code class="type">regclass</code>)</code></code>
- </td><td><code class="type">bigint</code></td><td>move GIN pending list entries into main index structure</td></tr></tbody></table></div></div><br class="table-break" /><p>
- <code class="function">brin_summarize_new_values</code> accepts the OID or name of a
- BRIN index and inspects the 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 the table pages.
- It returns the number of new page range summaries that were inserted
- into the index. <code class="function">brin_summarize_range</code> does the same, except
- it only summarizes the range that covers the given block number.
- </p><p>
- <code class="function">gin_clean_pending_list</code> accepts the OID or name of
- a GIN index and cleans up the pending list of the specified index
- by moving entries in it to the main GIN data structure in bulk.
- It returns the number of pages removed from the pending list.
- Note that if the argument is a GIN index built with
- the <code class="literal">fastupdate</code> option disabled, no cleanup happens and the
- return value is 0, because the index doesn't have a pending list.
- Please see <a class="xref" href="gin-implementation.html#GIN-FAST-UPDATE" title="66.4.1. GIN Fast Update Technique">Section 66.4.1</a> and <a class="xref" href="gin-tips.html" title="66.5. GIN Tips and Tricks">Section 66.5</a>
- for details of the pending list and <code class="literal">fastupdate</code> option.
- </p></div><div class="sect2" id="FUNCTIONS-ADMIN-GENFILE"><div class="titlepage"><div><div><h3 class="title">9.26.9. Generic File Access Functions</h3></div></div></div><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-ADMIN-GENFILE-TABLE" title="Table 9.94. Generic File Access Functions">Table 9.94</a> provide native access to
- files on the machine hosting the server. Only files within the
- database cluster directory and the <code class="varname">log_directory</code> can be
- accessed unless the user is granted the role
- <code class="literal">pg_read_server_files</code>. Use a relative path for files in
- the cluster directory, and a path matching the <code class="varname">log_directory</code>
- configuration setting for log files.
- </p><p>
- Note that granting users the EXECUTE privilege on
- <code class="function">pg_read_file()</code>, or related functions, allows them the
- ability to read any file on the server which the database can read and
- that those reads bypass all in-database privilege checks. This means that,
- among other things, a user with this access is able to read the contents of the
- <code class="literal">pg_authid</code> table where authentication information is contained,
- as well as read any file in the database. Therefore, granting access to these
- functions should be carefully considered.
- </p><div class="table" id="FUNCTIONS-ADMIN-GENFILE-TABLE"><p class="title"><strong>Table 9.94. Generic File Access Functions</strong></p><div class="table-contents"><table class="table" summary="Generic File Access Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_ls_dir(<em class="parameter"><code>dirname</code></em> <code class="type">text</code> [, <em class="parameter"><code>missing_ok</code></em> <code class="type">boolean</code>, <em class="parameter"><code>include_dot_dirs</code></em> <code class="type">boolean</code>])</code></code>
- </td><td><code class="type">setof text</code></td><td>
- List the contents of a directory. Restricted to superusers by default, but other users can be granted EXECUTE to run the function.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_ls_logdir()</code></code>
- </td><td><code class="type">setof record</code></td><td>
- List the name, size, and last modification time of files in the log
- directory. Access is granted to members of the <code class="literal">pg_monitor</code>
- role and may be granted to other non-superuser roles.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_ls_waldir()</code></code>
- </td><td><code class="type">setof record</code></td><td>
- List the name, size, and last modification time of files in the WAL
- directory. Access is granted to members of the <code class="literal">pg_monitor</code>
- role and may be granted to other non-superuser roles.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_ls_archive_statusdir()</code></code>
- </td><td><code class="type">setof record</code></td><td>
- List the name, size, and last modification time of files in the WAL
- archive status directory. Access is granted to members of the
- <code class="literal">pg_monitor</code> role and may be granted to other
- non-superuser roles.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_ls_tmpdir([<span class="optional"><em class="parameter"><code>tablespace</code></em> <code class="type">oid</code></span>])</code></code>
- </td><td><code class="type">setof record</code></td><td>
- List the name, size, and last modification time of files in the
- temporary directory for <em class="parameter"><code>tablespace</code></em>. If
- <em class="parameter"><code>tablespace</code></em> is not provided, the
- <code class="literal">pg_default</code> tablespace is used. Access is granted
- to members of the <code class="literal">pg_monitor</code> role and may be
- granted to other non-superuser roles.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_read_file(<em class="parameter"><code>filename</code></em> <code class="type">text</code> [, <em class="parameter"><code>offset</code></em> <code class="type">bigint</code>, <em class="parameter"><code>length</code></em> <code class="type">bigint</code> [, <em class="parameter"><code>missing_ok</code></em> <code class="type">boolean</code>] ])</code></code>
- </td><td><code class="type">text</code></td><td>
- Return the contents of a text file. Restricted to superusers by default, but other users can be granted EXECUTE to run the function.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_read_binary_file(<em class="parameter"><code>filename</code></em> <code class="type">text</code> [, <em class="parameter"><code>offset</code></em> <code class="type">bigint</code>, <em class="parameter"><code>length</code></em> <code class="type">bigint</code> [, <em class="parameter"><code>missing_ok</code></em> <code class="type">boolean</code>] ])</code></code>
- </td><td><code class="type">bytea</code></td><td>
- Return the contents of a file. Restricted to superusers by default, but other users can be granted EXECUTE to run the function.
- </td></tr><tr><td>
- <code class="literal"><code class="function">pg_stat_file(<em class="parameter"><code>filename</code></em> <code class="type">text</code>[, <em class="parameter"><code>missing_ok</code></em> <code class="type">boolean</code>])</code></code>
- </td><td><code class="type">record</code></td><td>
- Return information about a file. Restricted to superusers by default, but other users can be granted EXECUTE to run the function.
- </td></tr></tbody></table></div></div><br class="table-break" /><p>
- Some of these functions take an optional <em class="parameter"><code>missing_ok</code></em> parameter,
- which specifies the behavior when the file or directory does not exist.
- If <code class="literal">true</code>, the function returns NULL (except
- <code class="function">pg_ls_dir</code>, which returns an empty result set). If
- <code class="literal">false</code>, an error is raised. The default is <code class="literal">false</code>.
- </p><a id="id-1.5.8.31.11.6" class="indexterm"></a><p>
- <code class="function">pg_ls_dir</code> returns the names of all files (and directories
- and other special files) in the specified directory. The <em class="parameter"><code>
- include_dot_dirs</code></em> indicates whether <span class="quote">“<span class="quote">.</span>”</span> and <span class="quote">“<span class="quote">..</span>”</span> are
- included in the result set. The default is to exclude them
- (<code class="literal">false</code>), but including them can be useful when
- <em class="parameter"><code>missing_ok</code></em> is <code class="literal">true</code>, to distinguish an
- empty directory from an non-existent directory.
- </p><a id="id-1.5.8.31.11.8" class="indexterm"></a><p>
- <code class="function">pg_ls_logdir</code> returns the name, size, and last modified time
- (mtime) of each file in the log directory. By default, only superusers
- and members of the <code class="literal">pg_monitor</code> role can use this function.
- Access may be granted to others using <code class="command">GRANT</code>.
- Filenames beginning with a dot, directories, and other special files are not shown.
- </p><a id="id-1.5.8.31.11.10" class="indexterm"></a><p>
- <code class="function">pg_ls_waldir</code> returns the name, size, and last modified time
- (mtime) of each file in the write ahead log (WAL) directory. By
- default only superusers and members of the <code class="literal">pg_monitor</code> role
- can use this function. Access may be granted to others using
- <code class="command">GRANT</code>.
- Filenames beginning with a dot, directories, and other special files are not shown.
- </p><a id="id-1.5.8.31.11.12" class="indexterm"></a><p>
- <code class="function">pg_ls_archive_statusdir</code> returns the name, size, and
- last modified time (mtime) of each file in the WAL archive status
- directory <code class="filename">pg_wal/archive_status</code>. By default only
- superusers and members of the <code class="literal">pg_monitor</code> role can
- use this function. Access may be granted to others using
- <code class="command">GRANT</code>.
- Filenames beginning with a dot, directories, and other special files are not shown.
- </p><a id="id-1.5.8.31.11.14" class="indexterm"></a><p>
- <code class="function">pg_ls_tmpdir</code> returns the name, size, and last modified
- time (mtime) of each file in the temporary file directory for the specified
- <em class="parameter"><code>tablespace</code></em>. If <em class="parameter"><code>tablespace</code></em> is
- not provided, the <code class="literal">pg_default</code> tablespace is used. By
- default only superusers and members of the <code class="literal">pg_monitor</code>
- role can use this function. Access may be granted to others using
- <code class="command">GRANT</code>.
- Filenames beginning with a dot, directories, and other special files are not shown.
- </p><a id="id-1.5.8.31.11.16" class="indexterm"></a><p>
- <code class="function">pg_read_file</code> returns part of a text file, starting
- at the given <em class="parameter"><code>offset</code></em>, returning at most <em class="parameter"><code>length</code></em>
- bytes (less if the end of file is reached first). If <em class="parameter"><code>offset</code></em>
- is negative, it is relative to the end of the file.
- If <em class="parameter"><code>offset</code></em> and <em class="parameter"><code>length</code></em> are omitted, the entire
- file is returned. The bytes read from the file are interpreted as a string
- in the server encoding; an error is thrown if they are not valid in that
- encoding.
- </p><a id="id-1.5.8.31.11.18" class="indexterm"></a><p>
- <code class="function">pg_read_binary_file</code> is similar to
- <code class="function">pg_read_file</code>, except that the result is a <code class="type">bytea</code> value;
- accordingly, no encoding checks are performed.
- In combination with the <code class="function">convert_from</code> function, this function
- can be used to read a file in a specified encoding:
- </p><pre class="programlisting">
- SELECT convert_from(pg_read_binary_file('file_in_utf8.txt'), 'UTF8');
- </pre><p>
- </p><a id="id-1.5.8.31.11.20" class="indexterm"></a><p>
- <code class="function">pg_stat_file</code> returns a record containing the file
- size, last accessed time stamp, last modified time stamp,
- last file status change time stamp (Unix platforms only),
- file creation time stamp (Windows only), and a <code class="type">boolean</code>
- indicating if it is a directory. Typical usages include:
- </p><pre class="programlisting">
- SELECT * FROM pg_stat_file('filename');
- SELECT (pg_stat_file('filename')).modification;
- </pre><p>
- </p></div><div class="sect2" id="FUNCTIONS-ADVISORY-LOCKS"><div class="titlepage"><div><div><h3 class="title">9.26.10. Advisory Lock Functions</h3></div></div></div><p>
- The functions shown in <a class="xref" href="functions-admin.html#FUNCTIONS-ADVISORY-LOCKS-TABLE" title="Table 9.95. Advisory Lock Functions">Table 9.95</a>
- manage advisory locks. For details about proper use of these functions,
- see <a class="xref" href="explicit-locking.html#ADVISORY-LOCKS" title="13.3.5. Advisory Locks">Section 13.3.5</a>.
- </p><div class="table" id="FUNCTIONS-ADVISORY-LOCKS-TABLE"><p class="title"><strong>Table 9.95. Advisory Lock Functions</strong></p><div class="table-contents"><table class="table" summary="Advisory Lock Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
- <code class="literal"><code class="function">pg_advisory_lock(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain exclusive session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_lock(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain exclusive session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_lock_shared(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain shared session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_lock_shared(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain shared session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_unlock(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Release an exclusive session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_unlock(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Release an exclusive session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_unlock_all()</code></code>
- </td><td><code class="type">void</code></td><td>Release all session level advisory locks held by the current session</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_unlock_shared(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Release a shared session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_unlock_shared(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Release a shared session level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_xact_lock(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain exclusive transaction level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_xact_lock(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain exclusive transaction level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_xact_lock_shared(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain shared transaction level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_advisory_xact_lock_shared(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">void</code></td><td>Obtain shared transaction level advisory lock</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_lock(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain exclusive session level advisory lock if available</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_lock(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain exclusive session level advisory lock if available</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_lock_shared(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain shared session level advisory lock if available</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_lock_shared(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain shared session level advisory lock if available</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_xact_lock(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain exclusive transaction level advisory lock if available</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_xact_lock(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain exclusive transaction level advisory lock if available</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_xact_lock_shared(<em class="parameter"><code>key</code></em> <code class="type">bigint</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain shared transaction level advisory lock if available</td></tr><tr><td>
- <code class="literal"><code class="function">pg_try_advisory_xact_lock_shared(<em class="parameter"><code>key1</code></em> <code class="type">int</code>, <em class="parameter"><code>key2</code></em> <code class="type">int</code>)</code></code>
- </td><td><code class="type">boolean</code></td><td>Obtain shared transaction level advisory lock if available</td></tr></tbody></table></div></div><br class="table-break" /><a id="id-1.5.8.31.12.4" class="indexterm"></a><p>
- <code class="function">pg_advisory_lock</code> locks an application-defined resource,
- 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 lock on the same resource identifier,
- this 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 then be unlocked three times to be
- released for other sessions' use.
- </p><a id="id-1.5.8.31.12.6" class="indexterm"></a><p>
- <code class="function">pg_advisory_lock_shared</code> works the same as
- <code class="function">pg_advisory_lock</code>,
- except the lock can be shared with other sessions requesting shared locks.
- Only would-be exclusive lockers are locked out.
- </p><a id="id-1.5.8.31.12.8" class="indexterm"></a><p>
- <code class="function">pg_try_advisory_lock</code> is similar to
- <code class="function">pg_advisory_lock</code>, except the function will not wait for the
- lock to become available. It will either obtain the lock immediately and
- return <code class="literal">true</code>, or return <code class="literal">false</code> if the lock cannot be
- acquired immediately.
- </p><a id="id-1.5.8.31.12.10" class="indexterm"></a><p>
- <code class="function">pg_try_advisory_lock_shared</code> works the same as
- <code class="function">pg_try_advisory_lock</code>, except it attempts to acquire
- a shared rather than an exclusive lock.
- </p><a id="id-1.5.8.31.12.12" class="indexterm"></a><p>
- <code class="function">pg_advisory_unlock</code> will release a previously-acquired
- exclusive session level advisory lock. It
- returns <code class="literal">true</code> if the lock is successfully released.
- If the lock was not held, it will return <code class="literal">false</code>,
- and in addition, an SQL warning will be reported by the server.
- </p><a id="id-1.5.8.31.12.14" class="indexterm"></a><p>
- <code class="function">pg_advisory_unlock_shared</code> works the same as
- <code class="function">pg_advisory_unlock</code>,
- except it releases a shared session level advisory lock.
- </p><a id="id-1.5.8.31.12.16" class="indexterm"></a><p>
- <code class="function">pg_advisory_unlock_all</code> will release all session level advisory
- locks held by the current session. (This function is implicitly invoked
- at session end, even if the client disconnects ungracefully.)
- </p><a id="id-1.5.8.31.12.18" class="indexterm"></a><p>
- <code class="function">pg_advisory_xact_lock</code> works the same as
- <code class="function">pg_advisory_lock</code>, except the lock is automatically released
- at the end of the current transaction and cannot be released explicitly.
- </p><a id="id-1.5.8.31.12.20" class="indexterm"></a><p>
- <code class="function">pg_advisory_xact_lock_shared</code> works the same as
- <code class="function">pg_advisory_lock_shared</code>, except the lock is automatically released
- at the end of the current transaction and cannot be released explicitly.
- </p><a id="id-1.5.8.31.12.22" class="indexterm"></a><p>
- <code class="function">pg_try_advisory_xact_lock</code> works the same as
- <code class="function">pg_try_advisory_lock</code>, except the lock, if acquired,
- is automatically released at the end of the current transaction and
- cannot be released explicitly.
- </p><a id="id-1.5.8.31.12.24" class="indexterm"></a><p>
- <code class="function">pg_try_advisory_xact_lock_shared</code> works the same as
- <code class="function">pg_try_advisory_lock_shared</code>, except the lock, if acquired,
- is automatically released at the end of the current transaction and
- cannot be released explicitly.
- </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-info.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-trigger.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.25. System Information Functions and Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.27. Trigger Functions</td></tr></table></div></body></html>
|