Contents
pg_logical_slot_peek_changes()
pg_logical_slot_peek_changes()
is a system function returning a set of specified changes from the specified logical replication slot without consuming them, meaning the same set of changes can be retrieved again.
pg_logical_slot_peek_changes()
was added in PostgreSQL 9.4.
Usage
pg_logical_slot_peek_changes (slot_name
name
,upto_lsn
pg_lsn
,upto_nchanges
integer
, VARIADICoptions
text
[] ) →
setof record (lsn
pg_lsn
,xid
xid
,data
text
)
The returned set of changes will begin from the replication slot's current restart_lsn
value.
If both upto_lsn
and upto_nchanges
are NULL
, all changes up until the end of available WAL will be returned.
The options
parameter enables provision of options as defined by the output plugin associated with the logical replication slot.
To retrieve changes and advance the replication slot, use pg_logical_slot_get_changes()
or pg_logical_slot_get_binary_changes()
.
Change history
- PostgreSQL 9.4
- added (commit b89e1510)
Examples
Create a logical replication slot with test_decoding
specified as the plugin:
postgres=# SELECT * FROM pg_create_logical_replication_slot('test_slot_1', 'test_decoding'); slot_name | lsn -------------+----------- test_slot_1 | 0/0/301D458 (1 row)
Create some WAL activity, here by inserting a row into an existing table:
postgres=# INSERT INTO foo VALUES (1, clock_timestamp()); INSERT 0 1
This WAL activity can then be repeatedly queried via the previously created logical replication slot using pg_logical_slot_peek_changes()
:
postgres=# SELECT * FROM pg_logical_slot_peek_changes('test_slot_1', NULL, NULL, 'include-xids', '0'); lsn | xid | data -----------+-----+---------------------------------------------------------------------------------- 0/301D458 | 713 | BEGIN 0/301D458 | 713 | table public.foo: INSERT: id[integer]:1 val[text]:'2021-09-05 21:56:25.88791+01' 0/301D4E0 | 713 | COMMIT (3 rows) postgres=# SELECT * FROM pg_logical_slot_peek_changes('test_slot_1', NULL, NULL, 'include-xids', '0'); lsn | xid | data -----------+-----+---------------------------------------------------------------------------------- 0/301D458 | 713 | BEGIN 0/301D458 | 713 | table public.foo: INSERT: id[integer]:1 val[text]:'2021-09-05 21:56:25.88791+01' 0/301D4E0 | 713 | COMMIT (3 rows)
If the available WAL is then consumed, e.g. with pg_logical_slot_get_changes()
, or the replication slot is moved forward (and no other WAL activity takes place), the previously returned data is no longer available:
postgres=# SELECT * FROM pg_replication_slot_advance('test_slot_1', '0/301D4E0'); slot_name | end_lsn -------------+----------- test_slot_1 | 0/301D4E0 (1 row) postgres=# SELECT * FROM pg_logical_slot_peek_changes('test_slot_1', NULL, NULL, 'include-xids', '0'); lsn | xid | data -----+-----+------ (0 rows)
References
- PostgreSQL documentation: Replication Management Functions