pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Logical decoding of sequences
Date: Thu, 10 Feb 2022 17:52:23 +0000
Message-ID: <E1nIDcF-0006db-E0@gemulon.postgresql.org> (raw)

Logical decoding of sequences

This extends the logical decoding to also decode sequence increments.
We differentiate between sequences created in the current (in-progress)
transaction, and sequences created earlier. This mixed behavior is
necessary because while sequences are not transactional (increments are
not subject to ROLLBACK), relfilenode changes are. So we do this:

* Changes for sequences created in the same top-level transaction are
  treated as transactional, i.e. just like any other change from that
  transaction, and discarded in case of a rollback.

* Changes for sequences created earlier are applied immediately, as if
  performed outside any transaction. This applies also after ALTER
  SEQUENCE, which may create a new relfilenode.

Moreover, if we ever get support for DDL replication, the sequence
won't exist until the transaction gets applied.

Sequences created in the current transaction are tracked in a simple
hash table, identified by a relfilenode. That means a sequence may
already exist, but if a transaction does ALTER SEQUENCE then the
increments for the new relfilenode will be treated as transactional.

For each relfilenode we track the XID of (sub)transaction that created
it, which is needed for cleanup at transaction end. We don't need to
check the XID to decide if an increment is transactional - if we find a
match in the hash table, it has to be the same transaction.

This requires two minor changes to WAL-logging. Firstly, we need to
ensure the sequence record has a valid XID - until now the the increment
might have XID 0 if it was the first change in a subxact. But the
sequence might have been created in the same top-level transaction. So
we ensure the XID is assigned when WAL-logging increments.

The other change is addition of "created" flag, marking increments for
newly created relfilenodes. This makes it easier to maintain the hash
table of sequences that need transactional handling.
Note: This is needed because of subxacts. A XID 0 might still have the
sequence created in a different subxact of the same top-level xact.

This does not include any changes to test_decoding and/or the built-in
replication - those will be committed in separate patches.

A patch adding decoding of sequences was originally submitted by Cary
Huang. This commit reworks various important aspects (e.g. the WAL
logging and transactional/non-transactional handling). However, the
original patch and reviews were very useful.

Author: Tomas Vondra, Cary Huang
Reviewed-by: Peter Eisentraut, Hannu Krosing, Andres Freund
Discussion: https://postgr.es/m/d045f3c2-6cfb-06d3-5540-e63c320df8bc@enterprisedb.com
Discussion: https://postgr.es/m/1710ed7e13b.cd7177461430746.3372264562543607781@highgo.ca

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/0da92dc530c9251735fc70b20cd004d9630a1266

Modified Files
--------------
doc/src/sgml/logicaldecoding.sgml               |  65 +++-
src/backend/commands/sequence.c                 |  32 ++
src/backend/replication/logical/decode.c        | 129 ++++++++
src/backend/replication/logical/logical.c       |  88 +++++
src/backend/replication/logical/reorderbuffer.c | 405 ++++++++++++++++++++++++
src/include/access/rmgrlist.h                   |   2 +-
src/include/commands/sequence.h                 |   1 +
src/include/replication/decode.h                |   1 +
src/include/replication/output_plugin.h         |  27 ++
src/include/replication/reorderbuffer.h         |  43 ++-
10 files changed, 786 insertions(+), 7 deletions(-)



Message-ID: <E1nIDcF-0006db-E0@gemulon.postgresql.org>
Permalink:  ../E1nIDcF-0006db-E0@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1nIDcF-0006db-E0@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tomas.vondra@postgresql.org
  Subject: Re: pgsql: Logical decoding of sequences
  In-Reply-To: <E1nIDcF-0006db-E0@gemulon.postgresql.org>
  References: <E1nIDcF-0006db-E0@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox