pgsql: Implement streaming mode in ReorderBuffer. - Mailing list pgsql-committers

From Amit Kapila
Subject pgsql: Implement streaming mode in ReorderBuffer.
Date
Msg-id E1k4Ebl-0006ps-1P@gemulon.postgresql.org
Whole thread Raw
Responses Re: pgsql: Implement streaming mode in ReorderBuffer.  (Amit Kapila <amit.kapila16@gmail.com>)
Re: pgsql: Implement streaming mode in ReorderBuffer.  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: pgsql: Implement streaming mode in ReorderBuffer.  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-committers
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

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7259736a6e5b7c7588fff9578370736a6648acbb

Modified Files
--------------
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(-)


pgsql-committers by date:

Previous
From: Peter Geoghegan
Date:
Subject: pgsql: Make nbtree split REDO locking match original execution.
Next
From: Amit Kapila
Date:
Subject: Re: pgsql: Implement streaming mode in ReorderBuffer.