mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +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:
@ -73,6 +73,10 @@ static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
XLogRecPtr message_lsn, bool transactional,
|
||||
const char *prefix, Size message_size, const char *message);
|
||||
static void sequence_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
XLogRecPtr sequence_lsn, Relation rel,
|
||||
bool transactional,
|
||||
int64 last_value, int64 log_cnt, bool is_called);
|
||||
|
||||
/* streaming callbacks */
|
||||
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
@ -90,6 +94,10 @@ static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn
|
||||
static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
XLogRecPtr message_lsn, bool transactional,
|
||||
const char *prefix, Size message_size, const char *message);
|
||||
static void stream_sequence_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
XLogRecPtr sequence_lsn, Relation rel,
|
||||
bool transactional,
|
||||
int64 last_value, int64 log_cnt, bool is_called);
|
||||
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
int nrelations, Relation relations[], ReorderBufferChange *change);
|
||||
|
||||
@ -218,6 +226,7 @@ StartupDecodingContext(List *output_plugin_options,
|
||||
ctx->reorder->apply_truncate = truncate_cb_wrapper;
|
||||
ctx->reorder->commit = commit_cb_wrapper;
|
||||
ctx->reorder->message = message_cb_wrapper;
|
||||
ctx->reorder->sequence = sequence_cb_wrapper;
|
||||
|
||||
/*
|
||||
* To support streaming, we require start/stop/abort/commit/change
|
||||
@ -234,6 +243,7 @@ StartupDecodingContext(List *output_plugin_options,
|
||||
(ctx->callbacks.stream_commit_cb != NULL) ||
|
||||
(ctx->callbacks.stream_change_cb != NULL) ||
|
||||
(ctx->callbacks.stream_message_cb != NULL) ||
|
||||
(ctx->callbacks.stream_sequence_cb != NULL) ||
|
||||
(ctx->callbacks.stream_truncate_cb != NULL);
|
||||
|
||||
/*
|
||||
@ -251,6 +261,7 @@ StartupDecodingContext(List *output_plugin_options,
|
||||
ctx->reorder->stream_commit = stream_commit_cb_wrapper;
|
||||
ctx->reorder->stream_change = stream_change_cb_wrapper;
|
||||
ctx->reorder->stream_message = stream_message_cb_wrapper;
|
||||
ctx->reorder->stream_sequence = stream_sequence_cb_wrapper;
|
||||
ctx->reorder->stream_truncate = stream_truncate_cb_wrapper;
|
||||
|
||||
|
||||
@ -1203,6 +1214,42 @@ message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
error_context_stack = errcallback.previous;
|
||||
}
|
||||
|
||||
static void
|
||||
sequence_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
XLogRecPtr sequence_lsn, Relation rel, bool transactional,
|
||||
int64 last_value, int64 log_cnt, bool is_called)
|
||||
{
|
||||
LogicalDecodingContext *ctx = cache->private_data;
|
||||
LogicalErrorCallbackState state;
|
||||
ErrorContextCallback errcallback;
|
||||
|
||||
Assert(!ctx->fast_forward);
|
||||
|
||||
if (ctx->callbacks.sequence_cb == NULL)
|
||||
return;
|
||||
|
||||
/* Push callback + info on the error context stack */
|
||||
state.ctx = ctx;
|
||||
state.callback_name = "sequence";
|
||||
state.report_location = sequence_lsn;
|
||||
errcallback.callback = output_plugin_error_callback;
|
||||
errcallback.arg = (void *) &state;
|
||||
errcallback.previous = error_context_stack;
|
||||
error_context_stack = &errcallback;
|
||||
|
||||
/* set output state */
|
||||
ctx->accept_writes = true;
|
||||
ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
|
||||
ctx->write_location = sequence_lsn;
|
||||
|
||||
/* do the actual work: call callback */
|
||||
ctx->callbacks.sequence_cb(ctx, txn, sequence_lsn, rel, transactional,
|
||||
last_value, log_cnt, is_called);
|
||||
|
||||
/* Pop the error context stack */
|
||||
error_context_stack = errcallback.previous;
|
||||
}
|
||||
|
||||
static void
|
||||
stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
XLogRecPtr first_lsn)
|
||||
@ -1508,6 +1555,47 @@ stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
error_context_stack = errcallback.previous;
|
||||
}
|
||||
|
||||
static void
|
||||
stream_sequence_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
XLogRecPtr sequence_lsn, Relation rel,
|
||||
bool transactional,
|
||||
int64 last_value, int64 log_cnt, bool is_called)
|
||||
{
|
||||
LogicalDecodingContext *ctx = cache->private_data;
|
||||
LogicalErrorCallbackState state;
|
||||
ErrorContextCallback errcallback;
|
||||
|
||||
Assert(!ctx->fast_forward);
|
||||
|
||||
/* We're only supposed to call this when streaming is supported. */
|
||||
Assert(ctx->streaming);
|
||||
|
||||
/* this callback is optional */
|
||||
if (ctx->callbacks.stream_sequence_cb == NULL)
|
||||
return;
|
||||
|
||||
/* Push callback + info on the error context stack */
|
||||
state.ctx = ctx;
|
||||
state.callback_name = "stream_sequence";
|
||||
state.report_location = sequence_lsn;
|
||||
errcallback.callback = output_plugin_error_callback;
|
||||
errcallback.arg = (void *) &state;
|
||||
errcallback.previous = error_context_stack;
|
||||
error_context_stack = &errcallback;
|
||||
|
||||
/* set output state */
|
||||
ctx->accept_writes = true;
|
||||
ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
|
||||
ctx->write_location = sequence_lsn;
|
||||
|
||||
/* do the actual work: call callback */
|
||||
ctx->callbacks.sequence_cb(ctx, txn, sequence_lsn, rel, transactional,
|
||||
last_value, log_cnt, is_called);
|
||||
|
||||
/* Pop the error context stack */
|
||||
error_context_stack = errcallback.previous;
|
||||
}
|
||||
|
||||
static void
|
||||
stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
|
||||
int nrelations, Relation relations[],
|
||||
|
Reference in New Issue
Block a user