1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-30 06:01:21 +03:00

Introduce replication progress tracking infrastructure.

When implementing a replication solution ontop of logical decoding, two
related problems exist:
* How to safely keep track of replication progress
* How to change replication behavior, based on the origin of a row;
  e.g. to avoid loops in bi-directional replication setups

The solution to these problems, as implemented here, consist out of
three parts:

1) 'replication origins', which identify nodes in a replication setup.
2) 'replication progress tracking', which remembers, for each
   replication origin, how far replay has progressed in a efficient and
   crash safe manner.
3) The ability to filter out changes performed on the behest of a
   replication origin during logical decoding; this allows complex
   replication topologies. E.g. by filtering all replayed changes out.

Most of this could also be implemented in "userspace", e.g. by inserting
additional rows contain origin information, but that ends up being much
less efficient and more complicated.  We don't want to require various
replication solutions to reimplement logic for this independently. The
infrastructure is intended to be generic enough to be reusable.

This infrastructure also replaces the 'nodeid' infrastructure of commit
timestamps. It is intended to provide all the former capabilities,
except that there's only 2^16 different origins; but now they integrate
with logical decoding. Additionally more functionality is accessible via
SQL.  Since the commit timestamp infrastructure has also been introduced
in 9.5 (commit 73c986add) changing the API is not a problem.

For now the number of origins for which the replication progress can be
tracked simultaneously is determined by the max_replication_slots
GUC. That GUC is not a perfect match to configure this, but there
doesn't seem to be sufficient reason to introduce a separate new one.

Bumps both catversion and wal page magic.

Author: Andres Freund, with contributions from Petr Jelinek and Craig Ringer
Reviewed-By: Heikki Linnakangas, Petr Jelinek, Robert Haas, Steve Singer
Discussion: 20150216002155.GI15326@awork2.anarazel.de,
    20140923182422.GA15776@alap3.anarazel.de,
    20131114172632.GE7522@alap2.anarazel.de
This commit is contained in:
Andres Freund
2015-04-29 19:30:53 +02:00
parent c6e96a2f98
commit 5aa2350426
52 changed files with 2766 additions and 89 deletions

View File

@@ -16879,11 +16879,13 @@ postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
<title>Replication Functions</title>
<para>
The functions shown in <xref linkend="functions-replication-table"> are
for controlling and interacting with replication features.
See <xref linkend="streaming-replication">
and <xref linkend="streaming-replication-slots"> for information about the
underlying features. Use of these functions is restricted to superusers.
The functions shown
in <xref linkend="functions-replication-table"> are for
controlling and interacting with replication features.
See <xref linkend="streaming-replication">,
<xref linkend="streaming-replication-slots">, <xref linkend="replication-origins">
for information about the underlying features. Use of these
functions is restricted to superusers.
</para>
<para>
@@ -17040,6 +17042,195 @@ postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
on future calls.
</entry>
</row>
<row id="pg-replication-origin-create">
<entry>
<indexterm>
<primary>pg_replication_origin_create</primary>
</indexterm>
<literal><function>pg_replication_origin_create(<parameter>node_name</parameter> <type>text</type>)</function></literal>
</entry>
<entry>
<parameter>internal_id</parameter> <type>oid</type>
</entry>
<entry>
Create a replication origin with the the passed in external
name, and create an internal id for it.
</entry>
</row>
<row id="pg-replication-origin-drop">
<entry>
<indexterm>
<primary>pg_replication_origin_drop</primary>
</indexterm>
<literal><function>pg_replication_origin_drop(<parameter>node_name</parameter> <type>text</type>)</function></literal>
</entry>
<entry>
void
</entry>
<entry>
Delete a previously created replication origin, including the
associated replay progress.
</entry>
</row>
<row>
<entry>
<indexterm>
<primary>pg_replication_origin_oid</primary>
</indexterm>
<literal><function>pg_replication_origin_oid(<parameter>node_name</parameter> <type>text</type>)</function></literal>
</entry>
<entry>
<parameter>internal_id</parameter> <type>oid</type>
</entry>
<entry>
Lookup replication origin by name and return the internal
oid. If no corresponding replication origin is found a error
is thrown.
</entry>
</row>
<row id="pg-replication-origin-session-setup">
<entry>
<indexterm>
<primary>pg_replication_origin_session_setup</primary>
</indexterm>
<literal><function>pg_replication_origin_setup_session(<parameter>node_name</parameter> <type>text</type>)</function></literal>
</entry>
<entry>
void
</entry>
<entry>
Configure the current session to be replaying from the passed in
origin, allowing replay progress to be tracked. Use
<function>pg_replication_origin_session_reset</function> to revert.
Can only be used if no previous origin is configured.
</entry>
</row>
<row>
<entry>
<indexterm>
<primary>pg_replication_origin_session_reset</primary>
</indexterm>
<literal><function>pg_replication_origin_session_reset()</function></literal>
</entry>
<entry>
void
</entry>
<entry>
Cancel the effects
of <function>pg_replication_origin_session_setup()</function>.
</entry>
</row>
<row>
<entry>
<indexterm>
<primary>pg_replication_session_is_setup</primary>
</indexterm>
<literal><function>pg_replication_session_is_setup()</function></literal>
</entry>
<entry>
bool
</entry>
<entry>
Has a replication origin been configured in the current session?
</entry>
</row>
<row id="pg-replication-origin-session-progress">
<entry>
<indexterm>
<primary>pg_replication_origin_session_progress</primary>
</indexterm>
<literal><function>pg_replication_origin_progress(<parameter>flush</parameter> <type>bool</type>)</function></literal>
</entry>
<entry>
pg_lsn
</entry>
<entry>
Return the replay position for the replication origin configured in
the current session. The parameter <parameter>flush</parameter>
determines whether the corresponding local transaction will be
guaranteed to have been flushed to disk or not.
</entry>
</row>
<row id="pg-replication-origin-xact-setup">
<entry>
<indexterm>
<primary>pg_replication_origin_xact_setup</primary>
</indexterm>
<literal><function>pg_replication_origin_xact_setup(<parameter>origin_lsn</parameter> <type>pg_lsn</type>, <parameter>origin_timestamp</parameter> <type>timestamptz</type>)</function></literal>
</entry>
<entry>
void
</entry>
<entry>
Mark the current transaction to be replaying a transaction that has
committed at the passed in <acronym>LSN</acronym> and timestamp. Can
only be called when a replication origin has previously been
configured using
<function>pg_replication_origin_session_setup()</function>.
</entry>
</row>
<row id="pg-replication-origin-xact-reset">
<entry>
<indexterm>
<primary>pg_replication_origin_xact_reset</primary>
</indexterm>
<literal><function>pg_replication_origin_xact_reset()</function></literal>
</entry>
<entry>
void
</entry>
<entry>
Cancel the effects of
<function>pg_replication_origin_xact_setup()</function>.
</entry>
</row>
<row>
<entry>
<indexterm>
<primary>pg_replication_origin_advance</primary>
</indexterm>
<literal>pg_replication_origin_advance<function>(<parameter>node_name</parameter> <type>text</type>, <parameter>pos</parameter> <type>pg_lsn</type>)</function></literal>
</entry>
<entry>
void
</entry>
<entry>
Set replication progress for the passed in node to the passed in
position. This primarily is useful for setting up the initial position
or a new position after configuration changes and similar. Be aware
that careless use of this function can lead to inconsistently
replicated data.
</entry>
</row>
<row id="pg-replication-origin-progress">
<entry>
<indexterm>
<primary>pg_replication_origin_progress</primary>
</indexterm>
<literal><function>pg_replication_origin_progress(<parameter>node_name</parameter> <type>text</type>, <parameter>flush</parameter> <type>bool</type>)</function></literal>
</entry>
<entry>
pg_lsn
</entry>
<entry>
Return the replay position for the passed in replication origin. The
parameter <parameter>flush</parameter> determines whether the
corresponding local transaction will be guaranteed to have been
flushed to disk or not.
</entry>
</row>
</tbody>
</tgroup>
</table>