A function returning changes from a logical replication slot as bytea

pg_logical_slot_get_binary_changes() is a system function returning a set of specified changes from the specified logical replication slot, with the contents of each change returned as bytea.

pg_logical_slot_get_binary_changes() was added in PostgreSQL 9.4.


pg_logical_slot_get_binarychanges ( slot_name name, upto_lsn pg_lsn, upto_nchanges integer, VARIADIC options text[] ) →
        setof record ( lsn pg_lsn, xid xid, data text )

The returned set of changes will begin from the point changes were previously consumed.

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 without consuming them, use pg_logical_slot_peek_binary_changes().

Change history


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/3000758
(1 row)

Create some WAL activity, here by inserting a row into an existing table:

postgres=# INSERT INTO foo VALUES (1, 'foo');

This WAL activity can then be queried via the previously created logical replication slot using pg_logical_slot_get_binary_changes():

postgres=# SELECT * FROM pg_logical_slot_get_binary_changes('test_slot_1', NULL, NULL, 'include-xids', '0');
    lsn    | xid |                                                       data                                                       
 0/492AEB0 | 717 | \x424547494e
 0/492AEB0 | 717 | \x7461626c65207075626c69632e666f6f3a20494e534552543a2069645b696e74656765725d3a312076616c5b746578745d3a27666f6f27
 0/492AF20 | 717 | \x434f4d4d4954
(3 rows)


Logical replication, System function

See also

pg_logical_slot_get_changes(), pg_logical_slot_peek_changes(), pg_logical_slot_peek_binary_changes()