postgresql/src/backend/commands
Tomas Vondra 0da92dc530 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
2022-02-10 18:43:51 +01:00
..
Makefile Move parallel vacuum code to vacuumparallel.c. 2021-12-23 11:42:52 +05:30
aggregatecmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
alter.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
amcmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
analyze.c Add stxdinherit flag to pg_statistic_ext_data 2022-01-16 13:38:01 +01:00
async.c Reduce more the number of calls to GetMaxBackends() 2022-02-10 10:27:29 +09:00
cluster.c Fix various typos, grammar and code style in comments and docs 2022-01-25 09:40:04 +09:00
collationcmds.c Change collate and ctype fields to type text 2022-01-27 08:54:25 +01:00
comment.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
constraint.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
conversioncmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
copy.c Add HEADER support to COPY text format 2022-01-28 09:44:47 +01:00
copyfrom.c Remove redundant memory context switches in BeginCopyFrom(). 2022-01-19 12:31:15 -05:00
copyfromparse.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
copyto.c Fix incorrect memory context switch in COPY TO execution 2022-01-29 10:22:42 +09:00
createas.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
dbcommands.c Some cleanup for change of collate and ctype fields to type text 2022-02-02 11:58:55 +01:00
define.c Add Boolean node 2022-01-17 10:38:23 +01:00
discard.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
dropcmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
event_trigger.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
explain.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
extension.c Improve error message for missing extension. 2022-01-11 14:22:00 -05:00
foreigncmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
functioncmds.c Add Boolean node 2022-01-17 10:38:23 +01:00
indexcmds.c Add UNIQUE null treatment option 2022-02-03 11:48:21 +01:00
lockcmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
matview.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
opclasscmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
operatorcmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
policy.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
portalcmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
prepare.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
proclang.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
publicationcmds.c Fix various typos, grammar and code style in comments and docs 2022-01-25 09:40:04 +09:00
schemacmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
seclabel.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
sequence.c Logical decoding of sequences 2022-02-10 18:43:51 +01:00
statscmds.c Check syscache result in AlterStatistics 2022-01-23 03:16:31 +01:00
subscriptioncmds.c Fix various typos, grammar and code style in comments and docs 2022-01-25 09:40:04 +09:00
tablecmds.c Remove xloginsert.h from xlog.h 2022-01-30 12:25:24 -03:00
tablespace.c pg_upgrade: Preserve relfilenodes and tablespace OIDs. 2022-01-17 13:40:27 -05:00
trigger.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
tsearchcmds.c Add Boolean node 2022-01-17 10:38:23 +01:00
typecmds.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
user.c Add Boolean node 2022-01-17 10:38:23 +01:00
vacuum.c Remove useless inline marker. 2022-01-22 17:11:33 -05:00
vacuumparallel.c Unify VACUUM VERBOSE and autovacuum logging. 2022-01-14 16:50:34 -08:00
variable.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
view.c Update copyright for 2022 2022-01-07 19:04:57 -05:00