mirror of
https://github.com/postgres/postgres.git
synced 2025-07-26 01:22:12 +03:00
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
This commit is contained in:
@ -64,7 +64,8 @@ enum ReorderBufferChangeType
|
||||
REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT,
|
||||
REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM,
|
||||
REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT,
|
||||
REORDER_BUFFER_CHANGE_TRUNCATE
|
||||
REORDER_BUFFER_CHANGE_TRUNCATE,
|
||||
REORDER_BUFFER_CHANGE_SEQUENCE
|
||||
};
|
||||
|
||||
/* forward declaration */
|
||||
@ -158,6 +159,13 @@ typedef struct ReorderBufferChange
|
||||
uint32 ninvalidations; /* Number of messages */
|
||||
SharedInvalidationMessage *invalidations; /* invalidation message */
|
||||
} inval;
|
||||
|
||||
/* Context data for Sequence changes */
|
||||
struct
|
||||
{
|
||||
RelFileNode relnode;
|
||||
ReorderBufferTupleBuf *tuple;
|
||||
} sequence;
|
||||
} data;
|
||||
|
||||
/*
|
||||
@ -430,6 +438,15 @@ typedef void (*ReorderBufferMessageCB) (ReorderBuffer *rb,
|
||||
const char *prefix, Size sz,
|
||||
const char *message);
|
||||
|
||||
/* sequence callback signature */
|
||||
typedef void (*ReorderBufferSequenceCB) (ReorderBuffer *rb,
|
||||
ReorderBufferTXN *txn,
|
||||
XLogRecPtr sequence_lsn,
|
||||
Relation rel,
|
||||
bool transactional,
|
||||
int64 last_value, int64 log_cnt,
|
||||
bool is_called);
|
||||
|
||||
/* begin prepare callback signature */
|
||||
typedef void (*ReorderBufferBeginPrepareCB) (ReorderBuffer *rb,
|
||||
ReorderBufferTXN *txn);
|
||||
@ -496,6 +513,15 @@ typedef void (*ReorderBufferStreamMessageCB) (
|
||||
const char *prefix, Size sz,
|
||||
const char *message);
|
||||
|
||||
/* stream sequence callback signature */
|
||||
typedef void (*ReorderBufferStreamSequenceCB) (ReorderBuffer *rb,
|
||||
ReorderBufferTXN *txn,
|
||||
XLogRecPtr sequence_lsn,
|
||||
Relation rel,
|
||||
bool transactional,
|
||||
int64 last_value, int64 log_cnt,
|
||||
bool is_called);
|
||||
|
||||
/* stream truncate callback signature */
|
||||
typedef void (*ReorderBufferStreamTruncateCB) (
|
||||
ReorderBuffer *rb,
|
||||
@ -511,6 +537,12 @@ struct ReorderBuffer
|
||||
*/
|
||||
HTAB *by_txn;
|
||||
|
||||
/*
|
||||
* relfilenode => XID lookup table for sequences created in a transaction
|
||||
* (also includes altered sequences, which assigns new relfilenode)
|
||||
*/
|
||||
HTAB *sequences;
|
||||
|
||||
/*
|
||||
* Transactions that could be a toplevel xact, ordered by LSN of the first
|
||||
* record bearing that xid.
|
||||
@ -541,6 +573,7 @@ struct ReorderBuffer
|
||||
ReorderBufferApplyTruncateCB apply_truncate;
|
||||
ReorderBufferCommitCB commit;
|
||||
ReorderBufferMessageCB message;
|
||||
ReorderBufferSequenceCB sequence;
|
||||
|
||||
/*
|
||||
* Callbacks to be called when streaming a transaction at prepare time.
|
||||
@ -560,6 +593,7 @@ struct ReorderBuffer
|
||||
ReorderBufferStreamCommitCB stream_commit;
|
||||
ReorderBufferStreamChangeCB stream_change;
|
||||
ReorderBufferStreamMessageCB stream_message;
|
||||
ReorderBufferStreamSequenceCB stream_sequence;
|
||||
ReorderBufferStreamTruncateCB stream_truncate;
|
||||
|
||||
/*
|
||||
@ -635,6 +669,10 @@ void ReorderBufferQueueChange(ReorderBuffer *, TransactionId,
|
||||
void ReorderBufferQueueMessage(ReorderBuffer *, TransactionId, Snapshot snapshot, XLogRecPtr lsn,
|
||||
bool transactional, const char *prefix,
|
||||
Size message_size, const char *message);
|
||||
void ReorderBufferQueueSequence(ReorderBuffer *rb, TransactionId xid,
|
||||
Snapshot snapshot, XLogRecPtr lsn, RepOriginId origin_id,
|
||||
RelFileNode rnode, bool transactional, bool created,
|
||||
ReorderBufferTupleBuf *tuplebuf);
|
||||
void ReorderBufferCommit(ReorderBuffer *, TransactionId,
|
||||
XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
|
||||
TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn);
|
||||
@ -682,4 +720,7 @@ void ReorderBufferSetRestartPoint(ReorderBuffer *, XLogRecPtr ptr);
|
||||
|
||||
void StartupReorderBuffer(void);
|
||||
|
||||
bool ReorderBufferSequenceIsTransactional(ReorderBuffer *rb,
|
||||
RelFileNode rnode, bool created);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user