2014-03-18 18:20:01 +01:00
|
|
|
<!-- doc/src/sgml/logicaldecoding.sgml -->
|
|
|
|
<chapter id="logicaldecoding">
|
|
|
|
<title>Logical Decoding</title>
|
|
|
|
<indexterm zone="logicaldecoding">
|
|
|
|
<primary>Logical Decoding</primary>
|
|
|
|
</indexterm>
|
|
|
|
<para>
|
|
|
|
PostgreSQL provides infrastructure to stream the modifications performed
|
2016-04-25 02:44:22 +02:00
|
|
|
via SQL to external consumers. This functionality can be used for a
|
2014-03-18 18:20:01 +01:00
|
|
|
variety of purposes, including replication solutions and auditing.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Changes are sent out in streams identified by logical replication slots.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The format in which those changes are streamed is determined by the output
|
2014-10-31 03:52:21 +01:00
|
|
|
plugin used. An example plugin is provided in the PostgreSQL distribution.
|
|
|
|
Additional plugins can be
|
2014-03-18 18:20:01 +01:00
|
|
|
written to extend the choice of available formats without modifying any
|
|
|
|
core code.
|
|
|
|
Every output plugin has access to each individual new row produced
|
|
|
|
by <command>INSERT</command> and the new row version created
|
|
|
|
by <command>UPDATE</command>. Availability of old row versions for
|
2014-03-19 14:13:05 +01:00
|
|
|
<command>UPDATE</command> and <command>DELETE</command> depends on
|
2020-09-03 12:38:32 +02:00
|
|
|
the configured replica identity (see <xref linkend="sql-altertable-replica-identity"/>).
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Changes can be consumed either using the streaming replication protocol
|
2017-11-23 15:39:47 +01:00
|
|
|
(see <xref linkend="protocol-replication"/> and
|
|
|
|
<xref linkend="logicaldecoding-walsender"/>), or by calling functions
|
|
|
|
via SQL (see <xref linkend="logicaldecoding-sql"/>). It is also possible
|
2014-03-18 18:20:01 +01:00
|
|
|
to write additional methods of consuming the output of a replication slot
|
|
|
|
without modifying core code
|
2017-11-23 15:39:47 +01:00
|
|
|
(see <xref linkend="logicaldecoding-writer"/>).
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect1 id="logicaldecoding-example">
|
2014-10-31 03:52:21 +01:00
|
|
|
<title>Logical Decoding Examples</title>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2014-10-31 03:52:21 +01:00
|
|
|
The following example demonstrates controlling logical decoding using the
|
|
|
|
SQL interface.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
Before you can use logical decoding, you must set
|
2017-11-23 15:39:47 +01:00
|
|
|
<xref linkend="guc-wal-level"/> to <literal>logical</literal> and
|
|
|
|
<xref linkend="guc-max-replication-slots"/> to at least 1. Then, you
|
2014-10-31 03:52:21 +01:00
|
|
|
should connect to the target database (in the example
|
2014-03-18 18:20:01 +01:00
|
|
|
below, <literal>postgres</literal>) as a superuser.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
postgres=# -- Create a slot named 'regression_slot' using the output plugin 'test_decoding'
|
2021-03-03 02:58:43 +01:00
|
|
|
postgres=# SELECT * FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding', false, true);
|
2017-05-11 17:49:59 +02:00
|
|
|
slot_name | lsn
|
|
|
|
-----------------+-----------
|
2014-03-18 18:20:01 +01:00
|
|
|
regression_slot | 0/16B1970
|
|
|
|
(1 row)
|
|
|
|
|
2015-08-10 13:28:18 +02:00
|
|
|
postgres=# SELECT slot_name, plugin, slot_type, database, active, restart_lsn, confirmed_flush_lsn FROM pg_replication_slots;
|
|
|
|
slot_name | plugin | slot_type | database | active | restart_lsn | confirmed_flush_lsn
|
|
|
|
-----------------+---------------+-----------+----------+--------+-------------+-----------------
|
|
|
|
regression_slot | test_decoding | logical | postgres | f | 0/16A4408 | 0/16A4440
|
2014-03-18 18:20:01 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
postgres=# -- There are no changes to see yet
|
|
|
|
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2017-05-11 17:49:59 +02:00
|
|
|
-----+-----+------
|
2014-03-18 18:20:01 +01:00
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
postgres=# CREATE TABLE data(id serial primary key, data text);
|
|
|
|
CREATE TABLE
|
|
|
|
|
|
|
|
postgres=# -- DDL isn't replicated, so all you'll see is the transaction
|
|
|
|
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2017-05-11 17:49:59 +02:00
|
|
|
-----------+-------+--------------
|
|
|
|
0/BA2DA58 | 10297 | BEGIN 10297
|
|
|
|
0/BA5A5A0 | 10297 | COMMIT 10297
|
2014-03-18 18:20:01 +01:00
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
postgres=# -- Once changes are read, they're consumed and not emitted
|
|
|
|
postgres=# -- in a subsequent call:
|
|
|
|
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2017-05-11 17:49:59 +02:00
|
|
|
-----+-----+------
|
2014-03-18 18:20:01 +01:00
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
postgres=# BEGIN;
|
2020-02-12 05:31:14 +01:00
|
|
|
postgres=*# INSERT INTO data(data) VALUES('1');
|
|
|
|
postgres=*# INSERT INTO data(data) VALUES('2');
|
|
|
|
postgres=*# COMMIT;
|
2014-03-18 18:20:01 +01:00
|
|
|
|
|
|
|
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2017-05-11 17:49:59 +02:00
|
|
|
-----------+-------+---------------------------------------------------------
|
|
|
|
0/BA5A688 | 10298 | BEGIN 10298
|
|
|
|
0/BA5A6F0 | 10298 | table public.data: INSERT: id[integer]:1 data[text]:'1'
|
|
|
|
0/BA5A7F8 | 10298 | table public.data: INSERT: id[integer]:2 data[text]:'2'
|
|
|
|
0/BA5A8A8 | 10298 | COMMIT 10298
|
2014-03-18 18:20:01 +01:00
|
|
|
(4 rows)
|
|
|
|
|
|
|
|
postgres=# INSERT INTO data(data) VALUES('3');
|
|
|
|
|
|
|
|
postgres=# -- You can also peek ahead in the change stream without consuming changes
|
|
|
|
postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2017-05-11 17:49:59 +02:00
|
|
|
-----------+-------+---------------------------------------------------------
|
|
|
|
0/BA5A8E0 | 10299 | BEGIN 10299
|
|
|
|
0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
|
|
|
|
0/BA5A990 | 10299 | COMMIT 10299
|
2014-03-18 18:20:01 +01:00
|
|
|
(3 rows)
|
|
|
|
|
2014-07-29 05:07:54 +02:00
|
|
|
postgres=# -- The next call to pg_logical_slot_peek_changes() returns the same changes again
|
2014-03-18 18:20:01 +01:00
|
|
|
postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2017-05-11 17:49:59 +02:00
|
|
|
-----------+-------+---------------------------------------------------------
|
|
|
|
0/BA5A8E0 | 10299 | BEGIN 10299
|
|
|
|
0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
|
|
|
|
0/BA5A990 | 10299 | COMMIT 10299
|
2014-03-18 18:20:01 +01:00
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
postgres=# -- options can be passed to output plugin, to influence the formatting
|
|
|
|
postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'include-timestamp', 'on');
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2017-05-11 17:49:59 +02:00
|
|
|
-----------+-------+---------------------------------------------------------
|
|
|
|
0/BA5A8E0 | 10299 | BEGIN 10299
|
|
|
|
0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
|
|
|
|
0/BA5A990 | 10299 | COMMIT 10299 (at 2017-05-10 12:07:21.272494-04)
|
2014-03-18 18:20:01 +01:00
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
postgres=# -- Remember to destroy a slot you no longer need to stop it consuming
|
|
|
|
postgres=# -- server resources:
|
|
|
|
postgres=# SELECT pg_drop_replication_slot('regression_slot');
|
|
|
|
pg_drop_replication_slot
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
(1 row)
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2021-06-30 05:15:47 +02:00
|
|
|
The following examples shows how logical decoding is controlled over the
|
2014-10-31 03:52:21 +01:00
|
|
|
streaming replication protocol, using the
|
2017-11-23 15:39:47 +01:00
|
|
|
program <xref linkend="app-pgrecvlogical"/> included in the PostgreSQL
|
2014-10-31 03:52:21 +01:00
|
|
|
distribution. This requires that client authentication is set up to allow
|
|
|
|
replication connections
|
2017-11-23 15:39:47 +01:00
|
|
|
(see <xref linkend="streaming-replication-authentication"/>) and
|
2016-03-08 03:52:30 +01:00
|
|
|
that <varname>max_wal_senders</varname> is set sufficiently high to allow
|
2021-06-30 05:15:47 +02:00
|
|
|
an additional connection. The second example shows how to stream two-phase
|
|
|
|
transactions. Before you use two-phase commands, you must set
|
2021-08-18 19:44:57 +02:00
|
|
|
<xref linkend="guc-max-prepared-transactions"/> to at least 1.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2021-06-30 05:15:47 +02:00
|
|
|
Example 1:
|
2018-05-21 20:54:24 +02:00
|
|
|
$ pg_recvlogical -d postgres --slot=test --create-slot
|
|
|
|
$ pg_recvlogical -d postgres --slot=test --start -f -
|
2017-10-09 03:44:17 +02:00
|
|
|
<keycombo action="simul"><keycap>Control</keycap><keycap>Z</keycap></keycombo>
|
2014-10-31 03:52:21 +01:00
|
|
|
$ psql -d postgres -c "INSERT INTO data(data) VALUES('4');"
|
|
|
|
$ fg
|
2014-03-18 18:20:01 +01:00
|
|
|
BEGIN 693
|
2014-03-18 19:57:49 +01:00
|
|
|
table public.data: INSERT: id[integer]:4 data[text]:'4'
|
2014-03-18 18:20:01 +01:00
|
|
|
COMMIT 693
|
2017-10-09 03:44:17 +02:00
|
|
|
<keycombo action="simul"><keycap>Control</keycap><keycap>C</keycap></keycombo>
|
2018-05-21 20:54:24 +02:00
|
|
|
$ pg_recvlogical -d postgres --slot=test --drop-slot
|
2021-06-30 05:15:47 +02:00
|
|
|
|
|
|
|
Example 2:
|
|
|
|
$ pg_recvlogical -d postgres --slot=test --create-slot --two-phase
|
|
|
|
$ pg_recvlogical -d postgres --slot=test --start -f -
|
|
|
|
<keycombo action="simul"><keycap>Control</keycap><keycap>Z</keycap></keycombo>
|
|
|
|
$ psql -d postgres -c "BEGIN;INSERT INTO data(data) VALUES('5');PREPARE TRANSACTION 'test';"
|
|
|
|
$ fg
|
|
|
|
BEGIN 694
|
|
|
|
table public.data: INSERT: id[integer]:5 data[text]:'5'
|
|
|
|
PREPARE TRANSACTION 'test', txid 694
|
|
|
|
<keycombo action="simul"><keycap>Control</keycap><keycap>Z</keycap></keycombo>
|
|
|
|
$ psql -d postgres -c "COMMIT PREPARED 'test';"
|
|
|
|
$ fg
|
|
|
|
COMMIT PREPARED 'test', txid 694
|
|
|
|
<keycombo action="simul"><keycap>Control</keycap><keycap>C</keycap></keycombo>
|
|
|
|
$ pg_recvlogical -d postgres --slot=test --drop-slot
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2021-01-04 04:04:50 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
The following example shows SQL interface that can be used to decode prepared
|
|
|
|
transactions. Before you use two-phase commit commands, you must set
|
2021-03-03 02:58:43 +01:00
|
|
|
<varname>max_prepared_transactions</varname> to at least 1. You must also have
|
|
|
|
set the two-phase parameter as 'true' while creating the slot using
|
|
|
|
<function>pg_create_logical_replication_slot</function>
|
|
|
|
Note that we will stream the entire transaction after the commit if it
|
|
|
|
is not already decoded.
|
2021-01-04 04:04:50 +01:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
postgres=# BEGIN;
|
|
|
|
postgres=*# INSERT INTO data(data) VALUES('5');
|
|
|
|
postgres=*# PREPARE TRANSACTION 'test_prepared1';
|
|
|
|
|
2021-03-03 02:58:43 +01:00
|
|
|
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2021-01-04 04:04:50 +01:00
|
|
|
-----------+-----+---------------------------------------------------------
|
|
|
|
0/1689DC0 | 529 | BEGIN 529
|
|
|
|
0/1689DC0 | 529 | table public.data: INSERT: id[integer]:3 data[text]:'5'
|
|
|
|
0/1689FC0 | 529 | PREPARE TRANSACTION 'test_prepared1', txid 529
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
postgres=# COMMIT PREPARED 'test_prepared1';
|
2021-03-03 02:58:43 +01:00
|
|
|
postgres=# select * from pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2021-01-04 04:04:50 +01:00
|
|
|
-----------+-----+--------------------------------------------
|
|
|
|
0/168A060 | 529 | COMMIT PREPARED 'test_prepared1', txid 529
|
|
|
|
(4 row)
|
|
|
|
|
|
|
|
postgres=#-- you can also rollback a prepared transaction
|
|
|
|
postgres=# BEGIN;
|
|
|
|
postgres=*# INSERT INTO data(data) VALUES('6');
|
|
|
|
postgres=*# PREPARE TRANSACTION 'test_prepared2';
|
2021-03-03 02:58:43 +01:00
|
|
|
postgres=# select * from pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2021-01-04 04:04:50 +01:00
|
|
|
-----------+-----+---------------------------------------------------------
|
|
|
|
0/168A180 | 530 | BEGIN 530
|
|
|
|
0/168A1E8 | 530 | table public.data: INSERT: id[integer]:4 data[text]:'6'
|
|
|
|
0/168A430 | 530 | PREPARE TRANSACTION 'test_prepared2', txid 530
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
postgres=# ROLLBACK PREPARED 'test_prepared2';
|
2021-03-03 02:58:43 +01:00
|
|
|
postgres=# select * from pg_logical_slot_get_changes('regression_slot', NULL, NULL);
|
2022-04-20 17:04:28 +02:00
|
|
|
lsn | xid | data
|
2021-01-04 04:04:50 +01:00
|
|
|
-----------+-----+----------------------------------------------
|
|
|
|
0/168A4B8 | 530 | ROLLBACK PREPARED 'test_prepared2', txid 530
|
|
|
|
(1 row)
|
|
|
|
</programlisting>
|
|
|
|
</sect1>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect1 id="logicaldecoding-explanation">
|
|
|
|
<title>Logical Decoding Concepts</title>
|
2023-01-09 21:08:24 +01:00
|
|
|
<sect2 id="logicaldecoding-explanation-log-dec">
|
2014-05-07 03:28:58 +02:00
|
|
|
<title>Logical Decoding</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<indexterm>
|
|
|
|
<primary>Logical Decoding</primary>
|
|
|
|
</indexterm>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2014-03-24 07:42:13 +01:00
|
|
|
Logical decoding is the process of extracting all persistent changes
|
2014-03-18 18:20:01 +01:00
|
|
|
to a database's tables into a coherent, easy to understand format which
|
|
|
|
can be interpreted without detailed knowledge of the database's internal
|
|
|
|
state.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
In <productname>PostgreSQL</productname>, logical decoding is implemented
|
|
|
|
by decoding the contents of the <link linkend="wal">write-ahead
|
|
|
|
log</link>, which describe changes on a storage level, into an
|
|
|
|
application-specific form such as a stream of tuples or SQL statements.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
2016-08-11 21:09:24 +02:00
|
|
|
<sect2 id="logicaldecoding-replication-slots">
|
2014-04-22 21:22:10 +02:00
|
|
|
<title>Replication Slots</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<indexterm>
|
2014-04-22 21:22:10 +02:00
|
|
|
<primary>replication slot</primary>
|
|
|
|
<secondary>logical replication</secondary>
|
2014-03-18 18:20:01 +01:00
|
|
|
</indexterm>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
In the context of logical replication, a slot represents a stream of
|
2014-10-31 03:52:21 +01:00
|
|
|
changes that can be replayed to a client in the order they were made on
|
2014-03-18 18:20:01 +01:00
|
|
|
the origin server. Each slot streams a sequence of changes from a single
|
2016-09-03 17:19:11 +02:00
|
|
|
database.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<note>
|
2014-03-19 14:13:05 +01:00
|
|
|
<para><productname>PostgreSQL</productname> also has streaming replication slots
|
2017-11-23 15:39:47 +01:00
|
|
|
(see <xref linkend="streaming-replication"/>), but they are used somewhat
|
2014-03-18 18:20:01 +01:00
|
|
|
differently there.
|
|
|
|
</para>
|
|
|
|
</note>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2014-10-31 03:52:21 +01:00
|
|
|
A replication slot has an identifier that is unique across all databases
|
2014-03-18 18:20:01 +01:00
|
|
|
in a <productname>PostgreSQL</productname> cluster. Slots persist
|
|
|
|
independently of the connection using them and are crash-safe.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2016-09-03 17:19:11 +02:00
|
|
|
<para>
|
|
|
|
A logical slot will emit each change just once in normal operation.
|
|
|
|
The current position of each slot is persisted only at checkpoint, so in
|
|
|
|
the case of a crash the slot may return to an earlier LSN, which will
|
2020-09-10 08:50:19 +02:00
|
|
|
then cause recent changes to be sent again when the server restarts.
|
2016-09-03 17:19:11 +02:00
|
|
|
Logical decoding clients are responsible for avoiding ill effects from
|
|
|
|
handling the same message more than once. Clients may wish to record
|
|
|
|
the last LSN they saw when decoding and skip over any repeated data or
|
|
|
|
(when using the replication protocol) request that decoding start from
|
|
|
|
that LSN rather than letting the server determine the start point.
|
|
|
|
The Replication Progress Tracking feature is designed for this purpose,
|
|
|
|
refer to <link linkend="replication-origins">replication origins</link>.
|
|
|
|
</para>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
Multiple independent slots may exist for a single database. Each slot has
|
|
|
|
its own state, allowing different consumers to receive changes from
|
|
|
|
different points in the database change stream. For most applications, a
|
|
|
|
separate slot will be required for each consumer.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
A logical replication slot knows nothing about the state of the
|
|
|
|
receiver(s). It's even possible to have multiple different receivers using
|
|
|
|
the same slot at different times; they'll just get the changes following
|
|
|
|
on from when the last receiver stopped consuming them. Only one receiver
|
|
|
|
may consume changes from a slot at any given time.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2017-12-29 15:01:25 +01:00
|
|
|
<caution>
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
Replication slots persist across crashes and know nothing about the state
|
|
|
|
of their consumer(s). They will prevent removal of required resources
|
|
|
|
even when there is no connection using them. This consumes storage
|
|
|
|
because neither required WAL nor required rows from the system catalogs
|
2014-10-31 03:52:21 +01:00
|
|
|
can be removed by <command>VACUUM</command> as long as they are required by a replication
|
2017-12-29 15:01:25 +01:00
|
|
|
slot. In extreme cases this could cause the database to shut down to prevent
|
|
|
|
transaction ID wraparound (see <xref linkend="vacuum-for-wraparound"/>).
|
|
|
|
So if a slot is no longer required it should be dropped.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2017-12-29 15:01:25 +01:00
|
|
|
</caution>
|
2014-03-18 18:20:01 +01:00
|
|
|
</sect2>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2023-01-09 21:08:24 +01:00
|
|
|
<sect2 id="logicaldecoding-explanation-output-plugins">
|
2014-03-18 18:20:01 +01:00
|
|
|
<title>Output Plugins</title>
|
|
|
|
<para>
|
|
|
|
Output plugins transform the data from the write-ahead log's internal
|
|
|
|
representation into the format the consumer of a replication slot desires.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2023-01-09 21:08:24 +01:00
|
|
|
<sect2 id="logicaldecoding-explanation-exported-snapshots">
|
2014-03-18 18:20:01 +01:00
|
|
|
<title>Exported Snapshots</title>
|
|
|
|
<para>
|
2017-03-14 22:13:56 +01:00
|
|
|
When a new replication slot is created using the streaming replication
|
2022-03-02 10:30:41 +01:00
|
|
|
interface (see <xref linkend="protocol-replication-create-replication-slot"/>), a
|
2017-03-14 22:13:56 +01:00
|
|
|
snapshot is exported
|
2017-11-23 15:39:47 +01:00
|
|
|
(see <xref linkend="functions-snapshot-synchronization"/>), which will show
|
2014-03-18 18:20:01 +01:00
|
|
|
exactly the state of the database after which all changes will be
|
|
|
|
included in the change stream. This can be used to create a new replica by
|
|
|
|
using <link linkend="sql-set-transaction"><literal>SET TRANSACTION
|
|
|
|
SNAPSHOT</literal></link> to read the state of the database at the moment
|
|
|
|
the slot was created. This transaction can then be used to dump the
|
2014-10-31 03:52:21 +01:00
|
|
|
database's state at that point in time, which afterwards can be updated
|
2014-03-18 19:57:49 +01:00
|
|
|
using the slot's contents without losing any changes.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2017-03-14 22:13:56 +01:00
|
|
|
<para>
|
|
|
|
Creation of a snapshot is not always possible. In particular, it will
|
|
|
|
fail when connected to a hot standby. Applications that do not require
|
2017-10-09 03:44:17 +02:00
|
|
|
snapshot export may suppress it with the <literal>NOEXPORT_SNAPSHOT</literal>
|
2017-03-14 22:13:56 +01:00
|
|
|
option.
|
|
|
|
</para>
|
2014-03-18 18:20:01 +01:00
|
|
|
</sect2>
|
|
|
|
</sect1>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect1 id="logicaldecoding-walsender">
|
|
|
|
<title>Streaming Replication Protocol Interface</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2014-10-31 03:52:21 +01:00
|
|
|
The commands
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
2016-01-29 04:14:56 +01:00
|
|
|
<para><literal>CREATE_REPLICATION_SLOT <replaceable>slot_name</replaceable> LOGICAL <replaceable>output_plugin</replaceable></literal></para>
|
2014-10-31 03:52:21 +01:00
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
2017-10-09 03:44:17 +02:00
|
|
|
<para><literal>DROP_REPLICATION_SLOT <replaceable>slot_name</replaceable></literal> <optional> <literal>WAIT</literal> </optional></para>
|
2014-10-31 03:52:21 +01:00
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
2016-01-29 04:14:56 +01:00
|
|
|
<para><literal>START_REPLICATION SLOT <replaceable>slot_name</replaceable> LOGICAL ...</literal></para>
|
2014-10-31 03:52:21 +01:00
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
are used to create, drop, and stream changes from a replication
|
|
|
|
slot, respectively. These commands are only available over a replication
|
2014-03-18 18:20:01 +01:00
|
|
|
connection; they cannot be used via SQL.
|
2017-11-23 15:39:47 +01:00
|
|
|
See <xref linkend="protocol-replication"/> for details on these commands.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2017-11-23 15:39:47 +01:00
|
|
|
The command <xref linkend="app-pgrecvlogical"/> can be used to control
|
2015-02-12 01:16:32 +01:00
|
|
|
logical decoding over a streaming replication connection. (It uses
|
2014-10-31 03:52:21 +01:00
|
|
|
these commands internally.)
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
</sect1>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect1 id="logicaldecoding-sql">
|
|
|
|
<title>Logical Decoding <acronym>SQL</acronym> Interface</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2017-11-23 15:39:47 +01:00
|
|
|
See <xref linkend="functions-replication"/> for detailed documentation on
|
2014-03-18 18:20:01 +01:00
|
|
|
the SQL-level API for interacting with logical decoding.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2017-11-23 15:39:47 +01:00
|
|
|
Synchronous replication (see <xref linkend="synchronous-replication"/>) is
|
2014-10-31 13:11:06 +01:00
|
|
|
only supported on replication slots used over the streaming replication interface. The
|
2014-03-18 18:20:01 +01:00
|
|
|
function interface and additional, non-core interfaces do not support
|
|
|
|
synchronous replication.
|
|
|
|
</para>
|
|
|
|
</sect1>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect1 id="logicaldecoding-catalogs">
|
2014-10-31 03:52:21 +01:00
|
|
|
<title>System Catalogs Related to Logical Decoding</title>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2016-05-05 18:33:12 +02:00
|
|
|
The <link linkend="view-pg-replication-slots"><structname>pg_replication_slots</structname></link>
|
2014-03-18 18:20:01 +01:00
|
|
|
view and the
|
2020-05-29 10:14:33 +02:00
|
|
|
<link linkend="monitoring-pg-stat-replication-view">
|
|
|
|
<structname>pg_stat_replication</structname></link>
|
2014-03-18 18:20:01 +01:00
|
|
|
view provide information about the current state of replication slots and
|
2014-10-31 03:52:21 +01:00
|
|
|
streaming replication connections respectively. These views apply to both physical and
|
2021-05-25 07:21:45 +02:00
|
|
|
logical replication. The
|
|
|
|
<link linkend="monitoring-pg-stat-replication-slots-view">
|
|
|
|
<structname>pg_stat_replication_slots</structname></link>
|
|
|
|
view provides statistics information about the logical replication slots.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
</sect1>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect1 id="logicaldecoding-output-plugin">
|
|
|
|
<title>Logical Decoding Output Plugins</title>
|
|
|
|
<para>
|
|
|
|
An example output plugin can be found in the
|
|
|
|
<link linkend="test-decoding">
|
|
|
|
<filename>contrib/test_decoding</filename>
|
|
|
|
</link>
|
|
|
|
subdirectory of the PostgreSQL source tree.
|
|
|
|
</para>
|
|
|
|
<sect2 id="logicaldecoding-output-init">
|
|
|
|
<title>Initialization Function</title>
|
2018-02-03 03:10:59 +01:00
|
|
|
<indexterm zone="logicaldecoding-output-init">
|
2014-03-18 18:20:01 +01:00
|
|
|
<primary>_PG_output_plugin_init</primary>
|
|
|
|
</indexterm>
|
|
|
|
<para>
|
|
|
|
An output plugin is loaded by dynamically loading a shared library with
|
2014-07-17 04:20:15 +02:00
|
|
|
the output plugin's name as the library base name. The normal library
|
2014-03-18 18:20:01 +01:00
|
|
|
search path is used to locate the library. To provide the required output
|
|
|
|
plugin callbacks and to indicate that the library is actually an output
|
|
|
|
plugin it needs to provide a function named
|
|
|
|
<function>_PG_output_plugin_init</function>. This function is passed a
|
|
|
|
struct that needs to be filled with the callback function pointers for
|
|
|
|
individual actions.
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
typedef struct OutputPluginCallbacks
|
|
|
|
{
|
|
|
|
LogicalDecodeStartupCB startup_cb;
|
|
|
|
LogicalDecodeBeginCB begin_cb;
|
|
|
|
LogicalDecodeChangeCB change_cb;
|
2018-04-07 17:17:56 +02:00
|
|
|
LogicalDecodeTruncateCB truncate_cb;
|
2014-03-18 18:20:01 +01:00
|
|
|
LogicalDecodeCommitCB commit_cb;
|
2016-04-06 11:05:41 +02:00
|
|
|
LogicalDecodeMessageCB message_cb;
|
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
2015-04-29 19:30:53 +02:00
|
|
|
LogicalDecodeFilterByOriginCB filter_by_origin_cb;
|
2014-03-18 18:20:01 +01:00
|
|
|
LogicalDecodeShutdownCB shutdown_cb;
|
2020-12-30 11:47:26 +01:00
|
|
|
LogicalDecodeFilterPrepareCB filter_prepare_cb;
|
|
|
|
LogicalDecodeBeginPrepareCB begin_prepare_cb;
|
|
|
|
LogicalDecodePrepareCB prepare_cb;
|
|
|
|
LogicalDecodeCommitPreparedCB commit_prepared_cb;
|
|
|
|
LogicalDecodeRollbackPreparedCB rollback_prepared_cb;
|
2020-07-28 04:36:44 +02:00
|
|
|
LogicalDecodeStreamStartCB stream_start_cb;
|
|
|
|
LogicalDecodeStreamStopCB stream_stop_cb;
|
|
|
|
LogicalDecodeStreamAbortCB stream_abort_cb;
|
2020-12-30 11:47:26 +01:00
|
|
|
LogicalDecodeStreamPrepareCB stream_prepare_cb;
|
2020-07-28 04:36:44 +02:00
|
|
|
LogicalDecodeStreamCommitCB stream_commit_cb;
|
|
|
|
LogicalDecodeStreamChangeCB stream_change_cb;
|
|
|
|
LogicalDecodeStreamMessageCB stream_message_cb;
|
|
|
|
LogicalDecodeStreamTruncateCB stream_truncate_cb;
|
2014-03-18 18:20:01 +01:00
|
|
|
} OutputPluginCallbacks;
|
2014-07-08 17:39:07 +02:00
|
|
|
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef void (*LogicalOutputPluginInit) (struct OutputPluginCallbacks *cb);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
The <function>begin_cb</function>, <function>change_cb</function>
|
|
|
|
and <function>commit_cb</function> callbacks are required,
|
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
2015-04-29 19:30:53 +02:00
|
|
|
while <function>startup_cb</function>,
|
2018-04-07 17:17:56 +02:00
|
|
|
<function>filter_by_origin_cb</function>, <function>truncate_cb</function>,
|
2022-04-07 18:13:13 +02:00
|
|
|
and <function>shutdown_cb</function> are optional.
|
|
|
|
If <function>truncate_cb</function> is not set but a
|
2018-04-07 17:17:56 +02:00
|
|
|
<command>TRUNCATE</command> is to be decoded, the action will be ignored.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2020-07-28 04:36:44 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
An output plugin may also define functions to support streaming of large,
|
|
|
|
in-progress transactions. The <function>stream_start_cb</function>,
|
|
|
|
<function>stream_stop_cb</function>, <function>stream_abort_cb</function>,
|
2020-12-30 11:47:26 +01:00
|
|
|
<function>stream_commit_cb</function>, <function>stream_change_cb</function>,
|
|
|
|
and <function>stream_prepare_cb</function>
|
2022-04-07 18:13:13 +02:00
|
|
|
are required, while <function>stream_message_cb</function> and
|
2020-07-28 04:36:44 +02:00
|
|
|
<function>stream_truncate_cb</function> are optional.
|
|
|
|
</para>
|
2020-12-30 11:47:26 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
An output plugin may also define functions to support two-phase commits,
|
|
|
|
which allows actions to be decoded on the <command>PREPARE TRANSACTION</command>.
|
2021-04-22 22:47:57 +02:00
|
|
|
The <function>begin_prepare_cb</function>, <function>prepare_cb</function>,
|
2020-12-30 11:47:26 +01:00
|
|
|
<function>stream_prepare_cb</function>,
|
|
|
|
<function>commit_prepared_cb</function> and <function>rollback_prepared_cb</function>
|
|
|
|
callbacks are required, while <function>filter_prepare_cb</function> is optional.
|
|
|
|
</para>
|
2014-03-18 18:20:01 +01:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2 id="logicaldecoding-capabilities">
|
|
|
|
<title>Capabilities</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
To decode, format and output changes, output plugins can use most of the
|
|
|
|
backend's normal infrastructure, including calling output functions. Read
|
|
|
|
only access to relations is permitted as long as only relations are
|
|
|
|
accessed that either have been created by <command>initdb</command> in
|
2014-03-18 19:57:49 +01:00
|
|
|
the <literal>pg_catalog</literal> schema, or have been marked as user
|
2014-03-18 18:20:01 +01:00
|
|
|
provided catalog tables using
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
ALTER TABLE user_catalog_table SET (user_catalog_table = true);
|
|
|
|
CREATE TABLE another_catalog_table(data text) WITH (user_catalog_table = true);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
Implement streaming mode in ReorderBuffer.
Instead of serializing the transaction to disk after reaching the
logical_decoding_work_mem limit in memory, we consume the changes we have
in memory and invoke stream API methods added by commit 45fdc9738b.
However, sometimes if we have incomplete toast or speculative insert we
spill to the disk because we can't generate the complete tuple and stream.
And, as soon as we get the complete tuple we stream the transaction
including the serialized changes.
We can do this incremental processing thanks to having assignments
(associating subxact with toplevel xacts) in WAL right away, and
thanks to logging the invalidation messages at each command end. These
features are added by commits 0bead9af48 and c55040ccd0 respectively.
Now that we can stream in-progress transactions, the concurrent aborts
may cause failures when the output plugin consults catalogs (both system
and user-defined).
We handle such failures by returning ERRCODE_TRANSACTION_ROLLBACK
sqlerrcode from system table scan APIs to the backend or WALSender
decoding a specific uncommitted transaction. The decoding logic on the
receipt of such a sqlerrcode aborts the decoding of the current
transaction and continue with the decoding of other transactions.
We have ReorderBufferTXN pointer in each ReorderBufferChange by which we
know which xact it belongs to. The output plugin can use this to decide
which changes to discard in case of stream_abort_cb (e.g. when a subxact
gets discarded).
We also provide a new option via SQL APIs to fetch the changes being
streamed.
Author: Dilip Kumar, Tomas Vondra, Amit Kapila, Nikhil Sontakke
Reviewed-by: Amit Kapila, Kuntal Ghosh, Ajin Cherian
Tested-by: Neha Sharma, Mahendra Singh Thalor and Ajin Cherian
Discussion: https://postgr.es/m/688b0b7f-2f6c-d827-c27b-216a8e3ea700@2ndquadrant.com
2020-08-08 04:04:39 +02:00
|
|
|
Note that access to user catalog tables or regular system catalog tables
|
|
|
|
in the output plugins has to be done via the <literal>systable_*</literal>
|
|
|
|
scan APIs only. Access via the <literal>heap_*</literal> scan APIs will
|
|
|
|
error out. Additionally, any actions leading to transaction ID assignment
|
|
|
|
are prohibited. That, among others, includes writing to tables, performing
|
|
|
|
DDL changes, and calling <literal>pg_current_xact_id()</literal>.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
2014-10-01 13:13:59 +02:00
|
|
|
<sect2 id="logicaldecoding-output-mode">
|
|
|
|
<title>Output Modes</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-10-01 13:13:59 +02:00
|
|
|
<para>
|
|
|
|
Output plugin callbacks can pass data to the consumer in nearly arbitrary
|
|
|
|
formats. For some use cases, like viewing the changes via SQL, returning
|
2014-10-31 03:52:21 +01:00
|
|
|
data in a data type that can contain arbitrary data (e.g., <type>bytea</type>) is
|
2014-10-01 13:13:59 +02:00
|
|
|
cumbersome. If the output plugin only outputs textual data in the
|
2014-10-31 03:52:21 +01:00
|
|
|
server's encoding, it can declare that by
|
2017-10-09 03:44:17 +02:00
|
|
|
setting <literal>OutputPluginOptions.output_type</literal>
|
|
|
|
to <literal>OUTPUT_PLUGIN_TEXTUAL_OUTPUT</literal> instead
|
|
|
|
of <literal>OUTPUT_PLUGIN_BINARY_OUTPUT</literal> in
|
2014-10-01 13:13:59 +02:00
|
|
|
the <link linkend="logicaldecoding-output-plugin-startup">startup
|
2017-10-09 03:44:17 +02:00
|
|
|
callback</link>. In that case, all the data has to be in the server's encoding
|
|
|
|
so that a <type>text</type> datum can contain it. This is checked in assertion-enabled
|
2014-10-01 13:13:59 +02:00
|
|
|
builds.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect2 id="logicaldecoding-output-plugin-callbacks">
|
|
|
|
<title>Output Plugin Callbacks</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
An output plugin gets notified about changes that are happening via
|
|
|
|
various callbacks it needs to provide.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2014-07-17 04:20:15 +02:00
|
|
|
Concurrent transactions are decoded in commit order, and only changes
|
|
|
|
belonging to a specific transaction are decoded between
|
2014-03-18 18:20:01 +01:00
|
|
|
the <literal>begin</literal> and <literal>commit</literal>
|
|
|
|
callbacks. Transactions that were rolled back explicitly or implicitly
|
|
|
|
never get
|
2014-10-31 03:52:21 +01:00
|
|
|
decoded. Successful savepoints are
|
2014-03-18 18:20:01 +01:00
|
|
|
folded into the transaction containing them in the order they were
|
2020-12-30 11:47:26 +01:00
|
|
|
executed within that transaction. A transaction that is prepared for
|
|
|
|
a two-phase commit using <command>PREPARE TRANSACTION</command> will
|
|
|
|
also be decoded if the output plugin callbacks needed for decoding
|
2021-04-01 04:27:34 +02:00
|
|
|
them are provided. It is possible that the current prepared transaction
|
|
|
|
which is being decoded is aborted concurrently via a
|
|
|
|
<command>ROLLBACK PREPARED</command> command. In that case, the logical
|
|
|
|
decoding of this transaction will be aborted too. All the changes of such
|
|
|
|
a transaction are skipped once the abort is detected and the
|
|
|
|
<function>prepare_cb</function> callback is invoked. Thus even in case of
|
|
|
|
a concurrent abort, enough information is provided to the output plugin
|
|
|
|
for it to properly deal with <command>ROLLBACK PREPARED</command> once
|
|
|
|
that is decoded.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Only transactions that have already safely been flushed to disk will be
|
2014-10-31 03:52:21 +01:00
|
|
|
decoded. That can lead to a <command>COMMIT</command> not immediately being decoded in a
|
2014-03-18 18:20:01 +01:00
|
|
|
directly following <literal>pg_logical_slot_get_changes()</literal>
|
|
|
|
when <varname>synchronous_commit</varname> is set
|
|
|
|
to <literal>off</literal>.
|
|
|
|
</para>
|
|
|
|
</note>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-startup">
|
|
|
|
<title>Startup Callback</title>
|
|
|
|
<para>
|
|
|
|
The optional <function>startup_cb</function> callback is called whenever
|
2014-03-18 19:57:49 +01:00
|
|
|
a replication slot is created or asked to stream changes, independent
|
2014-03-18 18:20:01 +01:00
|
|
|
of the number of changes that are ready to be put out.
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef void (*LogicalDecodeStartupCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
OutputPluginOptions *options,
|
|
|
|
bool is_init);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 19:57:49 +01:00
|
|
|
The <literal>is_init</literal> parameter will be true when the
|
2014-03-18 18:20:01 +01:00
|
|
|
replication slot is being created and false
|
|
|
|
otherwise. <parameter>options</parameter> points to a struct of options
|
|
|
|
that output plugins can set:
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
typedef struct OutputPluginOptions
|
|
|
|
{
|
|
|
|
OutputPluginOutputType output_type;
|
2018-03-21 14:13:24 +01:00
|
|
|
bool receive_rewrites;
|
2014-03-18 18:20:01 +01:00
|
|
|
} OutputPluginOptions;
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
<literal>output_type</literal> has to either be set to
|
|
|
|
<literal>OUTPUT_PLUGIN_TEXTUAL_OUTPUT</literal>
|
2014-10-01 13:13:59 +02:00
|
|
|
or <literal>OUTPUT_PLUGIN_BINARY_OUTPUT</literal>. See also
|
2017-11-23 15:39:47 +01:00
|
|
|
<xref linkend="logicaldecoding-output-mode"/>.
|
2018-03-21 14:13:24 +01:00
|
|
|
If <literal>receive_rewrites</literal> is true, the output plugin will
|
|
|
|
also be called for changes made by heap rewrites during certain DDL
|
|
|
|
operations. These are of interest to plugins that handle DDL
|
|
|
|
replication, but they require special handling.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
The startup callback should validate the options present in
|
|
|
|
<literal>ctx->output_plugin_options</literal>. If the output plugin
|
|
|
|
needs to have a state, it can
|
|
|
|
use <literal>ctx->output_plugin_private</literal> to store it.
|
|
|
|
</para>
|
|
|
|
</sect3>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-shutdown">
|
|
|
|
<title>Shutdown Callback</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
The optional <function>shutdown_cb</function> callback is called
|
|
|
|
whenever a formerly active replication slot is not used anymore and can
|
|
|
|
be used to deallocate resources private to the output plugin. The slot
|
|
|
|
isn't necessarily being dropped, streaming is just being stopped.
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef void (*LogicalDecodeShutdownCB) (struct LogicalDecodingContext *ctx);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
</sect3>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-begin">
|
|
|
|
<title>Transaction Begin Callback</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
The required <function>begin_cb</function> callback is called whenever a
|
2014-07-17 04:20:15 +02:00
|
|
|
start of a committed transaction has been decoded. Aborted transactions
|
2014-03-18 18:20:01 +01:00
|
|
|
and their contents never get decoded.
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef void (*LogicalDecodeBeginCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
The <parameter>txn</parameter> parameter contains meta information about
|
2014-07-17 04:20:15 +02:00
|
|
|
the transaction, like the time stamp at which it has been committed and
|
2014-03-18 18:20:01 +01:00
|
|
|
its XID.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
</sect3>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-commit">
|
|
|
|
<title>Transaction End Callback</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
The required <function>commit_cb</function> callback is called whenever
|
|
|
|
a transaction commit has been
|
|
|
|
decoded. The <function>change_cb</function> callbacks for all modified
|
|
|
|
rows will have been called before this, if there have been any modified
|
|
|
|
rows.
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef void (*LogicalDecodeCommitCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr commit_lsn);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
</sect3>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-change">
|
2014-10-31 03:52:21 +01:00
|
|
|
<title>Change Callback</title>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
The required <function>change_cb</function> callback is called for every
|
|
|
|
individual row modification inside a transaction, may it be
|
2014-10-31 03:52:21 +01:00
|
|
|
an <command>INSERT</command>, <command>UPDATE</command>,
|
2014-03-18 18:20:01 +01:00
|
|
|
or <command>DELETE</command>. Even if the original command modified
|
2014-07-17 04:20:15 +02:00
|
|
|
several rows at once the callback will be called individually for each
|
2020-12-30 11:47:26 +01:00
|
|
|
row. The <function>change_cb</function> callback may access system or
|
|
|
|
user catalog tables to aid in the process of outputting the row
|
|
|
|
modification details. In case of decoding a prepared (but yet
|
|
|
|
uncommitted) transaction or decoding of an uncommitted transaction, this
|
|
|
|
change callback might also error out due to simultaneous rollback of
|
|
|
|
this very same transaction. In that case, the logical decoding of this
|
|
|
|
aborted transaction is stopped gracefully.
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef void (*LogicalDecodeChangeCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
Relation relation,
|
|
|
|
ReorderBufferChange *change);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
The <parameter>ctx</parameter> and <parameter>txn</parameter> parameters
|
|
|
|
have the same contents as for the <function>begin_cb</function>
|
|
|
|
and <function>commit_cb</function> callbacks, but additionally the
|
|
|
|
relation descriptor <parameter>relation</parameter> points to the
|
|
|
|
relation the row belongs to and a struct
|
|
|
|
<parameter>change</parameter> describing the row modification are passed
|
|
|
|
in.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Only changes in user defined tables that are not unlogged
|
2017-11-23 15:39:47 +01:00
|
|
|
(see <xref linkend="sql-createtable-unlogged"/>) and not temporary
|
|
|
|
(see <xref linkend="sql-createtable-temporary"/>) can be extracted using
|
2014-03-18 18:20:01 +01:00
|
|
|
logical decoding.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</sect3>
|
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
2015-04-29 19:30:53 +02:00
|
|
|
|
2018-04-07 17:17:56 +02:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-truncate">
|
|
|
|
<title>Truncate Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <function>truncate_cb</function> callback is called for a
|
|
|
|
<command>TRUNCATE</command> command.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeTruncateCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
int nrelations,
|
|
|
|
Relation relations[],
|
|
|
|
ReorderBufferChange *change);
|
|
|
|
</programlisting>
|
|
|
|
The parameters are analogous to the <function>change_cb</function>
|
|
|
|
callback. However, because <command>TRUNCATE</command> actions on
|
|
|
|
tables connected by foreign keys need to be executed together, this
|
|
|
|
callback receives an array of relations instead of just a single one.
|
|
|
|
See the description of the <xref linkend="sql-truncate"/> statement for
|
|
|
|
details.
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
2015-05-15 04:34:28 +02:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-filter-origin">
|
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
2015-04-29 19:30:53 +02:00
|
|
|
<title>Origin Filter Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The optional <function>filter_by_origin_cb</function> callback
|
2015-09-11 03:22:21 +02:00
|
|
|
is called to determine whether data that has been replayed
|
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
2015-04-29 19:30:53 +02:00
|
|
|
from <parameter>origin_id</parameter> is of interest to the
|
|
|
|
output plugin.
|
|
|
|
<programlisting>
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef bool (*LogicalDecodeFilterByOriginCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
RepOriginId origin_id);
|
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
2015-04-29 19:30:53 +02:00
|
|
|
</programlisting>
|
|
|
|
The <parameter>ctx</parameter> parameter has the same contents
|
|
|
|
as for the other callbacks. No information but the origin is
|
|
|
|
available. To signal that changes originating on the passed in
|
|
|
|
node are irrelevant, return true, causing them to be filtered
|
|
|
|
away; false otherwise. The other callbacks will not be called
|
|
|
|
for transactions and changes that have been filtered away.
|
|
|
|
</para>
|
|
|
|
<para>
|
2015-09-11 03:22:21 +02:00
|
|
|
This is useful when implementing cascading or multidirectional
|
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
2015-04-29 19:30:53 +02:00
|
|
|
replication solutions. Filtering by the origin allows to
|
|
|
|
prevent replicating the same changes back and forth in such
|
|
|
|
setups. While transactions and changes also carry information
|
|
|
|
about the origin, filtering via this callback is noticeably
|
|
|
|
more efficient.
|
|
|
|
</para>
|
|
|
|
</sect3>
|
2016-04-06 11:05:41 +02:00
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-message">
|
|
|
|
<title>Generic Message Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The optional <function>message_cb</function> callback is called whenever
|
|
|
|
a logical decoding message has been decoded.
|
|
|
|
<programlisting>
|
2017-02-16 00:15:47 +01:00
|
|
|
typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr message_lsn,
|
|
|
|
bool transactional,
|
|
|
|
const char *prefix,
|
|
|
|
Size message_size,
|
|
|
|
const char *message);
|
2016-04-06 11:05:41 +02:00
|
|
|
</programlisting>
|
|
|
|
The <parameter>txn</parameter> parameter contains meta information about
|
|
|
|
the transaction, like the time stamp at which it has been committed and
|
|
|
|
its XID. Note however that it can be NULL when the message is
|
|
|
|
non-transactional and the XID was not assigned yet in the transaction
|
|
|
|
which logged the message. The <parameter>lsn</parameter> has WAL
|
2017-05-12 19:51:27 +02:00
|
|
|
location of the message. The <parameter>transactional</parameter> says
|
2020-12-30 11:47:26 +01:00
|
|
|
if the message was sent as transactional or not. Similar to the change
|
|
|
|
callback, in case of decoding a prepared (but yet uncommitted)
|
|
|
|
transaction or decoding of an uncommitted transaction, this message
|
|
|
|
callback might also error out due to simultaneous rollback of
|
|
|
|
this very same transaction. In that case, the logical decoding of this
|
|
|
|
aborted transaction is stopped gracefully.
|
|
|
|
|
2016-04-06 11:05:41 +02:00
|
|
|
The <parameter>prefix</parameter> is arbitrary null-terminated prefix
|
|
|
|
which can be used for identifying interesting messages for the current
|
|
|
|
plugin. And finally the <parameter>message</parameter> parameter holds
|
|
|
|
the actual message of <parameter>message_size</parameter> size.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Extra care should be taken to ensure that the prefix the output plugin
|
|
|
|
considers interesting is unique. Using name of the extension or the
|
|
|
|
output plugin itself is often a good choice.
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
2020-12-30 11:47:26 +01:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-filter-prepare">
|
|
|
|
<title>Prepare Filter Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The optional <function>filter_prepare_cb</function> callback
|
|
|
|
is called to determine whether data that is part of the current
|
2021-02-24 08:13:17 +01:00
|
|
|
two-phase commit transaction should be considered for decoding
|
|
|
|
at this prepare stage or later as a regular one-phase transaction at
|
|
|
|
<command>COMMIT PREPARED</command> time. To signal that
|
2020-12-30 11:47:26 +01:00
|
|
|
decoding should be skipped, return <literal>true</literal>;
|
|
|
|
<literal>false</literal> otherwise. When the callback is not
|
2021-03-30 07:04:43 +02:00
|
|
|
defined, <literal>false</literal> is assumed (i.e. no filtering, all
|
|
|
|
transactions using two-phase commit are decoded in two phases as well).
|
2020-12-30 11:47:26 +01:00
|
|
|
<programlisting>
|
|
|
|
typedef bool (*LogicalDecodeFilterPrepareCB) (struct LogicalDecodingContext *ctx,
|
2021-03-30 07:04:43 +02:00
|
|
|
TransactionId xid,
|
2020-12-30 11:47:26 +01:00
|
|
|
const char *gid);
|
|
|
|
</programlisting>
|
2021-03-30 07:04:43 +02:00
|
|
|
The <parameter>ctx</parameter> parameter has the same contents as for
|
|
|
|
the other callbacks. The parameters <parameter>xid</parameter>
|
|
|
|
and <parameter>gid</parameter> provide two different ways to identify
|
|
|
|
the transaction. The later <command>COMMIT PREPARED</command> or
|
|
|
|
<command>ROLLBACK PREPARED</command> carries both identifiers,
|
|
|
|
providing an output plugin the choice of what to use.
|
2020-12-30 11:47:26 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2021-03-30 07:04:43 +02:00
|
|
|
The callback may be invoked multiple times per transaction to decode
|
|
|
|
and must provide the same static answer for a given pair of
|
|
|
|
<parameter>xid</parameter> and <parameter>gid</parameter> every time
|
|
|
|
it is called.
|
2020-12-30 11:47:26 +01:00
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-begin-prepare">
|
|
|
|
<title>Transaction Begin Prepare Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The required <function>begin_prepare_cb</function> callback is called
|
|
|
|
whenever the start of a prepared transaction has been decoded. The
|
|
|
|
<parameter>gid</parameter> field, which is part of the
|
2021-02-24 08:13:17 +01:00
|
|
|
<parameter>txn</parameter> parameter, can be used in this callback to
|
|
|
|
check if the plugin has already received this <command>PREPARE</command>
|
2022-04-20 17:04:28 +02:00
|
|
|
in which case it can either error out or skip the remaining changes of
|
2021-03-01 04:41:18 +01:00
|
|
|
the transaction.
|
2021-04-17 14:14:26 +02:00
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeBeginPrepareCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn);
|
|
|
|
</programlisting>
|
2020-12-30 11:47:26 +01:00
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-prepare">
|
|
|
|
<title>Transaction Prepare Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The required <function>prepare_cb</function> callback is called whenever
|
|
|
|
a transaction which is prepared for two-phase commit has been
|
|
|
|
decoded. The <function>change_cb</function> callback for all modified
|
|
|
|
rows will have been called before this, if there have been any modified
|
|
|
|
rows. The <parameter>gid</parameter> field, which is part of the
|
2021-02-24 08:13:17 +01:00
|
|
|
<parameter>txn</parameter> parameter, can be used in this callback.
|
2021-04-17 14:14:26 +02:00
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodePrepareCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr prepare_lsn);
|
|
|
|
</programlisting>
|
2020-12-30 11:47:26 +01:00
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-commit-prepared">
|
|
|
|
<title>Transaction Commit Prepared Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The required <function>commit_prepared_cb</function> callback is called
|
2021-02-24 08:13:17 +01:00
|
|
|
whenever a transaction <command>COMMIT PREPARED</command> has been decoded.
|
|
|
|
The <parameter>gid</parameter> field, which is part of the
|
|
|
|
<parameter>txn</parameter> parameter, can be used in this callback.
|
2021-04-17 14:14:26 +02:00
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeCommitPreparedCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr commit_lsn);
|
|
|
|
</programlisting>
|
2020-12-30 11:47:26 +01:00
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-rollback-prepared">
|
|
|
|
<title>Transaction Rollback Prepared Callback</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The required <function>rollback_prepared_cb</function> callback is called
|
2021-02-24 08:13:17 +01:00
|
|
|
whenever a transaction <command>ROLLBACK PREPARED</command> has been
|
|
|
|
decoded. The <parameter>gid</parameter> field, which is part of the
|
|
|
|
<parameter>txn</parameter> parameter, can be used in this callback. The
|
2020-12-30 11:47:26 +01:00
|
|
|
parameters <parameter>prepare_end_lsn</parameter> and
|
|
|
|
<parameter>prepare_time</parameter> can be used to check if the plugin
|
2021-02-24 08:13:17 +01:00
|
|
|
has received this <command>PREPARE TRANSACTION</command> in which case
|
|
|
|
it can apply the rollback, otherwise, it can skip the rollback operation. The
|
2020-12-30 11:47:26 +01:00
|
|
|
<parameter>gid</parameter> alone is not sufficient because the downstream
|
2021-02-24 08:13:17 +01:00
|
|
|
node can have a prepared transaction with same identifier.
|
2021-04-17 14:14:26 +02:00
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeRollbackPreparedCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr prepare_end_lsn,
|
|
|
|
TimestampTz prepare_time);
|
|
|
|
</programlisting>
|
2020-12-30 11:47:26 +01:00
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
2020-07-28 04:36:44 +02:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-start">
|
|
|
|
<title>Stream Start Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_start_cb</function> callback is called when opening
|
|
|
|
a block of streamed changes from an in-progress transaction.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamStartCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-stop">
|
|
|
|
<title>Stream Stop Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_stop_cb</function> callback is called when closing
|
|
|
|
a block of streamed changes from an in-progress transaction.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamStopCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-abort">
|
|
|
|
<title>Stream Abort Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_abort_cb</function> callback is called to abort
|
|
|
|
a previously streamed transaction.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamAbortCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr abort_lsn);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
2020-12-30 11:47:26 +01:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-prepare">
|
|
|
|
<title>Stream Prepare Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_prepare_cb</function> callback is called to prepare
|
|
|
|
a previously streamed transaction as part of a two-phase commit.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamPrepareCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr prepare_lsn);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
2020-07-28 04:36:44 +02:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-commit">
|
|
|
|
<title>Stream Commit Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_commit_cb</function> callback is called to commit
|
|
|
|
a previously streamed transaction.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamCommitCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr commit_lsn);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
2022-04-20 17:04:28 +02:00
|
|
|
|
2020-07-28 04:36:44 +02:00
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-change">
|
|
|
|
<title>Stream Change Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_change_cb</function> callback is called when sending
|
|
|
|
a change in a block of streamed changes (demarcated by
|
|
|
|
<function>stream_start_cb</function> and <function>stream_stop_cb</function> calls).
|
|
|
|
The actual changes are not displayed as the transaction can abort at a later
|
|
|
|
point in time and we don't decode changes for aborted transactions.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamChangeCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
Relation relation,
|
|
|
|
ReorderBufferChange *change);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-message">
|
|
|
|
<title>Stream Message Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_message_cb</function> callback is called when sending
|
|
|
|
a generic message in a block of streamed changes (demarcated by
|
|
|
|
<function>stream_start_cb</function> and <function>stream_stop_cb</function> calls).
|
|
|
|
The message contents for transactional messages are not displayed as the transaction
|
|
|
|
can abort at a later point in time and we don't decode changes for aborted
|
|
|
|
transactions.
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamMessageCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
XLogRecPtr message_lsn,
|
|
|
|
bool transactional,
|
|
|
|
const char *prefix,
|
|
|
|
Size message_size,
|
|
|
|
const char *message);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="logicaldecoding-output-plugin-stream-truncate">
|
|
|
|
<title>Stream Truncate Callback</title>
|
|
|
|
<para>
|
|
|
|
The <function>stream_truncate_cb</function> callback is called for a
|
|
|
|
<command>TRUNCATE</command> command in a block of streamed changes
|
|
|
|
(demarcated by <function>stream_start_cb</function> and
|
|
|
|
<function>stream_stop_cb</function> calls).
|
|
|
|
<programlisting>
|
|
|
|
typedef void (*LogicalDecodeStreamTruncateCB) (struct LogicalDecodingContext *ctx,
|
|
|
|
ReorderBufferTXN *txn,
|
|
|
|
int nrelations,
|
|
|
|
Relation relations[],
|
|
|
|
ReorderBufferChange *change);
|
|
|
|
</programlisting>
|
|
|
|
The parameters are analogous to the <function>stream_change_cb</function>
|
|
|
|
callback. However, because <command>TRUNCATE</command> actions on
|
|
|
|
tables connected by foreign keys need to be executed together, this
|
|
|
|
callback receives an array of relations instead of just a single one.
|
|
|
|
See the description of the <xref linkend="sql-truncate"/> statement for
|
|
|
|
details.
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
</sect2>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect2 id="logicaldecoding-output-plugin-output">
|
2014-10-31 03:52:21 +01:00
|
|
|
<title>Functions for Producing Output</title>
|
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
To actually produce output, output plugins can write data to
|
|
|
|
the <literal>StringInfo</literal> output buffer
|
|
|
|
in <literal>ctx->out</literal> when inside
|
2014-10-31 03:52:21 +01:00
|
|
|
the <function>begin_cb</function>, <function>commit_cb</function>,
|
2014-03-18 18:20:01 +01:00
|
|
|
or <function>change_cb</function> callbacks. Before writing to the output
|
2014-10-31 03:52:21 +01:00
|
|
|
buffer, <function>OutputPluginPrepareWrite(ctx, last_write)</function> has
|
2014-03-18 18:20:01 +01:00
|
|
|
to be called, and after finishing writing to the
|
2014-10-31 03:52:21 +01:00
|
|
|
buffer, <function>OutputPluginWrite(ctx, last_write)</function> has to be
|
2014-03-18 18:20:01 +01:00
|
|
|
called to perform the write. The <parameter>last_write</parameter>
|
|
|
|
indicates whether a particular write was the callback's last write.
|
|
|
|
</para>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
The following example shows how to output data to the consumer of an
|
|
|
|
output plugin:
|
2014-07-08 17:39:07 +02:00
|
|
|
<programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
OutputPluginPrepareWrite(ctx, true);
|
|
|
|
appendStringInfo(ctx->out, "BEGIN %u", txn->xid);
|
|
|
|
OutputPluginWrite(ctx, true);
|
2014-07-08 17:39:07 +02:00
|
|
|
</programlisting>
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
</sect1>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect1 id="logicaldecoding-writer">
|
|
|
|
<title>Logical Decoding Output Writers</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
|
|
|
It is possible to add more output methods for logical decoding.
|
|
|
|
For details, see
|
|
|
|
<filename>src/backend/replication/logical/logicalfuncs.c</filename>.
|
|
|
|
Essentially, three functions need to be provided: one to read WAL, one to
|
|
|
|
prepare writing output, and one to write the output
|
2017-11-23 15:39:47 +01:00
|
|
|
(see <xref linkend="logicaldecoding-output-plugin-output"/>).
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
|
|
|
</sect1>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<sect1 id="logicaldecoding-synchronous">
|
2014-10-31 03:52:21 +01:00
|
|
|
<title>Synchronous Replication Support for Logical Decoding</title>
|
2023-01-09 21:08:24 +01:00
|
|
|
<sect2 id="logicaldecoding-synchronous-overview">
|
2021-06-17 06:26:05 +02:00
|
|
|
<title>Overview</title>
|
2014-10-31 03:52:21 +01:00
|
|
|
|
2014-03-18 18:20:01 +01:00
|
|
|
<para>
|
2021-06-17 06:26:05 +02:00
|
|
|
Logical decoding can be used to build
|
|
|
|
<link linkend="synchronous-replication">synchronous
|
|
|
|
replication</link> solutions with the same user interface as synchronous
|
|
|
|
replication for <link linkend="streaming-replication">streaming
|
|
|
|
replication</link>. To do this, the streaming replication interface
|
|
|
|
(see <xref linkend="logicaldecoding-walsender"/>) must be used to stream out
|
|
|
|
data. Clients have to send <literal>Standby status update (F)</literal>
|
|
|
|
(see <xref linkend="protocol-replication"/>) messages, just like streaming
|
|
|
|
replication clients do.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
A synchronous replica receiving changes via logical decoding will work in
|
|
|
|
the scope of a single database. Since, in contrast to
|
|
|
|
that, <parameter>synchronous_standby_names</parameter> currently is
|
|
|
|
server wide, this means this technique will not work properly if more
|
|
|
|
than one database is actively used.
|
2014-03-18 18:20:01 +01:00
|
|
|
</para>
|
2021-06-17 06:26:05 +02:00
|
|
|
</note>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2 id="logicaldecoding-synchronous-caveats">
|
|
|
|
<title>Caveats</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
In synchronous replication setup, a deadlock can happen, if the transaction
|
2021-06-24 05:43:46 +02:00
|
|
|
has locked [user] catalog tables exclusively. See
|
|
|
|
<xref linkend="logicaldecoding-capabilities"/> for information on user
|
|
|
|
catalog tables. This is because logical decoding of transactions can lock
|
|
|
|
catalog tables to access them. To avoid this users must refrain from taking
|
|
|
|
an exclusive lock on [user] catalog tables. This can happen in the following
|
|
|
|
ways:
|
2021-06-17 06:26:05 +02:00
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Issuing an explicit <command>LOCK</command> on <structname>pg_class</structname>
|
2021-06-24 05:43:46 +02:00
|
|
|
in a transaction.
|
2021-06-17 06:26:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Perform <command>CLUSTER</command> on <structname>pg_class</structname> in
|
|
|
|
a transaction.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<command>PREPARE TRANSACTION</command> after <command>LOCK</command> command
|
|
|
|
on <structname>pg_class</structname> and allow logical decoding of two-phase
|
|
|
|
transactions.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<command>PREPARE TRANSACTION</command> after <command>CLUSTER</command>
|
|
|
|
command on <structname>pg_trigger</structname> and allow logical decoding of
|
|
|
|
two-phase transactions. This will lead to deadlock only when published table
|
|
|
|
have a trigger.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Executing <command>TRUNCATE</command> on [user] catalog table in a
|
|
|
|
transaction.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2021-06-24 05:43:46 +02:00
|
|
|
|
|
|
|
Note that these commands that can cause deadlock apply to not only explicitly
|
|
|
|
indicated system catalog tables above but also to any other [user] catalog
|
|
|
|
table.
|
2021-06-17 06:26:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
2014-03-18 18:20:01 +01:00
|
|
|
</sect1>
|
2020-07-28 04:36:44 +02:00
|
|
|
|
|
|
|
<sect1 id="logicaldecoding-streaming">
|
|
|
|
<title>Streaming of Large Transactions for Logical Decoding</title>
|
|
|
|
|
|
|
|
<para>
|
2020-09-01 00:33:37 +02:00
|
|
|
The basic output plugin callbacks (e.g., <function>begin_cb</function>,
|
2020-07-28 04:36:44 +02:00
|
|
|
<function>change_cb</function>, <function>commit_cb</function> and
|
|
|
|
<function>message_cb</function>) are only invoked when the transaction
|
|
|
|
actually commits. The changes are still decoded from the transaction
|
|
|
|
log, but are only passed to the output plugin at commit (and discarded
|
|
|
|
if the transaction aborts).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This means that while the decoding happens incrementally, and may spill
|
|
|
|
to disk to keep memory usage under control, all the decoded changes have
|
|
|
|
to be transmitted when the transaction finally commits (or more precisely,
|
|
|
|
when the commit is decoded from the transaction log). Depending on the
|
|
|
|
size of the transaction and network bandwidth, the transfer time may
|
|
|
|
significantly increase the apply lag.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To reduce the apply lag caused by large transactions, an output plugin
|
|
|
|
may provide additional callback to support incremental streaming of
|
|
|
|
in-progress transactions. There are multiple required streaming callbacks
|
|
|
|
(<function>stream_start_cb</function>, <function>stream_stop_cb</function>,
|
|
|
|
<function>stream_abort_cb</function>, <function>stream_commit_cb</function>
|
2022-04-07 18:13:13 +02:00
|
|
|
and <function>stream_change_cb</function>) and two optional callbacks
|
|
|
|
(<function>stream_message_cb</function> and <function>stream_truncate_cb</function>).
|
2021-08-04 04:17:06 +02:00
|
|
|
Also, if streaming of two-phase commands is to be supported, then additional
|
|
|
|
callbacks must be provided. (See <xref linkend="logicaldecoding-two-phase-commits"/>
|
|
|
|
for details).
|
2020-07-28 04:36:44 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
When streaming an in-progress transaction, the changes (and messages) are
|
|
|
|
streamed in blocks demarcated by <function>stream_start_cb</function>
|
|
|
|
and <function>stream_stop_cb</function> callbacks. Once all the decoded
|
2020-12-30 11:47:26 +01:00
|
|
|
changes are transmitted, the transaction can be committed using the
|
2022-03-15 03:29:23 +01:00
|
|
|
<function>stream_commit_cb</function> callback
|
2020-12-30 11:47:26 +01:00
|
|
|
(or possibly aborted using the <function>stream_abort_cb</function> callback).
|
|
|
|
If two-phase commits are supported, the transaction can be prepared using the
|
2021-02-24 08:13:17 +01:00
|
|
|
<function>stream_prepare_cb</function> callback,
|
|
|
|
<command>COMMIT PREPARED</command> using the
|
2020-12-30 11:47:26 +01:00
|
|
|
<function>commit_prepared_cb</function> callback or aborted using the
|
|
|
|
<function>rollback_prepared_cb</function>.
|
2020-07-28 04:36:44 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
One example sequence of streaming callback calls for one transaction may
|
|
|
|
look like this:
|
|
|
|
<programlisting>
|
|
|
|
stream_start_cb(...); <-- start of first block of changes
|
|
|
|
stream_change_cb(...);
|
|
|
|
stream_change_cb(...);
|
|
|
|
stream_message_cb(...);
|
|
|
|
stream_change_cb(...);
|
|
|
|
...
|
|
|
|
stream_change_cb(...);
|
|
|
|
stream_stop_cb(...); <-- end of first block of changes
|
|
|
|
|
|
|
|
stream_start_cb(...); <-- start of second block of changes
|
|
|
|
stream_change_cb(...);
|
|
|
|
stream_change_cb(...);
|
|
|
|
stream_change_cb(...);
|
|
|
|
...
|
|
|
|
stream_message_cb(...);
|
|
|
|
stream_change_cb(...);
|
|
|
|
stream_stop_cb(...); <-- end of second block of changes
|
|
|
|
|
2021-08-04 04:17:06 +02:00
|
|
|
|
|
|
|
[a. when using normal commit]
|
|
|
|
stream_commit_cb(...); <-- commit of the streamed transaction
|
|
|
|
|
|
|
|
[b. when using two-phase commit]
|
|
|
|
stream_prepare_cb(...); <-- prepare the streamed transaction
|
|
|
|
commit_prepared_cb(...); <-- commit of the prepared transaction
|
2020-07-28 04:36:44 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The actual sequence of callback calls may be more complicated, of course.
|
|
|
|
There may be blocks for multiple streamed transactions, some of the
|
|
|
|
transactions may get aborted, etc.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Similar to spill-to-disk behavior, streaming is triggered when the total
|
|
|
|
amount of changes decoded from the WAL (for all in-progress transactions)
|
2020-12-24 09:05:49 +01:00
|
|
|
exceeds the limit defined by <varname>logical_decoding_work_mem</varname> setting.
|
2021-07-16 10:35:38 +02:00
|
|
|
At that point, the largest top-level transaction (measured by the amount of memory
|
2020-07-28 04:36:44 +02:00
|
|
|
currently used for decoded changes) is selected and streamed. However, in
|
2020-12-24 09:05:49 +01:00
|
|
|
some cases we still have to spill to disk even if streaming is enabled
|
|
|
|
because we exceed the memory threshold but still have not decoded the
|
2020-09-01 00:33:37 +02:00
|
|
|
complete tuple e.g., only decoded toast table insert but not the main table
|
2020-07-28 04:36:44 +02:00
|
|
|
insert.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Even when streaming large transactions, the changes are still applied in
|
|
|
|
commit order, preserving the same guarantees as the non-streaming mode.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
</sect1>
|
2021-01-04 04:04:50 +01:00
|
|
|
|
|
|
|
<sect1 id="logicaldecoding-two-phase-commits">
|
2021-07-01 21:27:39 +02:00
|
|
|
<title>Two-phase Commit Support for Logical Decoding</title>
|
2021-01-04 04:04:50 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
With the basic output plugin callbacks (eg., <function>begin_cb</function>,
|
|
|
|
<function>change_cb</function>, <function>commit_cb</function> and
|
|
|
|
<function>message_cb</function>) two-phase commit commands like
|
|
|
|
<command>PREPARE TRANSACTION</command>, <command>COMMIT PREPARED</command>
|
|
|
|
and <command>ROLLBACK PREPARED</command> are not decoded. While the
|
|
|
|
<command>PREPARE TRANSACTION</command> is ignored,
|
|
|
|
<command>COMMIT PREPARED</command> is decoded as a <command>COMMIT</command>
|
|
|
|
and <command>ROLLBACK PREPARED</command> is decoded as a
|
|
|
|
<command>ROLLBACK</command>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To support the streaming of two-phase commands, an output plugin needs to
|
|
|
|
provide additional callbacks. There are multiple two-phase commit callbacks
|
|
|
|
that are required, (<function>begin_prepare_cb</function>,
|
2021-04-22 22:47:57 +02:00
|
|
|
<function>prepare_cb</function>, <function>commit_prepared_cb</function>,
|
2021-01-04 04:04:50 +01:00
|
|
|
<function>rollback_prepared_cb</function> and
|
|
|
|
<function>stream_prepare_cb</function>) and an optional callback
|
|
|
|
(<function>filter_prepare_cb</function>).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If the output plugin callbacks for decoding two-phase commit commands are
|
|
|
|
provided, then on <command>PREPARE TRANSACTION</command>, the changes of
|
|
|
|
that transaction are decoded, passed to the output plugin, and the
|
|
|
|
<function>prepare_cb</function> callback is invoked. This differs from the
|
|
|
|
basic decoding setup where changes are only passed to the output plugin
|
|
|
|
when a transaction is committed. The start of a prepared transaction is
|
|
|
|
indicated by the <function>begin_prepare_cb</function> callback.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2021-02-24 08:13:17 +01:00
|
|
|
When a prepared transaction is rolled back using the
|
2021-01-04 04:04:50 +01:00
|
|
|
<command>ROLLBACK PREPARED</command>, then the
|
|
|
|
<function>rollback_prepared_cb</function> callback is invoked and when the
|
|
|
|
prepared transaction is committed using <command>COMMIT PREPARED</command>,
|
|
|
|
then the <function>commit_prepared_cb</function> callback is invoked.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2021-03-30 07:04:43 +02:00
|
|
|
Optionally the output plugin can define filtering rules via
|
|
|
|
<function>filter_prepare_cb</function> to decode only specific transaction
|
|
|
|
in two phases. This can be achieved by pattern matching on the
|
|
|
|
<parameter>gid</parameter> or via lookups using the
|
|
|
|
<parameter>xid</parameter>.
|
2021-01-04 04:04:50 +01:00
|
|
|
</para>
|
|
|
|
|
2021-03-01 03:44:33 +01:00
|
|
|
<para>
|
|
|
|
The users that want to decode prepared transactions need to be careful about
|
|
|
|
below mentioned points:
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
If the prepared transaction has locked [user] catalog tables exclusively
|
|
|
|
then decoding prepare can block till the main transaction is committed.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The logical replication solution that builds distributed two phase commit
|
|
|
|
using this feature can deadlock if the prepared transaction has locked
|
2021-06-17 06:26:05 +02:00
|
|
|
[user] catalog tables exclusively. To avoid this users must refrain from
|
|
|
|
having locks on catalog tables (e.g. explicit <command>LOCK</command> command)
|
|
|
|
in such transactions.
|
2021-06-24 05:43:46 +02:00
|
|
|
See <xref linkend="logicaldecoding-synchronous-caveats"/> for the details.
|
2021-03-01 03:44:33 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
|
2021-01-04 04:04:50 +01:00
|
|
|
</sect1>
|
2014-03-18 18:20:01 +01:00
|
|
|
</chapter>
|