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
7259736a6e Implement streaming mode in ReorderBuffer.
contrib/test_decoding/Makefile | 2 +-
contrib/test_decoding/expected/stream.out | 94 +++
contrib/test_decoding/expected/truncate.out | 6 +
contrib/test_decoding/sql/stream.sql | 30 +
contrib/test_decoding/sql/truncate.sql | 1 +
contrib/test_decoding/test_decoding.c | 13 +
doc/src/sgml/logicaldecoding.sgml | 9 +-
doc/src/sgml/test-decoding.sgml | 22 +
src/backend/access/heap/heapam.c | 13 +
src/backend/access/heap/heapam_visibility.c | 42 +-
src/backend/access/index/genam.c | 53 ++
src/backend/access/table/tableam.c | 8 +
src/backend/access/transam/xact.c | 19 +
src/backend/replication/logical/decode.c | 17 +-
src/backend/replication/logical/logical.c | 10 +
src/backend/replication/logical/reorderbuffer.c | 981 +++++++++++++++++++++---
src/include/access/heapam_xlog.h | 1 +
src/include/access/tableam.h | 55 ++
src/include/access/xact.h | 4 +
src/include/replication/logical.h | 1 +
src/include/replication/reorderbuffer.h | 56 +-
21 files changed, 1331 insertions(+), 106 deletions(-)