| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pgoutput.c |
| 4 |
* Logical Replication output plugin |
| 5 |
* |
| 6 |
* Copyright (c) 2012-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* IDENTIFICATION |
| 9 |
* src/backend/replication/pgoutput/pgoutput.c |
| 10 |
* |
| 11 |
*------------------------------------------------------------------------- |
| 12 |
*/ |
| 13 |
#include "postgres.h" |
| 14 |
|
| 15 |
#include "access/tupconvert.h" |
| 16 |
#include "catalog/partition.h" |
| 17 |
#include "catalog/pg_publication.h" |
| 18 |
#include "catalog/pg_publication_rel.h" |
| 19 |
#include "catalog/pg_subscription.h" |
| 20 |
#include "commands/defrem.h" |
| 21 |
#include "commands/subscriptioncmds.h" |
| 22 |
#include "executor/executor.h" |
| 23 |
#include "fmgr.h" |
| 24 |
#include "nodes/makefuncs.h" |
| 25 |
#include "parser/parse_relation.h" |
| 26 |
#include "replication/logical.h" |
| 27 |
#include "replication/logicalproto.h" |
| 28 |
#include "replication/origin.h" |
| 29 |
#include "replication/pgoutput.h" |
| 30 |
#include "rewrite/rewriteHandler.h" |
| 31 |
#include "utils/builtins.h" |
| 32 |
#include "utils/inval.h" |
| 33 |
#include "utils/lsyscache.h" |
| 34 |
#include "utils/memutils.h" |
| 35 |
#include "utils/rel.h" |
| 36 |
#include "utils/syscache.h" |
| 37 |
#include "utils/varlena.h" |
| 38 |
|
| 39 |
PG_MODULE_MAGIC_EXT( |
| 40 |
.name = "pgoutput", |
| 41 |
.version = PG_VERSION |
| 42 |
); |
| 43 |
|
| 44 |
static void pgoutput_startup(LogicalDecodingContext *ctx, |
| 45 |
OutputPluginOptions *opt, bool is_init); |
| 46 |
static void pgoutput_shutdown(LogicalDecodingContext *ctx); |
| 47 |
static void pgoutput_begin_txn(LogicalDecodingContext *ctx, |
| 48 |
ReorderBufferTXN *txn); |
| 49 |
static void pgoutput_commit_txn(LogicalDecodingContext *ctx, |
| 50 |
ReorderBufferTXN *txn, XLogRecPtr commit_lsn); |
| 51 |
static void pgoutput_change(LogicalDecodingContext *ctx, |
| 52 |
ReorderBufferTXN *txn, Relation relation, |
| 53 |
ReorderBufferChange *change); |
| 54 |
static void pgoutput_truncate(LogicalDecodingContext *ctx, |
| 55 |
ReorderBufferTXN *txn, int nrelations, Relation relations[], |
| 56 |
ReorderBufferChange *change); |
| 57 |
static void pgoutput_message(LogicalDecodingContext *ctx, |
| 58 |
ReorderBufferTXN *txn, XLogRecPtr message_lsn, |
| 59 |
bool transactional, const char *prefix, |
| 60 |
Size sz, const char *message); |
| 61 |
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx, |
| 62 |
RepOriginId origin_id); |
| 63 |
static void pgoutput_begin_prepare_txn(LogicalDecodingContext *ctx, |
| 64 |
ReorderBufferTXN *txn); |
| 65 |
static void pgoutput_prepare_txn(LogicalDecodingContext *ctx, |
| 66 |
ReorderBufferTXN *txn, XLogRecPtr prepare_lsn); |
| 67 |
static void pgoutput_commit_prepared_txn(LogicalDecodingContext *ctx, |
| 68 |
ReorderBufferTXN *txn, XLogRecPtr commit_lsn); |
| 69 |
static void pgoutput_rollback_prepared_txn(LogicalDecodingContext *ctx, |
| 70 |
ReorderBufferTXN *txn, |
| 71 |
XLogRecPtr prepare_end_lsn, |
| 72 |
TimestampTz prepare_time); |
| 73 |
static void pgoutput_stream_start(struct LogicalDecodingContext *ctx, |
| 74 |
ReorderBufferTXN *txn); |
| 75 |
static void pgoutput_stream_stop(struct LogicalDecodingContext *ctx, |
| 76 |
ReorderBufferTXN *txn); |
| 77 |
static void pgoutput_stream_abort(struct LogicalDecodingContext *ctx, |
| 78 |
ReorderBufferTXN *txn, |
| 79 |
XLogRecPtr abort_lsn); |
| 80 |
static void pgoutput_stream_commit(struct LogicalDecodingContext *ctx, |
| 81 |
ReorderBufferTXN *txn, |
| 82 |
XLogRecPtr commit_lsn); |
| 83 |
static void pgoutput_stream_prepare_txn(LogicalDecodingContext *ctx, |
| 84 |
ReorderBufferTXN *txn, XLogRecPtr prepare_lsn); |
| 85 |
|
| 86 |
static bool publications_valid; |
| 87 |
|
| 88 |
static List *LoadPublications(List *pubnames); |
| 89 |
static void publication_invalidation_cb(Datum arg, int cacheid, |
| 90 |
uint32 hashvalue); |
| 91 |
static void send_repl_origin(LogicalDecodingContext *ctx, |
| 92 |
RepOriginId origin_id, XLogRecPtr origin_lsn, |
| 93 |
bool send_origin); |
| 94 |
|
| 95 |
/* |
| 96 |
* Only 3 publication actions are used for row filtering ("insert", "update", |
| 97 |
* "delete"). See RelationSyncEntry.exprstate[]. |
| 98 |
*/ |
| 99 |
enum RowFilterPubAction |
| 100 |
{ |
| 101 |
PUBACTION_INSERT, |
| 102 |
PUBACTION_UPDATE, |
| 103 |
PUBACTION_DELETE, |
| 104 |
}; |
| 105 |
|
| 106 |
#define NUM_ROWFILTER_PUBACTIONS (PUBACTION_DELETE+1) |
| 107 |
|
| 108 |
/* |
| 109 |
* Entry in the map used to remember which relation schemas we sent. |
| 110 |
* |
| 111 |
* The schema_sent flag determines if the current schema record for the |
| 112 |
* relation (and for its ancestor if publish_as_relid is set) was already |
| 113 |
* sent to the subscriber (in which case we don't need to send it again). |
| 114 |
* |
| 115 |
* The schema cache on downstream is however updated only at commit time, |
| 116 |
* and with streamed transactions the commit order may be different from |
| 117 |
* the order the transactions are sent in. Also, the (sub) transactions |
| 118 |
* might get aborted so we need to send the schema for each (sub) transaction |
| 119 |
* so that we don't lose the schema information on abort. For handling this, |
| 120 |
* we maintain the list of xids (streamed_txns) for those we have already sent |
| 121 |
* the schema. |
| 122 |
* |
| 123 |
* For partitions, 'pubactions' considers not only the table's own |
| 124 |
* publications, but also those of all of its ancestors. |
| 125 |
*/ |
| 126 |
typedef struct RelationSyncEntry |
| 127 |
{ |
| 128 |
Oid relid; /* relation oid */ |
| 129 |
|
| 130 |
bool replicate_valid; /* overall validity flag for entry */ |
| 131 |
|
| 132 |
bool schema_sent; |
| 133 |
|
| 134 |
/* |
| 135 |
* This will be PUBLISH_GENCOLS_STORED if the relation contains generated |
| 136 |
* columns and the 'publish_generated_columns' parameter is set to |
| 137 |
* PUBLISH_GENCOLS_STORED. Otherwise, it will be PUBLISH_GENCOLS_NONE, |
| 138 |
* indicating that no generated columns should be published, unless |
| 139 |
* explicitly specified in the column list. |
| 140 |
*/ |
| 141 |
PublishGencolsType include_gencols_type; |
| 142 |
List *streamed_txns; /* streamed toplevel transactions with this |
| 143 |
* schema */ |
| 144 |
|
| 145 |
/* are we publishing this rel? */ |
| 146 |
PublicationActions pubactions; |
| 147 |
|
| 148 |
/* |
| 149 |
* ExprState array for row filter. Different publication actions don't |
| 150 |
* allow multiple expressions to always be combined into one, because |
| 151 |
* updates or deletes restrict the column in expression to be part of the |
| 152 |
* replica identity index whereas inserts do not have this restriction, so |
| 153 |
* there is one ExprState per publication action. |
| 154 |
*/ |
| 155 |
ExprState *exprstate[NUM_ROWFILTER_PUBACTIONS]; |
| 156 |
EState *estate; /* executor state used for row filter */ |
| 157 |
TupleTableSlot *new_slot; /* slot for storing new tuple */ |
| 158 |
TupleTableSlot *old_slot; /* slot for storing old tuple */ |
| 159 |
|
| 160 |
/* |
| 161 |
* OID of the relation to publish changes as. For a partition, this may |
| 162 |
* be set to one of its ancestors whose schema will be used when |
| 163 |
* replicating changes, if publish_via_partition_root is set for the |
| 164 |
* publication. |
| 165 |
*/ |
| 166 |
Oid publish_as_relid; |
| 167 |
|
| 168 |
/* |
| 169 |
* Map used when replicating using an ancestor's schema to convert tuples |
| 170 |
* from partition's type to the ancestor's; NULL if publish_as_relid is |
| 171 |
* same as 'relid' or if unnecessary due to partition and the ancestor |
| 172 |
* having identical TupleDesc. |
| 173 |
*/ |
| 174 |
AttrMap *attrmap; |
| 175 |
|
| 176 |
/* |
| 177 |
* Columns included in the publication, or NULL if all columns are |
| 178 |
* included implicitly. Note that the attnums in this bitmap are not |
| 179 |
* shifted by FirstLowInvalidHeapAttributeNumber. |
| 180 |
*/ |
| 181 |
Bitmapset *columns; |
| 182 |
|
| 183 |
/* |
| 184 |
* Private context to store additional data for this entry - state for the |
| 185 |
* row filter expressions, column list, etc. |
| 186 |
*/ |
| 187 |
MemoryContext entry_cxt; |
| 188 |
} RelationSyncEntry; |
| 189 |
|
| 190 |
/* |
| 191 |
* Maintain a per-transaction level variable to track whether the transaction |
| 192 |
* has sent BEGIN. BEGIN is only sent when the first change in a transaction |
| 193 |
* is processed. This makes it possible to skip sending a pair of BEGIN/COMMIT |
| 194 |
* messages for empty transactions which saves network bandwidth. |
| 195 |
* |
| 196 |
* This optimization is not used for prepared transactions because if the |
| 197 |
* WALSender restarts after prepare of a transaction and before commit prepared |
| 198 |
* of the same transaction then we won't be able to figure out if we have |
| 199 |
* skipped sending BEGIN/PREPARE of a transaction as it was empty. This is |
| 200 |
* because we would have lost the in-memory txndata information that was |
| 201 |
* present prior to the restart. This will result in sending a spurious |
| 202 |
* COMMIT PREPARED without a corresponding prepared transaction at the |
| 203 |
* downstream which would lead to an error when it tries to process it. |
| 204 |
* |
| 205 |
* XXX We could achieve this optimization by changing protocol to send |
| 206 |
* additional information so that downstream can detect that the corresponding |
| 207 |
* prepare has not been sent. However, adding such a check for every |
| 208 |
* transaction in the downstream could be costly so we might want to do it |
| 209 |
* optionally. |
| 210 |
* |
| 211 |
* We also don't have this optimization for streamed transactions because |
| 212 |
* they can contain prepared transactions. |
| 213 |
*/ |
| 214 |
typedef struct PGOutputTxnData |
| 215 |
{ |
| 216 |
bool sent_begin_txn; /* flag indicating whether BEGIN has been sent */ |
| 217 |
} PGOutputTxnData; |
| 218 |
|
| 219 |
/* Map used to remember which relation schemas we sent. */ |
| 220 |
static HTAB *RelationSyncCache = NULL; |
| 221 |
|
| 222 |
static void init_rel_sync_cache(MemoryContext cachectx); |
| 223 |
static void cleanup_rel_sync_cache(TransactionId xid, bool is_commit); |
| 224 |
static RelationSyncEntry *get_rel_sync_entry(PGOutputData *data, |
| 225 |
Relation relation); |
| 226 |
static void send_relation_and_attrs(Relation relation, TransactionId xid, |
| 227 |
LogicalDecodingContext *ctx, |
| 228 |
RelationSyncEntry *relentry); |
| 229 |
static void rel_sync_cache_relation_cb(Datum arg, Oid relid); |
| 230 |
static void rel_sync_cache_publication_cb(Datum arg, int cacheid, |
| 231 |
uint32 hashvalue); |
| 232 |
static void set_schema_sent_in_streamed_txn(RelationSyncEntry *entry, |
| 233 |
TransactionId xid); |
| 234 |
static bool get_schema_sent_in_streamed_txn(RelationSyncEntry *entry, |
| 235 |
TransactionId xid); |
| 236 |
static void init_tuple_slot(PGOutputData *data, Relation relation, |
| 237 |
RelationSyncEntry *entry); |
| 238 |
static void pgoutput_memory_context_reset(void *arg); |
| 239 |
|
| 240 |
/* row filter routines */ |
| 241 |
static EState *create_estate_for_relation(Relation rel); |
| 242 |
static void pgoutput_row_filter_init(PGOutputData *data, |
| 243 |
List *publications, |
| 244 |
RelationSyncEntry *entry); |
| 245 |
static bool pgoutput_row_filter_exec_expr(ExprState *state, |
| 246 |
ExprContext *econtext); |
| 247 |
static bool pgoutput_row_filter(Relation relation, TupleTableSlot *old_slot, |
| 248 |
TupleTableSlot **new_slot_ptr, |
| 249 |
RelationSyncEntry *entry, |
| 250 |
ReorderBufferChangeType *action); |
| 251 |
|
| 252 |
/* column list routines */ |
| 253 |
static void pgoutput_column_list_init(PGOutputData *data, |
| 254 |
List *publications, |
| 255 |
RelationSyncEntry *entry); |
| 256 |
|
| 257 |
/* |
| 258 |
* Specify output plugin callbacks |
| 259 |
*/ |
| 260 |
void |
| 261 |
_PG_output_plugin_init(OutputPluginCallbacks *cb) |
| 262 |
{ |
| 263 |
cb->startup_cb = pgoutput_startup; |
| 264 |
cb->begin_cb = pgoutput_begin_txn; |
| 265 |
cb->change_cb = pgoutput_change; |
| 266 |
cb->truncate_cb = pgoutput_truncate; |
| 267 |
cb->message_cb = pgoutput_message; |
| 268 |
cb->commit_cb = pgoutput_commit_txn; |
| 269 |
|
| 270 |
cb->begin_prepare_cb = pgoutput_begin_prepare_txn; |
| 271 |
cb->prepare_cb = pgoutput_prepare_txn; |
| 272 |
cb->commit_prepared_cb = pgoutput_commit_prepared_txn; |
| 273 |
cb->rollback_prepared_cb = pgoutput_rollback_prepared_txn; |
| 274 |
cb->filter_by_origin_cb = pgoutput_origin_filter; |
| 275 |
cb->shutdown_cb = pgoutput_shutdown; |
| 276 |
|
| 277 |
/* transaction streaming */ |
| 278 |
cb->stream_start_cb = pgoutput_stream_start; |
| 279 |
cb->stream_stop_cb = pgoutput_stream_stop; |
| 280 |
cb->stream_abort_cb = pgoutput_stream_abort; |
| 281 |
cb->stream_commit_cb = pgoutput_stream_commit; |
| 282 |
cb->stream_change_cb = pgoutput_change; |
| 283 |
cb->stream_message_cb = pgoutput_message; |
| 284 |
cb->stream_truncate_cb = pgoutput_truncate; |
| 285 |
/* transaction streaming - two-phase commit */ |
| 286 |
cb->stream_prepare_cb = pgoutput_stream_prepare_txn; |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
parse_output_parameters(List *options, PGOutputData *data) |
| 291 |
{ |
| 292 |
ListCell *lc; |
| 293 |
bool protocol_version_given = false; |
| 294 |
bool publication_names_given = false; |
| 295 |
bool binary_option_given = false; |
| 296 |
bool messages_option_given = false; |
| 297 |
bool streaming_given = false; |
| 298 |
bool two_phase_option_given = false; |
| 299 |
bool origin_option_given = false; |
| 300 |
|
| 301 |
data->binary = false; |
| 302 |
data->streaming = LOGICALREP_STREAM_OFF; |
| 303 |
data->messages = false; |
| 304 |
data->two_phase = false; |
| 305 |
|
| 306 |
foreach(lc, options) |
| 307 |
{ |
| 308 |
DefElem *defel = (DefElem *) lfirst(lc); |
| 309 |
|
| 310 |
Assert(defel->arg == NULL || IsA(defel->arg, String)); |
| 311 |
|
| 312 |
/* Check each param, whether or not we recognize it */ |
| 313 |
if (strcmp(defel->defname, "proto_version") == 0) |
| 314 |
{ |
| 315 |
unsigned long parsed; |
| 316 |
char *endptr; |
| 317 |
|
| 318 |
if (protocol_version_given) |
| 319 |
ereport(ERROR, |
| 320 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 321 |
errmsg("conflicting or redundant options"))); |
| 322 |
protocol_version_given = true; |
| 323 |
|
| 324 |
errno = 0; |
| 325 |
parsed = strtoul(strVal(defel->arg), &endptr, 10); |
| 326 |
if (errno != 0 || *endptr != '\0') |
| 327 |
ereport(ERROR, |
| 328 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 329 |
errmsg("invalid proto_version"))); |
| 330 |
|
| 331 |
if (parsed > PG_UINT32_MAX) |
| 332 |
ereport(ERROR, |
| 333 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 334 |
errmsg("proto_version \"%s\" out of range", |
| 335 |
strVal(defel->arg)))); |
| 336 |
|
| 337 |
data->protocol_version = (uint32) parsed; |
| 338 |
} |
| 339 |
else if (strcmp(defel->defname, "publication_names") == 0) |
| 340 |
{ |
| 341 |
if (publication_names_given) |
| 342 |
ereport(ERROR, |
| 343 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 344 |
errmsg("conflicting or redundant options"))); |
| 345 |
publication_names_given = true; |
| 346 |
|
| 347 |
/* |
| 348 |
* Pass a copy of the DefElem->arg since SplitIdentifierString |
| 349 |
* modifies its input. |
| 350 |
*/ |
| 351 |
if (!SplitIdentifierString(pstrdup(strVal(defel->arg)), ',', |
| 352 |
&data->publication_names)) |
| 353 |
ereport(ERROR, |
| 354 |
(errcode(ERRCODE_INVALID_NAME), |
| 355 |
errmsg("invalid publication_names syntax"))); |
| 356 |
} |
| 357 |
else if (strcmp(defel->defname, "binary") == 0) |
| 358 |
{ |
| 359 |
if (binary_option_given) |
| 360 |
ereport(ERROR, |
| 361 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 362 |
errmsg("conflicting or redundant options"))); |
| 363 |
binary_option_given = true; |
| 364 |
|
| 365 |
data->binary = defGetBoolean(defel); |
| 366 |
} |
| 367 |
else if (strcmp(defel->defname, "messages") == 0) |
| 368 |
{ |
| 369 |
if (messages_option_given) |
| 370 |
ereport(ERROR, |
| 371 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 372 |
errmsg("conflicting or redundant options"))); |
| 373 |
messages_option_given = true; |
| 374 |
|
| 375 |
data->messages = defGetBoolean(defel); |
| 376 |
} |
| 377 |
else if (strcmp(defel->defname, "streaming") == 0) |
| 378 |
{ |
| 379 |
if (streaming_given) |
| 380 |
ereport(ERROR, |
| 381 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 382 |
errmsg("conflicting or redundant options"))); |
| 383 |
streaming_given = true; |
| 384 |
|
| 385 |
data->streaming = defGetStreamingMode(defel); |
| 386 |
} |
| 387 |
else if (strcmp(defel->defname, "two_phase") == 0) |
| 388 |
{ |
| 389 |
if (two_phase_option_given) |
| 390 |
ereport(ERROR, |
| 391 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 392 |
errmsg("conflicting or redundant options"))); |
| 393 |
two_phase_option_given = true; |
| 394 |
|
| 395 |
data->two_phase = defGetBoolean(defel); |
| 396 |
} |
| 397 |
else if (strcmp(defel->defname, "origin") == 0) |
| 398 |
{ |
| 399 |
char *origin; |
| 400 |
|
| 401 |
if (origin_option_given) |
| 402 |
ereport(ERROR, |
| 403 |
errcode(ERRCODE_SYNTAX_ERROR), |
| 404 |
errmsg("conflicting or redundant options")); |
| 405 |
origin_option_given = true; |
| 406 |
|
| 407 |
origin = defGetString(defel); |
| 408 |
if (pg_strcasecmp(origin, LOGICALREP_ORIGIN_NONE) == 0) |
| 409 |
data->publish_no_origin = true; |
| 410 |
else if (pg_strcasecmp(origin, LOGICALREP_ORIGIN_ANY) == 0) |
| 411 |
data->publish_no_origin = false; |
| 412 |
else |
| 413 |
ereport(ERROR, |
| 414 |
errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 415 |
errmsg("unrecognized origin value: \"%s\"", origin)); |
| 416 |
} |
| 417 |
else |
| 418 |
elog(ERROR, "unrecognized pgoutput option: %s", defel->defname); |
| 419 |
} |
| 420 |
|
| 421 |
/* Check required options */ |
| 422 |
if (!protocol_version_given) |
| 423 |
ereport(ERROR, |
| 424 |
errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 425 |
errmsg("option \"%s\" missing", "proto_version")); |
| 426 |
if (!publication_names_given) |
| 427 |
ereport(ERROR, |
| 428 |
errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 429 |
errmsg("option \"%s\" missing", "publication_names")); |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
* Memory context reset callback of PGOutputData->context. |
| 434 |
*/ |
| 435 |
static void |
| 436 |
pgoutput_memory_context_reset(void *arg) |
| 437 |
{ |
| 438 |
if (RelationSyncCache) |
| 439 |
{ |
| 440 |
hash_destroy(RelationSyncCache); |
| 441 |
RelationSyncCache = NULL; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
/* |
| 446 |
* Initialize this plugin |
| 447 |
*/ |
| 448 |
static void |
| 449 |
pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, |
| 450 |
bool is_init) |
| 451 |
{ |
| 452 |
PGOutputData *data = palloc0(sizeof(PGOutputData)); |
| 453 |
static bool publication_callback_registered = false; |
| 454 |
MemoryContextCallback *mcallback; |
| 455 |
|
| 456 |
/* Create our memory context for private allocations. */ |
| 457 |
data->context = AllocSetContextCreate(ctx->context, |
| 458 |
"logical replication output context", |
| 459 |
ALLOCSET_DEFAULT_SIZES); |
| 460 |
|
| 461 |
data->cachectx = AllocSetContextCreate(ctx->context, |
| 462 |
"logical replication cache context", |
| 463 |
ALLOCSET_DEFAULT_SIZES); |
| 464 |
|
| 465 |
data->pubctx = AllocSetContextCreate(ctx->context, |
| 466 |
"logical replication publication list context", |
| 467 |
ALLOCSET_SMALL_SIZES); |
| 468 |
|
| 469 |
/* |
| 470 |
* Ensure to cleanup RelationSyncCache even when logical decoding invoked |
| 471 |
* via SQL interface ends up with an error. |
| 472 |
*/ |
| 473 |
mcallback = palloc0(sizeof(MemoryContextCallback)); |
| 474 |
mcallback->func = pgoutput_memory_context_reset; |
| 475 |
MemoryContextRegisterResetCallback(ctx->context, mcallback); |
| 476 |
|
| 477 |
ctx->output_plugin_private = data; |
| 478 |
|
| 479 |
/* This plugin uses binary protocol. */ |
| 480 |
opt->output_type = OUTPUT_PLUGIN_BINARY_OUTPUT; |
| 481 |
|
| 482 |
/* |
| 483 |
* This is replication start and not slot initialization. |
| 484 |
* |
| 485 |
* Parse and validate options passed by the client. |
| 486 |
*/ |
| 487 |
if (!is_init) |
| 488 |
{ |
| 489 |
/* Parse the params and ERROR if we see any we don't recognize */ |
| 490 |
parse_output_parameters(ctx->output_plugin_options, data); |
| 491 |
|
| 492 |
/* Check if we support requested protocol */ |
| 493 |
if (data->protocol_version > LOGICALREP_PROTO_MAX_VERSION_NUM) |
| 494 |
ereport(ERROR, |
| 495 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 496 |
errmsg("client sent proto_version=%d but server only supports protocol %d or lower", |
| 497 |
data->protocol_version, LOGICALREP_PROTO_MAX_VERSION_NUM))); |
| 498 |
|
| 499 |
if (data->protocol_version < LOGICALREP_PROTO_MIN_VERSION_NUM) |
| 500 |
ereport(ERROR, |
| 501 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 502 |
errmsg("client sent proto_version=%d but server only supports protocol %d or higher", |
| 503 |
data->protocol_version, LOGICALREP_PROTO_MIN_VERSION_NUM))); |
| 504 |
|
| 505 |
/* |
| 506 |
* Decide whether to enable streaming. It is disabled by default, in |
| 507 |
* which case we just update the flag in decoding context. Otherwise |
| 508 |
* we only allow it with sufficient version of the protocol, and when |
| 509 |
* the output plugin supports it. |
| 510 |
*/ |
| 511 |
if (data->streaming == LOGICALREP_STREAM_OFF) |
| 512 |
ctx->streaming = false; |
| 513 |
else if (data->streaming == LOGICALREP_STREAM_ON && |
| 514 |
data->protocol_version < LOGICALREP_PROTO_STREAM_VERSION_NUM) |
| 515 |
ereport(ERROR, |
| 516 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 517 |
errmsg("requested proto_version=%d does not support streaming, need %d or higher", |
| 518 |
data->protocol_version, LOGICALREP_PROTO_STREAM_VERSION_NUM))); |
| 519 |
else if (data->streaming == LOGICALREP_STREAM_PARALLEL && |
| 520 |
data->protocol_version < LOGICALREP_PROTO_STREAM_PARALLEL_VERSION_NUM) |
| 521 |
ereport(ERROR, |
| 522 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 523 |
errmsg("requested proto_version=%d does not support parallel streaming, need %d or higher", |
| 524 |
data->protocol_version, LOGICALREP_PROTO_STREAM_PARALLEL_VERSION_NUM))); |
| 525 |
else if (!ctx->streaming) |
| 526 |
ereport(ERROR, |
| 527 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 528 |
errmsg("streaming requested, but not supported by output plugin"))); |
| 529 |
|
| 530 |
/* |
| 531 |
* Here, we just check whether the two-phase option is passed by |
| 532 |
* plugin and decide whether to enable it at later point of time. It |
| 533 |
* remains enabled if the previous start-up has done so. But we only |
| 534 |
* allow the option to be passed in with sufficient version of the |
| 535 |
* protocol, and when the output plugin supports it. |
| 536 |
*/ |
| 537 |
if (!data->two_phase) |
| 538 |
ctx->twophase_opt_given = false; |
| 539 |
else if (data->protocol_version < LOGICALREP_PROTO_TWOPHASE_VERSION_NUM) |
| 540 |
ereport(ERROR, |
| 541 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 542 |
errmsg("requested proto_version=%d does not support two-phase commit, need %d or higher", |
| 543 |
data->protocol_version, LOGICALREP_PROTO_TWOPHASE_VERSION_NUM))); |
| 544 |
else if (!ctx->twophase) |
| 545 |
ereport(ERROR, |
| 546 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 547 |
errmsg("two-phase commit requested, but not supported by output plugin"))); |
| 548 |
else |
| 549 |
ctx->twophase_opt_given = true; |
| 550 |
|
| 551 |
/* Init publication state. */ |
| 552 |
data->publications = NIL; |
| 553 |
publications_valid = false; |
| 554 |
|
| 555 |
/* |
| 556 |
* Register callback for pg_publication if we didn't already do that |
| 557 |
* during some previous call in this process. |
| 558 |
*/ |
| 559 |
if (!publication_callback_registered) |
| 560 |
{ |
| 561 |
CacheRegisterSyscacheCallback(PUBLICATIONOID, |
| 562 |
publication_invalidation_cb, |
| 563 |
(Datum) 0); |
| 564 |
CacheRegisterRelSyncCallback(rel_sync_cache_relation_cb, |
| 565 |
(Datum) 0); |
| 566 |
publication_callback_registered = true; |
| 567 |
} |
| 568 |
|
| 569 |
/* Initialize relation schema cache. */ |
| 570 |
init_rel_sync_cache(CacheMemoryContext); |
| 571 |
} |
| 572 |
else |
| 573 |
{ |
| 574 |
/* |
| 575 |
* Disable the streaming and prepared transactions during the slot |
| 576 |
* initialization mode. |
| 577 |
*/ |
| 578 |
ctx->streaming = false; |
| 579 |
ctx->twophase = false; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
/* |
| 584 |
* BEGIN callback. |
| 585 |
* |
| 586 |
* Don't send the BEGIN message here instead postpone it until the first |
| 587 |
* change. In logical replication, a common scenario is to replicate a set of |
| 588 |
* tables (instead of all tables) and transactions whose changes were on |
| 589 |
* the table(s) that are not published will produce empty transactions. These |
| 590 |
* empty transactions will send BEGIN and COMMIT messages to subscribers, |
| 591 |
* using bandwidth on something with little/no use for logical replication. |
| 592 |
*/ |
| 593 |
static void |
| 594 |
pgoutput_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn) |
| 595 |
{ |
| 596 |
PGOutputTxnData *txndata = MemoryContextAllocZero(ctx->context, |
| 597 |
sizeof(PGOutputTxnData)); |
| 598 |
|
| 599 |
txn->output_plugin_private = txndata; |
| 600 |
} |
| 601 |
|
| 602 |
/* |
| 603 |
* Send BEGIN. |
| 604 |
* |
| 605 |
* This is called while processing the first change of the transaction. |
| 606 |
*/ |
| 607 |
static void |
| 608 |
pgoutput_send_begin(LogicalDecodingContext *ctx, ReorderBufferTXN *txn) |
| 609 |
{ |
| 610 |
bool send_replication_origin = txn->origin_id != InvalidRepOriginId; |
| 611 |
PGOutputTxnData *txndata = (PGOutputTxnData *) txn->output_plugin_private; |
| 612 |
|
| 613 |
Assert(txndata); |
| 614 |
Assert(!txndata->sent_begin_txn); |
| 615 |
|
| 616 |
OutputPluginPrepareWrite(ctx, !send_replication_origin); |
| 617 |
logicalrep_write_begin(ctx->out, txn); |
| 618 |
txndata->sent_begin_txn = true; |
| 619 |
|
| 620 |
send_repl_origin(ctx, txn->origin_id, txn->origin_lsn, |
| 621 |
send_replication_origin); |
| 622 |
|
| 623 |
OutputPluginWrite(ctx, true); |
| 624 |
} |
| 625 |
|
| 626 |
/* |
| 627 |
* COMMIT callback |
| 628 |
*/ |
| 629 |
static void |
| 630 |
pgoutput_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, |
| 631 |
XLogRecPtr commit_lsn) |
| 632 |
{ |
| 633 |
PGOutputTxnData *txndata = (PGOutputTxnData *) txn->output_plugin_private; |
| 634 |
bool sent_begin_txn; |
| 635 |
|
| 636 |
Assert(txndata); |
| 637 |
|
| 638 |
/* |
| 639 |
* We don't need to send the commit message unless some relevant change |
| 640 |
* from this transaction has been sent to the downstream. |
| 641 |
*/ |
| 642 |
sent_begin_txn = txndata->sent_begin_txn; |
| 643 |
OutputPluginUpdateProgress(ctx, !sent_begin_txn); |
| 644 |
pfree(txndata); |
| 645 |
txn->output_plugin_private = NULL; |
| 646 |
|
| 647 |
if (!sent_begin_txn) |
| 648 |
{ |
| 649 |
elog(DEBUG1, "skipped replication of an empty transaction with XID: %u", txn->xid); |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
OutputPluginPrepareWrite(ctx, true); |
| 654 |
logicalrep_write_commit(ctx->out, txn, commit_lsn); |
| 655 |
OutputPluginWrite(ctx, true); |
| 656 |
} |
| 657 |
|
| 658 |
/* |
| 659 |
* BEGIN PREPARE callback |
| 660 |
*/ |
| 661 |
static void |
| 662 |
pgoutput_begin_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn) |
| 663 |
{ |
| 664 |
bool send_replication_origin = txn->origin_id != InvalidRepOriginId; |
| 665 |
|
| 666 |
OutputPluginPrepareWrite(ctx, !send_replication_origin); |
| 667 |
logicalrep_write_begin_prepare(ctx->out, txn); |
| 668 |
|
| 669 |
send_repl_origin(ctx, txn->origin_id, txn->origin_lsn, |
| 670 |
send_replication_origin); |
| 671 |
|
| 672 |
OutputPluginWrite(ctx, true); |
| 673 |
} |
| 674 |
|
| 675 |
/* |
| 676 |
* PREPARE callback |
| 677 |
*/ |
| 678 |
static void |
| 679 |
pgoutput_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, |
| 680 |
XLogRecPtr prepare_lsn) |
| 681 |
{ |
| 682 |
OutputPluginUpdateProgress(ctx, false); |
| 683 |
|
| 684 |
OutputPluginPrepareWrite(ctx, true); |
| 685 |
logicalrep_write_prepare(ctx->out, txn, prepare_lsn); |
| 686 |
OutputPluginWrite(ctx, true); |
| 687 |
} |
| 688 |
|
| 689 |
/* |
| 690 |
* COMMIT PREPARED callback |
| 691 |
*/ |
| 692 |
static void |
| 693 |
pgoutput_commit_prepared_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, |
| 694 |
XLogRecPtr commit_lsn) |
| 695 |
{ |
| 696 |
OutputPluginUpdateProgress(ctx, false); |
| 697 |
|
| 698 |
OutputPluginPrepareWrite(ctx, true); |
| 699 |
logicalrep_write_commit_prepared(ctx->out, txn, commit_lsn); |
| 700 |
OutputPluginWrite(ctx, true); |
| 701 |
} |
| 702 |
|
| 703 |
/* |
| 704 |
* ROLLBACK PREPARED callback |
| 705 |
*/ |
| 706 |
static void |
| 707 |
pgoutput_rollback_prepared_txn(LogicalDecodingContext *ctx, |
| 708 |
ReorderBufferTXN *txn, |
| 709 |
XLogRecPtr prepare_end_lsn, |
| 710 |
TimestampTz prepare_time) |
| 711 |
{ |
| 712 |
OutputPluginUpdateProgress(ctx, false); |
| 713 |
|
| 714 |
OutputPluginPrepareWrite(ctx, true); |
| 715 |
logicalrep_write_rollback_prepared(ctx->out, txn, prepare_end_lsn, |
| 716 |
prepare_time); |
| 717 |
OutputPluginWrite(ctx, true); |
| 718 |
} |
| 719 |
|
| 720 |
/* |
| 721 |
* Write the current schema of the relation and its ancestor (if any) if not |
| 722 |
* done yet. |
| 723 |
*/ |
| 724 |
static void |
| 725 |
maybe_send_schema(LogicalDecodingContext *ctx, |
| 726 |
ReorderBufferChange *change, |
| 727 |
Relation relation, RelationSyncEntry *relentry) |
| 728 |
{ |
| 729 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 730 |
bool schema_sent; |
| 731 |
TransactionId xid = InvalidTransactionId; |
| 732 |
TransactionId topxid = InvalidTransactionId; |
| 733 |
|
| 734 |
/* |
| 735 |
* Remember XID of the (sub)transaction for the change. We don't care if |
| 736 |
* it's top-level transaction or not (we have already sent that XID in |
| 737 |
* start of the current streaming block). |
| 738 |
* |
| 739 |
* If we're not in a streaming block, just use InvalidTransactionId and |
| 740 |
* the write methods will not include it. |
| 741 |
*/ |
| 742 |
if (data->in_streaming) |
| 743 |
xid = change->txn->xid; |
| 744 |
|
| 745 |
if (rbtxn_is_subtxn(change->txn)) |
| 746 |
topxid = rbtxn_get_toptxn(change->txn)->xid; |
| 747 |
else |
| 748 |
topxid = xid; |
| 749 |
|
| 750 |
/* |
| 751 |
* Do we need to send the schema? We do track streamed transactions |
| 752 |
* separately, because those may be applied later (and the regular |
| 753 |
* transactions won't see their effects until then) and in an order that |
| 754 |
* we don't know at this point. |
| 755 |
* |
| 756 |
* XXX There is a scope of optimization here. Currently, we always send |
| 757 |
* the schema first time in a streaming transaction but we can probably |
| 758 |
* avoid that by checking 'relentry->schema_sent' flag. However, before |
| 759 |
* doing that we need to study its impact on the case where we have a mix |
| 760 |
* of streaming and non-streaming transactions. |
| 761 |
*/ |
| 762 |
if (data->in_streaming) |
| 763 |
schema_sent = get_schema_sent_in_streamed_txn(relentry, topxid); |
| 764 |
else |
| 765 |
schema_sent = relentry->schema_sent; |
| 766 |
|
| 767 |
/* Nothing to do if we already sent the schema. */ |
| 768 |
if (schema_sent) |
| 769 |
return; |
| 770 |
|
| 771 |
/* |
| 772 |
* Send the schema. If the changes will be published using an ancestor's |
| 773 |
* schema, not the relation's own, send that ancestor's schema before |
| 774 |
* sending relation's own (XXX - maybe sending only the former suffices?). |
| 775 |
*/ |
| 776 |
if (relentry->publish_as_relid != RelationGetRelid(relation)) |
| 777 |
{ |
| 778 |
Relation ancestor = RelationIdGetRelation(relentry->publish_as_relid); |
| 779 |
|
| 780 |
send_relation_and_attrs(ancestor, xid, ctx, relentry); |
| 781 |
RelationClose(ancestor); |
| 782 |
} |
| 783 |
|
| 784 |
send_relation_and_attrs(relation, xid, ctx, relentry); |
| 785 |
|
| 786 |
if (data->in_streaming) |
| 787 |
set_schema_sent_in_streamed_txn(relentry, topxid); |
| 788 |
else |
| 789 |
relentry->schema_sent = true; |
| 790 |
} |
| 791 |
|
| 792 |
/* |
| 793 |
* Sends a relation |
| 794 |
*/ |
| 795 |
static void |
| 796 |
send_relation_and_attrs(Relation relation, TransactionId xid, |
| 797 |
LogicalDecodingContext *ctx, |
| 798 |
RelationSyncEntry *relentry) |
| 799 |
{ |
| 800 |
TupleDesc desc = RelationGetDescr(relation); |
| 801 |
Bitmapset *columns = relentry->columns; |
| 802 |
PublishGencolsType include_gencols_type = relentry->include_gencols_type; |
| 803 |
int i; |
| 804 |
|
| 805 |
/* |
| 806 |
* Write out type info if needed. We do that only for user-created types. |
| 807 |
* We use FirstGenbkiObjectId as the cutoff, so that we only consider |
| 808 |
* objects with hand-assigned OIDs to be "built in", not for instance any |
| 809 |
* function or type defined in the information_schema. This is important |
| 810 |
* because only hand-assigned OIDs can be expected to remain stable across |
| 811 |
* major versions. |
| 812 |
*/ |
| 813 |
for (i = 0; i < desc->natts; i++) |
| 814 |
{ |
| 815 |
Form_pg_attribute att = TupleDescAttr(desc, i); |
| 816 |
|
| 817 |
if (!logicalrep_should_publish_column(att, columns, |
| 818 |
include_gencols_type)) |
| 819 |
continue; |
| 820 |
|
| 821 |
if (att->atttypid < FirstGenbkiObjectId) |
| 822 |
continue; |
| 823 |
|
| 824 |
OutputPluginPrepareWrite(ctx, false); |
| 825 |
logicalrep_write_typ(ctx->out, xid, att->atttypid); |
| 826 |
OutputPluginWrite(ctx, false); |
| 827 |
} |
| 828 |
|
| 829 |
OutputPluginPrepareWrite(ctx, false); |
| 830 |
logicalrep_write_rel(ctx->out, xid, relation, columns, |
| 831 |
include_gencols_type); |
| 832 |
OutputPluginWrite(ctx, false); |
| 833 |
} |
| 834 |
|
| 835 |
/* |
| 836 |
* Executor state preparation for evaluation of row filter expressions for the |
| 837 |
* specified relation. |
| 838 |
*/ |
| 839 |
static EState * |
| 840 |
create_estate_for_relation(Relation rel) |
| 841 |
{ |
| 842 |
EState *estate; |
| 843 |
RangeTblEntry *rte; |
| 844 |
List *perminfos = NIL; |
| 845 |
|
| 846 |
estate = CreateExecutorState(); |
| 847 |
|
| 848 |
rte = makeNode(RangeTblEntry); |
| 849 |
rte->rtekind = RTE_RELATION; |
| 850 |
rte->relid = RelationGetRelid(rel); |
| 851 |
rte->relkind = rel->rd_rel->relkind; |
| 852 |
rte->rellockmode = AccessShareLock; |
| 853 |
|
| 854 |
addRTEPermissionInfo(&perminfos, rte); |
| 855 |
|
| 856 |
ExecInitRangeTable(estate, list_make1(rte), perminfos, |
| 857 |
bms_make_singleton(1)); |
| 858 |
|
| 859 |
estate->es_output_cid = GetCurrentCommandId(false); |
| 860 |
|
| 861 |
return estate; |
| 862 |
} |
| 863 |
|
| 864 |
/* |
| 865 |
* Evaluates row filter. |
| 866 |
* |
| 867 |
* If the row filter evaluates to NULL, it is taken as false i.e. the change |
| 868 |
* isn't replicated. |
| 869 |
*/ |
| 870 |
static bool |
| 871 |
pgoutput_row_filter_exec_expr(ExprState *state, ExprContext *econtext) |
| 872 |
{ |
| 873 |
Datum ret; |
| 874 |
bool isnull; |
| 875 |
|
| 876 |
Assert(state != NULL); |
| 877 |
|
| 878 |
ret = ExecEvalExprSwitchContext(state, econtext, &isnull); |
| 879 |
|
| 880 |
elog(DEBUG3, "row filter evaluates to %s (isnull: %s)", |
| 881 |
isnull ? "false" : DatumGetBool(ret) ? "true" : "false", |
| 882 |
isnull ? "true" : "false"); |
| 883 |
|
| 884 |
if (isnull) |
| 885 |
return false; |
| 886 |
|
| 887 |
return DatumGetBool(ret); |
| 888 |
} |
| 889 |
|
| 890 |
/* |
| 891 |
* Make sure the per-entry memory context exists. |
| 892 |
*/ |
| 893 |
static void |
| 894 |
pgoutput_ensure_entry_cxt(PGOutputData *data, RelationSyncEntry *entry) |
| 895 |
{ |
| 896 |
Relation relation; |
| 897 |
|
| 898 |
/* The context may already exist, in which case bail out. */ |
| 899 |
if (entry->entry_cxt) |
| 900 |
return; |
| 901 |
|
| 902 |
relation = RelationIdGetRelation(entry->publish_as_relid); |
| 903 |
|
| 904 |
entry->entry_cxt = AllocSetContextCreate(data->cachectx, |
| 905 |
"entry private context", |
| 906 |
ALLOCSET_SMALL_SIZES); |
| 907 |
|
| 908 |
MemoryContextCopyAndSetIdentifier(entry->entry_cxt, |
| 909 |
RelationGetRelationName(relation)); |
| 910 |
} |
| 911 |
|
| 912 |
/* |
| 913 |
* Initialize the row filter. |
| 914 |
*/ |
| 915 |
static void |
| 916 |
pgoutput_row_filter_init(PGOutputData *data, List *publications, |
| 917 |
RelationSyncEntry *entry) |
| 918 |
{ |
| 919 |
ListCell *lc; |
| 920 |
List *rfnodes[] = {NIL, NIL, NIL}; /* One per pubaction */ |
| 921 |
bool no_filter[] = {false, false, false}; /* One per pubaction */ |
| 922 |
MemoryContext oldctx; |
| 923 |
int idx; |
| 924 |
bool has_filter = true; |
| 925 |
Oid schemaid = get_rel_namespace(entry->publish_as_relid); |
| 926 |
|
| 927 |
/* |
| 928 |
* Find if there are any row filters for this relation. If there are, then |
| 929 |
* prepare the necessary ExprState and cache it in entry->exprstate. To |
| 930 |
* build an expression state, we need to ensure the following: |
| 931 |
* |
| 932 |
* All the given publication-table mappings must be checked. |
| 933 |
* |
| 934 |
* Multiple publications might have multiple row filters for this |
| 935 |
* relation. Since row filter usage depends on the DML operation, there |
| 936 |
* are multiple lists (one for each operation) to which row filters will |
| 937 |
* be appended. |
| 938 |
* |
| 939 |
* FOR ALL TABLES and FOR TABLES IN SCHEMA implies "don't use row filter |
| 940 |
* expression" so it takes precedence. |
| 941 |
*/ |
| 942 |
foreach(lc, publications) |
| 943 |
{ |
| 944 |
Publication *pub = lfirst(lc); |
| 945 |
HeapTuple rftuple = NULL; |
| 946 |
Datum rfdatum = 0; |
| 947 |
bool pub_no_filter = true; |
| 948 |
|
| 949 |
/* |
| 950 |
* If the publication is FOR ALL TABLES, or the publication includes a |
| 951 |
* FOR TABLES IN SCHEMA where the table belongs to the referred |
| 952 |
* schema, then it is treated the same as if there are no row filters |
| 953 |
* (even if other publications have a row filter). |
| 954 |
*/ |
| 955 |
if (!pub->alltables && |
| 956 |
!SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP, |
| 957 |
ObjectIdGetDatum(schemaid), |
| 958 |
ObjectIdGetDatum(pub->oid))) |
| 959 |
{ |
| 960 |
/* |
| 961 |
* Check for the presence of a row filter in this publication. |
| 962 |
*/ |
| 963 |
rftuple = SearchSysCache2(PUBLICATIONRELMAP, |
| 964 |
ObjectIdGetDatum(entry->publish_as_relid), |
| 965 |
ObjectIdGetDatum(pub->oid)); |
| 966 |
|
| 967 |
if (HeapTupleIsValid(rftuple)) |
| 968 |
{ |
| 969 |
/* Null indicates no filter. */ |
| 970 |
rfdatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple, |
| 971 |
Anum_pg_publication_rel_prqual, |
| 972 |
&pub_no_filter); |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
if (pub_no_filter) |
| 977 |
{ |
| 978 |
if (rftuple) |
| 979 |
ReleaseSysCache(rftuple); |
| 980 |
|
| 981 |
no_filter[PUBACTION_INSERT] |= pub->pubactions.pubinsert; |
| 982 |
no_filter[PUBACTION_UPDATE] |= pub->pubactions.pubupdate; |
| 983 |
no_filter[PUBACTION_DELETE] |= pub->pubactions.pubdelete; |
| 984 |
|
| 985 |
/* |
| 986 |
* Quick exit if all the DML actions are publicized via this |
| 987 |
* publication. |
| 988 |
*/ |
| 989 |
if (no_filter[PUBACTION_INSERT] && |
| 990 |
no_filter[PUBACTION_UPDATE] && |
| 991 |
no_filter[PUBACTION_DELETE]) |
| 992 |
{ |
| 993 |
has_filter = false; |
| 994 |
break; |
| 995 |
} |
| 996 |
|
| 997 |
/* No additional work for this publication. Next one. */ |
| 998 |
continue; |
| 999 |
} |
| 1000 |
|
| 1001 |
/* Form the per pubaction row filter lists. */ |
| 1002 |
if (pub->pubactions.pubinsert && !no_filter[PUBACTION_INSERT]) |
| 1003 |
rfnodes[PUBACTION_INSERT] = lappend(rfnodes[PUBACTION_INSERT], |
| 1004 |
TextDatumGetCString(rfdatum)); |
| 1005 |
if (pub->pubactions.pubupdate && !no_filter[PUBACTION_UPDATE]) |
| 1006 |
rfnodes[PUBACTION_UPDATE] = lappend(rfnodes[PUBACTION_UPDATE], |
| 1007 |
TextDatumGetCString(rfdatum)); |
| 1008 |
if (pub->pubactions.pubdelete && !no_filter[PUBACTION_DELETE]) |
| 1009 |
rfnodes[PUBACTION_DELETE] = lappend(rfnodes[PUBACTION_DELETE], |
| 1010 |
TextDatumGetCString(rfdatum)); |
| 1011 |
|
| 1012 |
ReleaseSysCache(rftuple); |
| 1013 |
} /* loop all subscribed publications */ |
| 1014 |
|
| 1015 |
/* Clean the row filter */ |
| 1016 |
for (idx = 0; idx < NUM_ROWFILTER_PUBACTIONS; idx++) |
| 1017 |
{ |
| 1018 |
if (no_filter[idx]) |
| 1019 |
{ |
| 1020 |
list_free_deep(rfnodes[idx]); |
| 1021 |
rfnodes[idx] = NIL; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (has_filter) |
| 1026 |
{ |
| 1027 |
Relation relation = RelationIdGetRelation(entry->publish_as_relid); |
| 1028 |
|
| 1029 |
pgoutput_ensure_entry_cxt(data, entry); |
| 1030 |
|
| 1031 |
/* |
| 1032 |
* Now all the filters for all pubactions are known. Combine them when |
| 1033 |
* their pubactions are the same. |
| 1034 |
*/ |
| 1035 |
oldctx = MemoryContextSwitchTo(entry->entry_cxt); |
| 1036 |
entry->estate = create_estate_for_relation(relation); |
| 1037 |
for (idx = 0; idx < NUM_ROWFILTER_PUBACTIONS; idx++) |
| 1038 |
{ |
| 1039 |
List *filters = NIL; |
| 1040 |
Expr *rfnode; |
| 1041 |
|
| 1042 |
if (rfnodes[idx] == NIL) |
| 1043 |
continue; |
| 1044 |
|
| 1045 |
foreach(lc, rfnodes[idx]) |
| 1046 |
filters = lappend(filters, expand_generated_columns_in_expr(stringToNode((char *) lfirst(lc)), relation, 1)); |
| 1047 |
|
| 1048 |
/* combine the row filter and cache the ExprState */ |
| 1049 |
rfnode = make_orclause(filters); |
| 1050 |
entry->exprstate[idx] = ExecPrepareExpr(rfnode, entry->estate); |
| 1051 |
} /* for each pubaction */ |
| 1052 |
MemoryContextSwitchTo(oldctx); |
| 1053 |
|
| 1054 |
RelationClose(relation); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
/* |
| 1059 |
* If the table contains a generated column, check for any conflicting |
| 1060 |
* values of 'publish_generated_columns' parameter in the publications. |
| 1061 |
*/ |
| 1062 |
static void |
| 1063 |
check_and_init_gencol(PGOutputData *data, List *publications, |
| 1064 |
RelationSyncEntry *entry) |
| 1065 |
{ |
| 1066 |
Relation relation = RelationIdGetRelation(entry->publish_as_relid); |
| 1067 |
TupleDesc desc = RelationGetDescr(relation); |
| 1068 |
bool gencolpresent = false; |
| 1069 |
bool first = true; |
| 1070 |
|
| 1071 |
/* Check if there is any generated column present. */ |
| 1072 |
for (int i = 0; i < desc->natts; i++) |
| 1073 |
{ |
| 1074 |
Form_pg_attribute att = TupleDescAttr(desc, i); |
| 1075 |
|
| 1076 |
if (att->attgenerated) |
| 1077 |
{ |
| 1078 |
gencolpresent = true; |
| 1079 |
break; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* There are no generated columns to be published. */ |
| 1084 |
if (!gencolpresent) |
| 1085 |
{ |
| 1086 |
entry->include_gencols_type = PUBLISH_GENCOLS_NONE; |
| 1087 |
return; |
| 1088 |
} |
| 1089 |
|
| 1090 |
/* |
| 1091 |
* There may be a conflicting value for 'publish_generated_columns' |
| 1092 |
* parameter in the publications. |
| 1093 |
*/ |
| 1094 |
foreach_ptr(Publication, pub, publications) |
| 1095 |
{ |
| 1096 |
/* |
| 1097 |
* The column list takes precedence over the |
| 1098 |
* 'publish_generated_columns' parameter. Those will be checked later, |
| 1099 |
* see pgoutput_column_list_init. |
| 1100 |
*/ |
| 1101 |
if (check_and_fetch_column_list(pub, entry->publish_as_relid, NULL, NULL)) |
| 1102 |
continue; |
| 1103 |
|
| 1104 |
if (first) |
| 1105 |
{ |
| 1106 |
entry->include_gencols_type = pub->pubgencols_type; |
| 1107 |
first = false; |
| 1108 |
} |
| 1109 |
else if (entry->include_gencols_type != pub->pubgencols_type) |
| 1110 |
ereport(ERROR, |
| 1111 |
errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1112 |
errmsg("cannot use different values of publish_generated_columns for table \"%s.%s\" in different publications", |
| 1113 |
get_namespace_name(RelationGetNamespace(relation)), |
| 1114 |
RelationGetRelationName(relation))); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* |
| 1119 |
* Initialize the column list. |
| 1120 |
*/ |
| 1121 |
static void |
| 1122 |
pgoutput_column_list_init(PGOutputData *data, List *publications, |
| 1123 |
RelationSyncEntry *entry) |
| 1124 |
{ |
| 1125 |
ListCell *lc; |
| 1126 |
bool first = true; |
| 1127 |
Relation relation = RelationIdGetRelation(entry->publish_as_relid); |
| 1128 |
bool found_pub_collist = false; |
| 1129 |
Bitmapset *relcols = NULL; |
| 1130 |
|
| 1131 |
pgoutput_ensure_entry_cxt(data, entry); |
| 1132 |
|
| 1133 |
/* |
| 1134 |
* Find if there are any column lists for this relation. If there are, |
| 1135 |
* build a bitmap using the column lists. |
| 1136 |
* |
| 1137 |
* Multiple publications might have multiple column lists for this |
| 1138 |
* relation. |
| 1139 |
* |
| 1140 |
* Note that we don't support the case where the column list is different |
| 1141 |
* for the same table when combining publications. See comments atop |
| 1142 |
* fetch_table_list. But one can later change the publication so we still |
| 1143 |
* need to check all the given publication-table mappings and report an |
| 1144 |
* error if any publications have a different column list. |
| 1145 |
*/ |
| 1146 |
foreach(lc, publications) |
| 1147 |
{ |
| 1148 |
Publication *pub = lfirst(lc); |
| 1149 |
Bitmapset *cols = NULL; |
| 1150 |
|
| 1151 |
/* Retrieve the bitmap of columns for a column list publication. */ |
| 1152 |
found_pub_collist |= check_and_fetch_column_list(pub, |
| 1153 |
entry->publish_as_relid, |
| 1154 |
entry->entry_cxt, &cols); |
| 1155 |
|
| 1156 |
/* |
| 1157 |
* For non-column list publications — e.g. TABLE (without a column |
| 1158 |
* list), ALL TABLES, or ALL TABLES IN SCHEMA, we consider all columns |
| 1159 |
* of the table (including generated columns when |
| 1160 |
* 'publish_generated_columns' parameter is true). |
| 1161 |
*/ |
| 1162 |
if (!cols) |
| 1163 |
{ |
| 1164 |
/* |
| 1165 |
* Cache the table columns for the first publication with no |
| 1166 |
* specified column list to detect publication with a different |
| 1167 |
* column list. |
| 1168 |
*/ |
| 1169 |
if (!relcols && (list_length(publications) > 1)) |
| 1170 |
{ |
| 1171 |
MemoryContext oldcxt = MemoryContextSwitchTo(entry->entry_cxt); |
| 1172 |
|
| 1173 |
relcols = pub_form_cols_map(relation, |
| 1174 |
entry->include_gencols_type); |
| 1175 |
MemoryContextSwitchTo(oldcxt); |
| 1176 |
} |
| 1177 |
|
| 1178 |
cols = relcols; |
| 1179 |
} |
| 1180 |
|
| 1181 |
if (first) |
| 1182 |
{ |
| 1183 |
entry->columns = cols; |
| 1184 |
first = false; |
| 1185 |
} |
| 1186 |
else if (!bms_equal(entry->columns, cols)) |
| 1187 |
ereport(ERROR, |
| 1188 |
errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1189 |
errmsg("cannot use different column lists for table \"%s.%s\" in different publications", |
| 1190 |
get_namespace_name(RelationGetNamespace(relation)), |
| 1191 |
RelationGetRelationName(relation))); |
| 1192 |
} /* loop all subscribed publications */ |
| 1193 |
|
| 1194 |
/* |
| 1195 |
* If no column list publications exist, columns to be published will be |
| 1196 |
* computed later according to the 'publish_generated_columns' parameter. |
| 1197 |
*/ |
| 1198 |
if (!found_pub_collist) |
| 1199 |
entry->columns = NULL; |
| 1200 |
|
| 1201 |
RelationClose(relation); |
| 1202 |
} |
| 1203 |
|
| 1204 |
/* |
| 1205 |
* Initialize the slot for storing new and old tuples, and build the map that |
| 1206 |
* will be used to convert the relation's tuples into the ancestor's format. |
| 1207 |
*/ |
| 1208 |
static void |
| 1209 |
init_tuple_slot(PGOutputData *data, Relation relation, |
| 1210 |
RelationSyncEntry *entry) |
| 1211 |
{ |
| 1212 |
MemoryContext oldctx; |
| 1213 |
TupleDesc oldtupdesc; |
| 1214 |
TupleDesc newtupdesc; |
| 1215 |
|
| 1216 |
oldctx = MemoryContextSwitchTo(data->cachectx); |
| 1217 |
|
| 1218 |
/* |
| 1219 |
* Create tuple table slots. Create a copy of the TupleDesc as it needs to |
| 1220 |
* live as long as the cache remains. |
| 1221 |
*/ |
| 1222 |
oldtupdesc = CreateTupleDescCopyConstr(RelationGetDescr(relation)); |
| 1223 |
newtupdesc = CreateTupleDescCopyConstr(RelationGetDescr(relation)); |
| 1224 |
|
| 1225 |
entry->old_slot = MakeSingleTupleTableSlot(oldtupdesc, &TTSOpsHeapTuple); |
| 1226 |
entry->new_slot = MakeSingleTupleTableSlot(newtupdesc, &TTSOpsHeapTuple); |
| 1227 |
|
| 1228 |
MemoryContextSwitchTo(oldctx); |
| 1229 |
|
| 1230 |
/* |
| 1231 |
* Cache the map that will be used to convert the relation's tuples into |
| 1232 |
* the ancestor's format, if needed. |
| 1233 |
*/ |
| 1234 |
if (entry->publish_as_relid != RelationGetRelid(relation)) |
| 1235 |
{ |
| 1236 |
Relation ancestor = RelationIdGetRelation(entry->publish_as_relid); |
| 1237 |
TupleDesc indesc = RelationGetDescr(relation); |
| 1238 |
TupleDesc outdesc = RelationGetDescr(ancestor); |
| 1239 |
|
| 1240 |
/* Map must live as long as the logical decoding context. */ |
| 1241 |
oldctx = MemoryContextSwitchTo(data->cachectx); |
| 1242 |
|
| 1243 |
entry->attrmap = build_attrmap_by_name_if_req(indesc, outdesc, false); |
| 1244 |
|
| 1245 |
MemoryContextSwitchTo(oldctx); |
| 1246 |
RelationClose(ancestor); |
| 1247 |
} |
| 1248 |
} |
| 1249 |
|
| 1250 |
/* |
| 1251 |
* Change is checked against the row filter if any. |
| 1252 |
* |
| 1253 |
* Returns true if the change is to be replicated, else false. |
| 1254 |
* |
| 1255 |
* For inserts, evaluate the row filter for new tuple. |
| 1256 |
* For deletes, evaluate the row filter for old tuple. |
| 1257 |
* For updates, evaluate the row filter for old and new tuple. |
| 1258 |
* |
| 1259 |
* For updates, if both evaluations are true, we allow sending the UPDATE and |
| 1260 |
* if both the evaluations are false, it doesn't replicate the UPDATE. Now, if |
| 1261 |
* only one of the tuples matches the row filter expression, we transform |
| 1262 |
* UPDATE to DELETE or INSERT to avoid any data inconsistency based on the |
| 1263 |
* following rules: |
| 1264 |
* |
| 1265 |
* Case 1: old-row (no match) new-row (no match) -> (drop change) |
| 1266 |
* Case 2: old-row (no match) new row (match) -> INSERT |
| 1267 |
* Case 3: old-row (match) new-row (no match) -> DELETE |
| 1268 |
* Case 4: old-row (match) new row (match) -> UPDATE |
| 1269 |
* |
| 1270 |
* The new action is updated in the action parameter. |
| 1271 |
* |
| 1272 |
* The new slot could be updated when transforming the UPDATE into INSERT, |
| 1273 |
* because the original new tuple might not have column values from the replica |
| 1274 |
* identity. |
| 1275 |
* |
| 1276 |
* Examples: |
| 1277 |
* Let's say the old tuple satisfies the row filter but the new tuple doesn't. |
| 1278 |
* Since the old tuple satisfies, the initial table synchronization copied this |
| 1279 |
* row (or another method was used to guarantee that there is data |
| 1280 |
* consistency). However, after the UPDATE the new tuple doesn't satisfy the |
| 1281 |
* row filter, so from a data consistency perspective, that row should be |
| 1282 |
* removed on the subscriber. The UPDATE should be transformed into a DELETE |
| 1283 |
* statement and be sent to the subscriber. Keeping this row on the subscriber |
| 1284 |
* is undesirable because it doesn't reflect what was defined in the row filter |
| 1285 |
* expression on the publisher. This row on the subscriber would likely not be |
| 1286 |
* modified by replication again. If someone inserted a new row with the same |
| 1287 |
* old identifier, replication could stop due to a constraint violation. |
| 1288 |
* |
| 1289 |
* Let's say the old tuple doesn't match the row filter but the new tuple does. |
| 1290 |
* Since the old tuple doesn't satisfy, the initial table synchronization |
| 1291 |
* probably didn't copy this row. However, after the UPDATE the new tuple does |
| 1292 |
* satisfy the row filter, so from a data consistency perspective, that row |
| 1293 |
* should be inserted on the subscriber. Otherwise, subsequent UPDATE or DELETE |
| 1294 |
* statements have no effect (it matches no row -- see |
| 1295 |
* apply_handle_update_internal()). So, the UPDATE should be transformed into a |
| 1296 |
* INSERT statement and be sent to the subscriber. However, this might surprise |
| 1297 |
* someone who expects the data set to satisfy the row filter expression on the |
| 1298 |
* provider. |
| 1299 |
*/ |
| 1300 |
static bool |
| 1301 |
pgoutput_row_filter(Relation relation, TupleTableSlot *old_slot, |
| 1302 |
TupleTableSlot **new_slot_ptr, RelationSyncEntry *entry, |
| 1303 |
ReorderBufferChangeType *action) |
| 1304 |
{ |
| 1305 |
TupleDesc desc; |
| 1306 |
int i; |
| 1307 |
bool old_matched, |
| 1308 |
new_matched, |
| 1309 |
result; |
| 1310 |
TupleTableSlot *tmp_new_slot; |
| 1311 |
TupleTableSlot *new_slot = *new_slot_ptr; |
| 1312 |
ExprContext *ecxt; |
| 1313 |
ExprState *filter_exprstate; |
| 1314 |
|
| 1315 |
/* |
| 1316 |
* We need this map to avoid relying on ReorderBufferChangeType enums |
| 1317 |
* having specific values. |
| 1318 |
*/ |
| 1319 |
static const int map_changetype_pubaction[] = { |
| 1320 |
[REORDER_BUFFER_CHANGE_INSERT] = PUBACTION_INSERT, |
| 1321 |
[REORDER_BUFFER_CHANGE_UPDATE] = PUBACTION_UPDATE, |
| 1322 |
[REORDER_BUFFER_CHANGE_DELETE] = PUBACTION_DELETE |
| 1323 |
}; |
| 1324 |
|
| 1325 |
Assert(*action == REORDER_BUFFER_CHANGE_INSERT || |
| 1326 |
*action == REORDER_BUFFER_CHANGE_UPDATE || |
| 1327 |
*action == REORDER_BUFFER_CHANGE_DELETE); |
| 1328 |
|
| 1329 |
Assert(new_slot || old_slot); |
| 1330 |
|
| 1331 |
/* Get the corresponding row filter */ |
| 1332 |
filter_exprstate = entry->exprstate[map_changetype_pubaction[*action]]; |
| 1333 |
|
| 1334 |
/* Bail out if there is no row filter */ |
| 1335 |
if (!filter_exprstate) |
| 1336 |
return true; |
| 1337 |
|
| 1338 |
elog(DEBUG3, "table \"%s.%s\" has row filter", |
| 1339 |
get_namespace_name(RelationGetNamespace(relation)), |
| 1340 |
RelationGetRelationName(relation)); |
| 1341 |
|
| 1342 |
ResetPerTupleExprContext(entry->estate); |
| 1343 |
|
| 1344 |
ecxt = GetPerTupleExprContext(entry->estate); |
| 1345 |
|
| 1346 |
/* |
| 1347 |
* For the following occasions where there is only one tuple, we can |
| 1348 |
* evaluate the row filter for that tuple and return. |
| 1349 |
* |
| 1350 |
* For inserts, we only have the new tuple. |
| 1351 |
* |
| 1352 |
* For updates, we can have only a new tuple when none of the replica |
| 1353 |
* identity columns changed and none of those columns have external data |
| 1354 |
* but we still need to evaluate the row filter for the new tuple as the |
| 1355 |
* existing values of those columns might not match the filter. Also, |
| 1356 |
* users can use constant expressions in the row filter, so we anyway need |
| 1357 |
* to evaluate it for the new tuple. |
| 1358 |
* |
| 1359 |
* For deletes, we only have the old tuple. |
| 1360 |
*/ |
| 1361 |
if (!new_slot || !old_slot) |
| 1362 |
{ |
| 1363 |
ecxt->ecxt_scantuple = new_slot ? new_slot : old_slot; |
| 1364 |
result = pgoutput_row_filter_exec_expr(filter_exprstate, ecxt); |
| 1365 |
|
| 1366 |
return result; |
| 1367 |
} |
| 1368 |
|
| 1369 |
/* |
| 1370 |
* Both the old and new tuples must be valid only for updates and need to |
| 1371 |
* be checked against the row filter. |
| 1372 |
*/ |
| 1373 |
Assert(map_changetype_pubaction[*action] == PUBACTION_UPDATE); |
| 1374 |
|
| 1375 |
slot_getallattrs(new_slot); |
| 1376 |
slot_getallattrs(old_slot); |
| 1377 |
|
| 1378 |
tmp_new_slot = NULL; |
| 1379 |
desc = RelationGetDescr(relation); |
| 1380 |
|
| 1381 |
/* |
| 1382 |
* The new tuple might not have all the replica identity columns, in which |
| 1383 |
* case it needs to be copied over from the old tuple. |
| 1384 |
*/ |
| 1385 |
for (i = 0; i < desc->natts; i++) |
| 1386 |
{ |
| 1387 |
CompactAttribute *att = TupleDescCompactAttr(desc, i); |
| 1388 |
|
| 1389 |
/* |
| 1390 |
* if the column in the new tuple or old tuple is null, nothing to do |
| 1391 |
*/ |
| 1392 |
if (new_slot->tts_isnull[i] || old_slot->tts_isnull[i]) |
| 1393 |
continue; |
| 1394 |
|
| 1395 |
/* |
| 1396 |
* Unchanged toasted replica identity columns are only logged in the |
| 1397 |
* old tuple. Copy this over to the new tuple. The changed (or WAL |
| 1398 |
* Logged) toast values are always assembled in memory and set as |
| 1399 |
* VARTAG_INDIRECT. See ReorderBufferToastReplace. |
| 1400 |
*/ |
| 1401 |
if (att->attlen == -1 && |
| 1402 |
VARATT_IS_EXTERNAL_ONDISK(new_slot->tts_values[i]) && |
| 1403 |
!VARATT_IS_EXTERNAL_ONDISK(old_slot->tts_values[i])) |
| 1404 |
{ |
| 1405 |
if (!tmp_new_slot) |
| 1406 |
{ |
| 1407 |
tmp_new_slot = MakeSingleTupleTableSlot(desc, &TTSOpsVirtual); |
| 1408 |
ExecClearTuple(tmp_new_slot); |
| 1409 |
|
| 1410 |
memcpy(tmp_new_slot->tts_values, new_slot->tts_values, |
| 1411 |
desc->natts * sizeof(Datum)); |
| 1412 |
memcpy(tmp_new_slot->tts_isnull, new_slot->tts_isnull, |
| 1413 |
desc->natts * sizeof(bool)); |
| 1414 |
} |
| 1415 |
|
| 1416 |
tmp_new_slot->tts_values[i] = old_slot->tts_values[i]; |
| 1417 |
tmp_new_slot->tts_isnull[i] = old_slot->tts_isnull[i]; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
ecxt->ecxt_scantuple = old_slot; |
| 1422 |
old_matched = pgoutput_row_filter_exec_expr(filter_exprstate, ecxt); |
| 1423 |
|
| 1424 |
if (tmp_new_slot) |
| 1425 |
{ |
| 1426 |
ExecStoreVirtualTuple(tmp_new_slot); |
| 1427 |
ecxt->ecxt_scantuple = tmp_new_slot; |
| 1428 |
} |
| 1429 |
else |
| 1430 |
ecxt->ecxt_scantuple = new_slot; |
| 1431 |
|
| 1432 |
new_matched = pgoutput_row_filter_exec_expr(filter_exprstate, ecxt); |
| 1433 |
|
| 1434 |
/* |
| 1435 |
* Case 1: if both tuples don't match the row filter, bailout. Send |
| 1436 |
* nothing. |
| 1437 |
*/ |
| 1438 |
if (!old_matched && !new_matched) |
| 1439 |
return false; |
| 1440 |
|
| 1441 |
/* |
| 1442 |
* Case 2: if the old tuple doesn't satisfy the row filter but the new |
| 1443 |
* tuple does, transform the UPDATE into INSERT. |
| 1444 |
* |
| 1445 |
* Use the newly transformed tuple that must contain the column values for |
| 1446 |
* all the replica identity columns. This is required to ensure that the |
| 1447 |
* while inserting the tuple in the downstream node, we have all the |
| 1448 |
* required column values. |
| 1449 |
*/ |
| 1450 |
if (!old_matched && new_matched) |
| 1451 |
{ |
| 1452 |
*action = REORDER_BUFFER_CHANGE_INSERT; |
| 1453 |
|
| 1454 |
if (tmp_new_slot) |
| 1455 |
*new_slot_ptr = tmp_new_slot; |
| 1456 |
} |
| 1457 |
|
| 1458 |
/* |
| 1459 |
* Case 3: if the old tuple satisfies the row filter but the new tuple |
| 1460 |
* doesn't, transform the UPDATE into DELETE. |
| 1461 |
* |
| 1462 |
* This transformation does not require another tuple. The Old tuple will |
| 1463 |
* be used for DELETE. |
| 1464 |
*/ |
| 1465 |
else if (old_matched && !new_matched) |
| 1466 |
*action = REORDER_BUFFER_CHANGE_DELETE; |
| 1467 |
|
| 1468 |
/* |
| 1469 |
* Case 4: if both tuples match the row filter, transformation isn't |
| 1470 |
* required. (*action is default UPDATE). |
| 1471 |
*/ |
| 1472 |
|
| 1473 |
return true; |
| 1474 |
} |
| 1475 |
|
| 1476 |
/* |
| 1477 |
* Sends the decoded DML over wire. |
| 1478 |
* |
| 1479 |
* This is called both in streaming and non-streaming modes. |
| 1480 |
*/ |
| 1481 |
static void |
| 1482 |
pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, |
| 1483 |
Relation relation, ReorderBufferChange *change) |
| 1484 |
{ |
| 1485 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 1486 |
PGOutputTxnData *txndata = (PGOutputTxnData *) txn->output_plugin_private; |
| 1487 |
MemoryContext old; |
| 1488 |
RelationSyncEntry *relentry; |
| 1489 |
TransactionId xid = InvalidTransactionId; |
| 1490 |
Relation ancestor = NULL; |
| 1491 |
Relation targetrel = relation; |
| 1492 |
ReorderBufferChangeType action = change->action; |
| 1493 |
TupleTableSlot *old_slot = NULL; |
| 1494 |
TupleTableSlot *new_slot = NULL; |
| 1495 |
|
| 1496 |
if (!is_publishable_relation(relation)) |
| 1497 |
return; |
| 1498 |
|
| 1499 |
/* |
| 1500 |
* Remember the xid for the change in streaming mode. We need to send xid |
| 1501 |
* with each change in the streaming mode so that subscriber can make |
| 1502 |
* their association and on aborts, it can discard the corresponding |
| 1503 |
* changes. |
| 1504 |
*/ |
| 1505 |
if (data->in_streaming) |
| 1506 |
xid = change->txn->xid; |
| 1507 |
|
| 1508 |
relentry = get_rel_sync_entry(data, relation); |
| 1509 |
|
| 1510 |
/* First check the table filter */ |
| 1511 |
switch (action) |
| 1512 |
{ |
| 1513 |
case REORDER_BUFFER_CHANGE_INSERT: |
| 1514 |
if (!relentry->pubactions.pubinsert) |
| 1515 |
return; |
| 1516 |
break; |
| 1517 |
case REORDER_BUFFER_CHANGE_UPDATE: |
| 1518 |
if (!relentry->pubactions.pubupdate) |
| 1519 |
return; |
| 1520 |
break; |
| 1521 |
case REORDER_BUFFER_CHANGE_DELETE: |
| 1522 |
if (!relentry->pubactions.pubdelete) |
| 1523 |
return; |
| 1524 |
|
| 1525 |
/* |
| 1526 |
* This is only possible if deletes are allowed even when replica |
| 1527 |
* identity is not defined for a table. Since the DELETE action |
| 1528 |
* can't be published, we simply return. |
| 1529 |
*/ |
| 1530 |
if (!change->data.tp.oldtuple) |
| 1531 |
{ |
| 1532 |
elog(DEBUG1, "didn't send DELETE change because of missing oldtuple"); |
| 1533 |
return; |
| 1534 |
} |
| 1535 |
break; |
| 1536 |
default: |
| 1537 |
Assert(false); |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* Avoid leaking memory by using and resetting our own context */ |
| 1541 |
old = MemoryContextSwitchTo(data->context); |
| 1542 |
|
| 1543 |
/* Switch relation if publishing via root. */ |
| 1544 |
if (relentry->publish_as_relid != RelationGetRelid(relation)) |
| 1545 |
{ |
| 1546 |
Assert(relation->rd_rel->relispartition); |
| 1547 |
ancestor = RelationIdGetRelation(relentry->publish_as_relid); |
| 1548 |
targetrel = ancestor; |
| 1549 |
} |
| 1550 |
|
| 1551 |
if (change->data.tp.oldtuple) |
| 1552 |
{ |
| 1553 |
old_slot = relentry->old_slot; |
| 1554 |
ExecStoreHeapTuple(change->data.tp.oldtuple, old_slot, false); |
| 1555 |
|
| 1556 |
/* Convert tuple if needed. */ |
| 1557 |
if (relentry->attrmap) |
| 1558 |
{ |
| 1559 |
TupleTableSlot *slot = MakeTupleTableSlot(RelationGetDescr(targetrel), |
| 1560 |
&TTSOpsVirtual); |
| 1561 |
|
| 1562 |
old_slot = execute_attr_map_slot(relentry->attrmap, old_slot, slot); |
| 1563 |
} |
| 1564 |
} |
| 1565 |
|
| 1566 |
if (change->data.tp.newtuple) |
| 1567 |
{ |
| 1568 |
new_slot = relentry->new_slot; |
| 1569 |
ExecStoreHeapTuple(change->data.tp.newtuple, new_slot, false); |
| 1570 |
|
| 1571 |
/* Convert tuple if needed. */ |
| 1572 |
if (relentry->attrmap) |
| 1573 |
{ |
| 1574 |
TupleTableSlot *slot = MakeTupleTableSlot(RelationGetDescr(targetrel), |
| 1575 |
&TTSOpsVirtual); |
| 1576 |
|
| 1577 |
new_slot = execute_attr_map_slot(relentry->attrmap, new_slot, slot); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
/* |
| 1582 |
* Check row filter. |
| 1583 |
* |
| 1584 |
* Updates could be transformed to inserts or deletes based on the results |
| 1585 |
* of the row filter for old and new tuple. |
| 1586 |
*/ |
| 1587 |
if (!pgoutput_row_filter(targetrel, old_slot, &new_slot, relentry, &action)) |
| 1588 |
goto cleanup; |
| 1589 |
|
| 1590 |
/* |
| 1591 |
* Send BEGIN if we haven't yet. |
| 1592 |
* |
| 1593 |
* We send the BEGIN message after ensuring that we will actually send the |
| 1594 |
* change. This avoids sending a pair of BEGIN/COMMIT messages for empty |
| 1595 |
* transactions. |
| 1596 |
*/ |
| 1597 |
if (txndata && !txndata->sent_begin_txn) |
| 1598 |
pgoutput_send_begin(ctx, txn); |
| 1599 |
|
| 1600 |
/* |
| 1601 |
* Schema should be sent using the original relation because it also sends |
| 1602 |
* the ancestor's relation. |
| 1603 |
*/ |
| 1604 |
maybe_send_schema(ctx, change, relation, relentry); |
| 1605 |
|
| 1606 |
OutputPluginPrepareWrite(ctx, true); |
| 1607 |
|
| 1608 |
/* Send the data */ |
| 1609 |
switch (action) |
| 1610 |
{ |
| 1611 |
case REORDER_BUFFER_CHANGE_INSERT: |
| 1612 |
logicalrep_write_insert(ctx->out, xid, targetrel, new_slot, |
| 1613 |
data->binary, relentry->columns, |
| 1614 |
relentry->include_gencols_type); |
| 1615 |
break; |
| 1616 |
case REORDER_BUFFER_CHANGE_UPDATE: |
| 1617 |
logicalrep_write_update(ctx->out, xid, targetrel, old_slot, |
| 1618 |
new_slot, data->binary, relentry->columns, |
| 1619 |
relentry->include_gencols_type); |
| 1620 |
break; |
| 1621 |
case REORDER_BUFFER_CHANGE_DELETE: |
| 1622 |
logicalrep_write_delete(ctx->out, xid, targetrel, old_slot, |
| 1623 |
data->binary, relentry->columns, |
| 1624 |
relentry->include_gencols_type); |
| 1625 |
break; |
| 1626 |
default: |
| 1627 |
Assert(false); |
| 1628 |
} |
| 1629 |
|
| 1630 |
OutputPluginWrite(ctx, true); |
| 1631 |
|
| 1632 |
cleanup: |
| 1633 |
if (RelationIsValid(ancestor)) |
| 1634 |
{ |
| 1635 |
RelationClose(ancestor); |
| 1636 |
ancestor = NULL; |
| 1637 |
} |
| 1638 |
|
| 1639 |
/* Drop the new slots that were used to store the converted tuples. */ |
| 1640 |
if (relentry->attrmap) |
| 1641 |
{ |
| 1642 |
if (old_slot) |
| 1643 |
ExecDropSingleTupleTableSlot(old_slot); |
| 1644 |
|
| 1645 |
if (new_slot) |
| 1646 |
ExecDropSingleTupleTableSlot(new_slot); |
| 1647 |
} |
| 1648 |
|
| 1649 |
MemoryContextSwitchTo(old); |
| 1650 |
MemoryContextReset(data->context); |
| 1651 |
} |
| 1652 |
|
| 1653 |
static void |
| 1654 |
pgoutput_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, |
| 1655 |
int nrelations, Relation relations[], ReorderBufferChange *change) |
| 1656 |
{ |
| 1657 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 1658 |
PGOutputTxnData *txndata = (PGOutputTxnData *) txn->output_plugin_private; |
| 1659 |
MemoryContext old; |
| 1660 |
RelationSyncEntry *relentry; |
| 1661 |
int i; |
| 1662 |
int nrelids; |
| 1663 |
Oid *relids; |
| 1664 |
TransactionId xid = InvalidTransactionId; |
| 1665 |
|
| 1666 |
/* Remember the xid for the change in streaming mode. See pgoutput_change. */ |
| 1667 |
if (data->in_streaming) |
| 1668 |
xid = change->txn->xid; |
| 1669 |
|
| 1670 |
old = MemoryContextSwitchTo(data->context); |
| 1671 |
|
| 1672 |
relids = palloc0(nrelations * sizeof(Oid)); |
| 1673 |
nrelids = 0; |
| 1674 |
|
| 1675 |
for (i = 0; i < nrelations; i++) |
| 1676 |
{ |
| 1677 |
Relation relation = relations[i]; |
| 1678 |
Oid relid = RelationGetRelid(relation); |
| 1679 |
|
| 1680 |
if (!is_publishable_relation(relation)) |
| 1681 |
continue; |
| 1682 |
|
| 1683 |
relentry = get_rel_sync_entry(data, relation); |
| 1684 |
|
| 1685 |
if (!relentry->pubactions.pubtruncate) |
| 1686 |
continue; |
| 1687 |
|
| 1688 |
/* |
| 1689 |
* Don't send partitions if the publication wants to send only the |
| 1690 |
* root tables through it. |
| 1691 |
*/ |
| 1692 |
if (relation->rd_rel->relispartition && |
| 1693 |
relentry->publish_as_relid != relid) |
| 1694 |
continue; |
| 1695 |
|
| 1696 |
relids[nrelids++] = relid; |
| 1697 |
|
| 1698 |
/* Send BEGIN if we haven't yet */ |
| 1699 |
if (txndata && !txndata->sent_begin_txn) |
| 1700 |
pgoutput_send_begin(ctx, txn); |
| 1701 |
|
| 1702 |
maybe_send_schema(ctx, change, relation, relentry); |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (nrelids > 0) |
| 1706 |
{ |
| 1707 |
OutputPluginPrepareWrite(ctx, true); |
| 1708 |
logicalrep_write_truncate(ctx->out, |
| 1709 |
xid, |
| 1710 |
nrelids, |
| 1711 |
relids, |
| 1712 |
change->data.truncate.cascade, |
| 1713 |
change->data.truncate.restart_seqs); |
| 1714 |
OutputPluginWrite(ctx, true); |
| 1715 |
} |
| 1716 |
|
| 1717 |
MemoryContextSwitchTo(old); |
| 1718 |
MemoryContextReset(data->context); |
| 1719 |
} |
| 1720 |
|
| 1721 |
static void |
| 1722 |
pgoutput_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, |
| 1723 |
XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, |
| 1724 |
const char *message) |
| 1725 |
{ |
| 1726 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 1727 |
TransactionId xid = InvalidTransactionId; |
| 1728 |
|
| 1729 |
if (!data->messages) |
| 1730 |
return; |
| 1731 |
|
| 1732 |
/* |
| 1733 |
* Remember the xid for the message in streaming mode. See |
| 1734 |
* pgoutput_change. |
| 1735 |
*/ |
| 1736 |
if (data->in_streaming) |
| 1737 |
xid = txn->xid; |
| 1738 |
|
| 1739 |
/* |
| 1740 |
* Output BEGIN if we haven't yet. Avoid for non-transactional messages. |
| 1741 |
*/ |
| 1742 |
if (transactional) |
| 1743 |
{ |
| 1744 |
PGOutputTxnData *txndata = (PGOutputTxnData *) txn->output_plugin_private; |
| 1745 |
|
| 1746 |
/* Send BEGIN if we haven't yet */ |
| 1747 |
if (txndata && !txndata->sent_begin_txn) |
| 1748 |
pgoutput_send_begin(ctx, txn); |
| 1749 |
} |
| 1750 |
|
| 1751 |
OutputPluginPrepareWrite(ctx, true); |
| 1752 |
logicalrep_write_message(ctx->out, |
| 1753 |
xid, |
| 1754 |
message_lsn, |
| 1755 |
transactional, |
| 1756 |
prefix, |
| 1757 |
sz, |
| 1758 |
message); |
| 1759 |
OutputPluginWrite(ctx, true); |
| 1760 |
} |
| 1761 |
|
| 1762 |
/* |
| 1763 |
* Return true if the data is associated with an origin and the user has |
| 1764 |
* requested the changes that don't have an origin, false otherwise. |
| 1765 |
*/ |
| 1766 |
static bool |
| 1767 |
pgoutput_origin_filter(LogicalDecodingContext *ctx, |
| 1768 |
RepOriginId origin_id) |
| 1769 |
{ |
| 1770 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 1771 |
|
| 1772 |
if (data->publish_no_origin && origin_id != InvalidRepOriginId) |
| 1773 |
return true; |
| 1774 |
|
| 1775 |
return false; |
| 1776 |
} |
| 1777 |
|
| 1778 |
/* |
| 1779 |
* Shutdown the output plugin. |
| 1780 |
* |
| 1781 |
* Note, we don't need to clean the data->context, data->cachectx, and |
| 1782 |
* data->pubctx as they are child contexts of the ctx->context so they |
| 1783 |
* will be cleaned up by logical decoding machinery. |
| 1784 |
*/ |
| 1785 |
static void |
| 1786 |
pgoutput_shutdown(LogicalDecodingContext *ctx) |
| 1787 |
{ |
| 1788 |
pgoutput_memory_context_reset(NULL); |
| 1789 |
} |
| 1790 |
|
| 1791 |
/* |
| 1792 |
* Load publications from the list of publication names. |
| 1793 |
* |
| 1794 |
* Here, we skip the publications that don't exist yet. This will allow us |
| 1795 |
* to silently continue the replication in the absence of a missing publication. |
| 1796 |
* This is required because we allow the users to create publications after they |
| 1797 |
* have specified the required publications at the time of replication start. |
| 1798 |
*/ |
| 1799 |
static List * |
| 1800 |
LoadPublications(List *pubnames) |
| 1801 |
{ |
| 1802 |
List *result = NIL; |
| 1803 |
ListCell *lc; |
| 1804 |
|
| 1805 |
foreach(lc, pubnames) |
| 1806 |
{ |
| 1807 |
char *pubname = (char *) lfirst(lc); |
| 1808 |
Publication *pub = GetPublicationByName(pubname, true); |
| 1809 |
|
| 1810 |
if (pub) |
| 1811 |
result = lappend(result, pub); |
| 1812 |
else |
| 1813 |
ereport(WARNING, |
| 1814 |
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1815 |
errmsg("skipped loading publication \"%s\"", pubname), |
| 1816 |
errdetail("The publication does not exist at this point in the WAL."), |
| 1817 |
errhint("Create the publication if it does not exist.")); |
| 1818 |
} |
| 1819 |
|
| 1820 |
return result; |
| 1821 |
} |
| 1822 |
|
| 1823 |
/* |
| 1824 |
* Publication syscache invalidation callback. |
| 1825 |
* |
| 1826 |
* Called for invalidations on pg_publication. |
| 1827 |
*/ |
| 1828 |
static void |
| 1829 |
publication_invalidation_cb(Datum arg, int cacheid, uint32 hashvalue) |
| 1830 |
{ |
| 1831 |
publications_valid = false; |
| 1832 |
} |
| 1833 |
|
| 1834 |
/* |
| 1835 |
* START STREAM callback |
| 1836 |
*/ |
| 1837 |
static void |
| 1838 |
pgoutput_stream_start(struct LogicalDecodingContext *ctx, |
| 1839 |
ReorderBufferTXN *txn) |
| 1840 |
{ |
| 1841 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 1842 |
bool send_replication_origin = txn->origin_id != InvalidRepOriginId; |
| 1843 |
|
| 1844 |
/* we can't nest streaming of transactions */ |
| 1845 |
Assert(!data->in_streaming); |
| 1846 |
|
| 1847 |
/* |
| 1848 |
* If we already sent the first stream for this transaction then don't |
| 1849 |
* send the origin id in the subsequent streams. |
| 1850 |
*/ |
| 1851 |
if (rbtxn_is_streamed(txn)) |
| 1852 |
send_replication_origin = false; |
| 1853 |
|
| 1854 |
OutputPluginPrepareWrite(ctx, !send_replication_origin); |
| 1855 |
logicalrep_write_stream_start(ctx->out, txn->xid, !rbtxn_is_streamed(txn)); |
| 1856 |
|
| 1857 |
send_repl_origin(ctx, txn->origin_id, InvalidXLogRecPtr, |
| 1858 |
send_replication_origin); |
| 1859 |
|
| 1860 |
OutputPluginWrite(ctx, true); |
| 1861 |
|
| 1862 |
/* we're streaming a chunk of transaction now */ |
| 1863 |
data->in_streaming = true; |
| 1864 |
} |
| 1865 |
|
| 1866 |
/* |
| 1867 |
* STOP STREAM callback |
| 1868 |
*/ |
| 1869 |
static void |
| 1870 |
pgoutput_stream_stop(struct LogicalDecodingContext *ctx, |
| 1871 |
ReorderBufferTXN *txn) |
| 1872 |
{ |
| 1873 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 1874 |
|
| 1875 |
/* we should be streaming a transaction */ |
| 1876 |
Assert(data->in_streaming); |
| 1877 |
|
| 1878 |
OutputPluginPrepareWrite(ctx, true); |
| 1879 |
logicalrep_write_stream_stop(ctx->out); |
| 1880 |
OutputPluginWrite(ctx, true); |
| 1881 |
|
| 1882 |
/* we've stopped streaming a transaction */ |
| 1883 |
data->in_streaming = false; |
| 1884 |
} |
| 1885 |
|
| 1886 |
/* |
| 1887 |
* Notify downstream to discard the streamed transaction (along with all |
| 1888 |
* its subtransactions, if it's a toplevel transaction). |
| 1889 |
*/ |
| 1890 |
static void |
| 1891 |
pgoutput_stream_abort(struct LogicalDecodingContext *ctx, |
| 1892 |
ReorderBufferTXN *txn, |
| 1893 |
XLogRecPtr abort_lsn) |
| 1894 |
{ |
| 1895 |
ReorderBufferTXN *toptxn; |
| 1896 |
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private; |
| 1897 |
bool write_abort_info = (data->streaming == LOGICALREP_STREAM_PARALLEL); |
| 1898 |
|
| 1899 |
/* |
| 1900 |
* The abort should happen outside streaming block, even for streamed |
| 1901 |
* transactions. The transaction has to be marked as streamed, though. |
| 1902 |
*/ |
| 1903 |
Assert(!data->in_streaming); |
| 1904 |
|
| 1905 |
/* determine the toplevel transaction */ |
| 1906 |
toptxn = rbtxn_get_toptxn(txn); |
| 1907 |
|
| 1908 |
Assert(rbtxn_is_streamed(toptxn)); |
| 1909 |
|
| 1910 |
OutputPluginPrepareWrite(ctx, true); |
| 1911 |
logicalrep_write_stream_abort(ctx->out, toptxn->xid, txn->xid, abort_lsn, |
| 1912 |
txn->xact_time.abort_time, write_abort_info); |
| 1913 |
|
| 1914 |
OutputPluginWrite(ctx, true); |
| 1915 |
|
| 1916 |
cleanup_rel_sync_cache(toptxn->xid, false); |
| 1917 |
} |
| 1918 |
|
| 1919 |
/* |
| 1920 |
* Notify downstream to apply the streamed transaction (along with all |
| 1921 |
* its subtransactions). |
| 1922 |
*/ |
| 1923 |
static void |
| 1924 |
pgoutput_stream_commit(struct LogicalDecodingContext *ctx, |
| 1925 |
ReorderBufferTXN *txn, |
| 1926 |
XLogRecPtr commit_lsn) |
| 1927 |
{ |
| 1928 |
PGOutputData *data PG_USED_FOR_ASSERTS_ONLY = (PGOutputData *) ctx->output_plugin_private; |
| 1929 |
|
| 1930 |
/* |
| 1931 |
* The commit should happen outside streaming block, even for streamed |
| 1932 |
* transactions. The transaction has to be marked as streamed, though. |
| 1933 |
*/ |
| 1934 |
Assert(!data->in_streaming); |
| 1935 |
Assert(rbtxn_is_streamed(txn)); |
| 1936 |
|
| 1937 |
OutputPluginUpdateProgress(ctx, false); |
| 1938 |
|
| 1939 |
OutputPluginPrepareWrite(ctx, true); |
| 1940 |
logicalrep_write_stream_commit(ctx->out, txn, commit_lsn); |
| 1941 |
OutputPluginWrite(ctx, true); |
| 1942 |
|
| 1943 |
cleanup_rel_sync_cache(txn->xid, true); |
| 1944 |
} |
| 1945 |
|
| 1946 |
/* |
| 1947 |
* PREPARE callback (for streaming two-phase commit). |
| 1948 |
* |
| 1949 |
* Notify the downstream to prepare the transaction. |
| 1950 |
*/ |
| 1951 |
static void |
| 1952 |
pgoutput_stream_prepare_txn(LogicalDecodingContext *ctx, |
| 1953 |
ReorderBufferTXN *txn, |
| 1954 |
XLogRecPtr prepare_lsn) |
| 1955 |
{ |
| 1956 |
Assert(rbtxn_is_streamed(txn)); |
| 1957 |
|
| 1958 |
OutputPluginUpdateProgress(ctx, false); |
| 1959 |
OutputPluginPrepareWrite(ctx, true); |
| 1960 |
logicalrep_write_stream_prepare(ctx->out, txn, prepare_lsn); |
| 1961 |
OutputPluginWrite(ctx, true); |
| 1962 |
} |
| 1963 |
|
| 1964 |
/* |
| 1965 |
* Initialize the relation schema sync cache for a decoding session. |
| 1966 |
* |
| 1967 |
* The hash table is destroyed at the end of a decoding session. While |
| 1968 |
* relcache invalidations still exist and will still be invoked, they |
| 1969 |
* will just see the null hash table global and take no action. |
| 1970 |
*/ |
| 1971 |
static void |
| 1972 |
init_rel_sync_cache(MemoryContext cachectx) |
| 1973 |
{ |
| 1974 |
HASHCTL ctl; |
| 1975 |
static bool relation_callbacks_registered = false; |
| 1976 |
|
| 1977 |
/* Nothing to do if hash table already exists */ |
| 1978 |
if (RelationSyncCache != NULL) |
| 1979 |
return; |
| 1980 |
|
| 1981 |
/* Make a new hash table for the cache */ |
| 1982 |
ctl.keysize = sizeof(Oid); |
| 1983 |
ctl.entrysize = sizeof(RelationSyncEntry); |
| 1984 |
ctl.hcxt = cachectx; |
| 1985 |
|
| 1986 |
RelationSyncCache = hash_create("logical replication output relation cache", |
| 1987 |
128, &ctl, |
| 1988 |
HASH_ELEM | HASH_CONTEXT | HASH_BLOBS); |
| 1989 |
|
| 1990 |
Assert(RelationSyncCache != NULL); |
| 1991 |
|
| 1992 |
/* No more to do if we already registered callbacks */ |
| 1993 |
if (relation_callbacks_registered) |
| 1994 |
return; |
| 1995 |
|
| 1996 |
/* We must update the cache entry for a relation after a relcache flush */ |
| 1997 |
CacheRegisterRelcacheCallback(rel_sync_cache_relation_cb, (Datum) 0); |
| 1998 |
|
| 1999 |
/* |
| 2000 |
* Flush all cache entries after a pg_namespace change, in case it was a |
| 2001 |
* schema rename affecting a relation being replicated. |
| 2002 |
* |
| 2003 |
* XXX: It is not a good idea to invalidate all the relation entries in |
| 2004 |
* RelationSyncCache on schema rename. We can optimize it to invalidate |
| 2005 |
* only the required relations by either having a specific invalidation |
| 2006 |
* message containing impacted relations or by having schema information |
| 2007 |
* in each RelationSyncCache entry and using hashvalue of pg_namespace.oid |
| 2008 |
* passed to the callback. |
| 2009 |
*/ |
| 2010 |
CacheRegisterSyscacheCallback(NAMESPACEOID, |
| 2011 |
rel_sync_cache_publication_cb, |
| 2012 |
(Datum) 0); |
| 2013 |
|
| 2014 |
relation_callbacks_registered = true; |
| 2015 |
} |
| 2016 |
|
| 2017 |
/* |
| 2018 |
* We expect relatively small number of streamed transactions. |
| 2019 |
*/ |
| 2020 |
static bool |
| 2021 |
get_schema_sent_in_streamed_txn(RelationSyncEntry *entry, TransactionId xid) |
| 2022 |
{ |
| 2023 |
return list_member_xid(entry->streamed_txns, xid); |
| 2024 |
} |
| 2025 |
|
| 2026 |
/* |
| 2027 |
* Add the xid in the rel sync entry for which we have already sent the schema |
| 2028 |
* of the relation. |
| 2029 |
*/ |
| 2030 |
static void |
| 2031 |
set_schema_sent_in_streamed_txn(RelationSyncEntry *entry, TransactionId xid) |
| 2032 |
{ |
| 2033 |
MemoryContext oldctx; |
| 2034 |
|
| 2035 |
oldctx = MemoryContextSwitchTo(CacheMemoryContext); |
| 2036 |
|
| 2037 |
entry->streamed_txns = lappend_xid(entry->streamed_txns, xid); |
| 2038 |
|
| 2039 |
MemoryContextSwitchTo(oldctx); |
| 2040 |
} |
| 2041 |
|
| 2042 |
/* |
| 2043 |
* Find or create entry in the relation schema cache. |
| 2044 |
* |
| 2045 |
* This looks up publications that the given relation is directly or |
| 2046 |
* indirectly part of (the latter if it's really the relation's ancestor that |
| 2047 |
* is part of a publication) and fills up the found entry with the information |
| 2048 |
* about which operations to publish and whether to use an ancestor's schema |
| 2049 |
* when publishing. |
| 2050 |
*/ |
| 2051 |
static RelationSyncEntry * |
| 2052 |
get_rel_sync_entry(PGOutputData *data, Relation relation) |
| 2053 |
{ |
| 2054 |
RelationSyncEntry *entry; |
| 2055 |
bool found; |
| 2056 |
MemoryContext oldctx; |
| 2057 |
Oid relid = RelationGetRelid(relation); |
| 2058 |
|
| 2059 |
Assert(RelationSyncCache != NULL); |
| 2060 |
|
| 2061 |
/* Find cached relation info, creating if not found */ |
| 2062 |
entry = (RelationSyncEntry *) hash_search(RelationSyncCache, |
| 2063 |
&relid, |
| 2064 |
HASH_ENTER, &found); |
| 2065 |
Assert(entry != NULL); |
| 2066 |
|
| 2067 |
/* initialize entry, if it's new */ |
| 2068 |
if (!found) |
| 2069 |
{ |
| 2070 |
entry->replicate_valid = false; |
| 2071 |
entry->schema_sent = false; |
| 2072 |
entry->include_gencols_type = PUBLISH_GENCOLS_NONE; |
| 2073 |
entry->streamed_txns = NIL; |
| 2074 |
entry->pubactions.pubinsert = entry->pubactions.pubupdate = |
| 2075 |
entry->pubactions.pubdelete = entry->pubactions.pubtruncate = false; |
| 2076 |
entry->new_slot = NULL; |
| 2077 |
entry->old_slot = NULL; |
| 2078 |
memset(entry->exprstate, 0, sizeof(entry->exprstate)); |
| 2079 |
entry->entry_cxt = NULL; |
| 2080 |
entry->publish_as_relid = InvalidOid; |
| 2081 |
entry->columns = NULL; |
| 2082 |
entry->attrmap = NULL; |
| 2083 |
} |
| 2084 |
|
| 2085 |
/* Validate the entry */ |
| 2086 |
if (!entry->replicate_valid) |
| 2087 |
{ |
| 2088 |
Oid schemaId = get_rel_namespace(relid); |
| 2089 |
List *pubids = GetRelationPublications(relid); |
| 2090 |
|
| 2091 |
/* |
| 2092 |
* We don't acquire a lock on the namespace system table as we build |
| 2093 |
* the cache entry using a historic snapshot and all the later changes |
| 2094 |
* are absorbed while decoding WAL. |
| 2095 |
*/ |
| 2096 |
List *schemaPubids = GetSchemaPublications(schemaId); |
| 2097 |
ListCell *lc; |
| 2098 |
Oid publish_as_relid = relid; |
| 2099 |
int publish_ancestor_level = 0; |
| 2100 |
bool am_partition = get_rel_relispartition(relid); |
| 2101 |
char relkind = get_rel_relkind(relid); |
| 2102 |
List *rel_publications = NIL; |
| 2103 |
|
| 2104 |
/* Reload publications if needed before use. */ |
| 2105 |
if (!publications_valid) |
| 2106 |
{ |
| 2107 |
MemoryContextReset(data->pubctx); |
| 2108 |
|
| 2109 |
oldctx = MemoryContextSwitchTo(data->pubctx); |
| 2110 |
data->publications = LoadPublications(data->publication_names); |
| 2111 |
MemoryContextSwitchTo(oldctx); |
| 2112 |
publications_valid = true; |
| 2113 |
} |
| 2114 |
|
| 2115 |
/* |
| 2116 |
* Reset schema_sent status as the relation definition may have |
| 2117 |
* changed. Also reset pubactions to empty in case rel was dropped |
| 2118 |
* from a publication. Also free any objects that depended on the |
| 2119 |
* earlier definition. |
| 2120 |
*/ |
| 2121 |
entry->schema_sent = false; |
| 2122 |
entry->include_gencols_type = PUBLISH_GENCOLS_NONE; |
| 2123 |
list_free(entry->streamed_txns); |
| 2124 |
entry->streamed_txns = NIL; |
| 2125 |
bms_free(entry->columns); |
| 2126 |
entry->columns = NULL; |
| 2127 |
entry->pubactions.pubinsert = false; |
| 2128 |
entry->pubactions.pubupdate = false; |
| 2129 |
entry->pubactions.pubdelete = false; |
| 2130 |
entry->pubactions.pubtruncate = false; |
| 2131 |
|
| 2132 |
/* |
| 2133 |
* Tuple slots cleanups. (Will be rebuilt later if needed). |
| 2134 |
*/ |
| 2135 |
if (entry->old_slot) |
| 2136 |
{ |
| 2137 |
TupleDesc desc = entry->old_slot->tts_tupleDescriptor; |
| 2138 |
|
| 2139 |
Assert(desc->tdrefcount == -1); |
| 2140 |
|
| 2141 |
ExecDropSingleTupleTableSlot(entry->old_slot); |
| 2142 |
|
| 2143 |
/* |
| 2144 |
* ExecDropSingleTupleTableSlot() would not free the TupleDesc, so |
| 2145 |
* do it now to avoid any leaks. |
| 2146 |
*/ |
| 2147 |
FreeTupleDesc(desc); |
| 2148 |
} |
| 2149 |
if (entry->new_slot) |
| 2150 |
{ |
| 2151 |
TupleDesc desc = entry->new_slot->tts_tupleDescriptor; |
| 2152 |
|
| 2153 |
Assert(desc->tdrefcount == -1); |
| 2154 |
|
| 2155 |
ExecDropSingleTupleTableSlot(entry->new_slot); |
| 2156 |
|
| 2157 |
/* |
| 2158 |
* ExecDropSingleTupleTableSlot() would not free the TupleDesc, so |
| 2159 |
* do it now to avoid any leaks. |
| 2160 |
*/ |
| 2161 |
FreeTupleDesc(desc); |
| 2162 |
} |
| 2163 |
|
| 2164 |
entry->old_slot = NULL; |
| 2165 |
entry->new_slot = NULL; |
| 2166 |
|
| 2167 |
if (entry->attrmap) |
| 2168 |
free_attrmap(entry->attrmap); |
| 2169 |
entry->attrmap = NULL; |
| 2170 |
|
| 2171 |
/* |
| 2172 |
* Row filter cache cleanups. |
| 2173 |
*/ |
| 2174 |
if (entry->entry_cxt) |
| 2175 |
MemoryContextDelete(entry->entry_cxt); |
| 2176 |
|
| 2177 |
entry->entry_cxt = NULL; |
| 2178 |
entry->estate = NULL; |
| 2179 |
memset(entry->exprstate, 0, sizeof(entry->exprstate)); |
| 2180 |
|
| 2181 |
/* |
| 2182 |
* Build publication cache. We can't use one provided by relcache as |
| 2183 |
* relcache considers all publications that the given relation is in, |
| 2184 |
* but here we only need to consider ones that the subscriber |
| 2185 |
* requested. |
| 2186 |
*/ |
| 2187 |
foreach(lc, data->publications) |
| 2188 |
{ |
| 2189 |
Publication *pub = lfirst(lc); |
| 2190 |
bool publish = false; |
| 2191 |
|
| 2192 |
/* |
| 2193 |
* Under what relid should we publish changes in this publication? |
| 2194 |
* We'll use the top-most relid across all publications. Also |
| 2195 |
* track the ancestor level for this publication. |
| 2196 |
*/ |
| 2197 |
Oid pub_relid = relid; |
| 2198 |
int ancestor_level = 0; |
| 2199 |
|
| 2200 |
/* |
| 2201 |
* If this is a FOR ALL TABLES publication, pick the partition |
| 2202 |
* root and set the ancestor level accordingly. |
| 2203 |
*/ |
| 2204 |
if (pub->alltables) |
| 2205 |
{ |
| 2206 |
publish = true; |
| 2207 |
if (pub->pubviaroot && am_partition) |
| 2208 |
{ |
| 2209 |
List *ancestors = get_partition_ancestors(relid); |
| 2210 |
|
| 2211 |
pub_relid = llast_oid(ancestors); |
| 2212 |
ancestor_level = list_length(ancestors); |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
if (!publish) |
| 2217 |
{ |
| 2218 |
bool ancestor_published = false; |
| 2219 |
|
| 2220 |
/* |
| 2221 |
* For a partition, check if any of the ancestors are |
| 2222 |
* published. If so, note down the topmost ancestor that is |
| 2223 |
* published via this publication, which will be used as the |
| 2224 |
* relation via which to publish the partition's changes. |
| 2225 |
*/ |
| 2226 |
if (am_partition) |
| 2227 |
{ |
| 2228 |
Oid ancestor; |
| 2229 |
int level; |
| 2230 |
List *ancestors = get_partition_ancestors(relid); |
| 2231 |
|
| 2232 |
ancestor = GetTopMostAncestorInPublication(pub->oid, |
| 2233 |
ancestors, |
| 2234 |
&level); |
| 2235 |
|
| 2236 |
if (ancestor != InvalidOid) |
| 2237 |
{ |
| 2238 |
ancestor_published = true; |
| 2239 |
if (pub->pubviaroot) |
| 2240 |
{ |
| 2241 |
pub_relid = ancestor; |
| 2242 |
ancestor_level = level; |
| 2243 |
} |
| 2244 |
} |
| 2245 |
} |
| 2246 |
|
| 2247 |
if (list_member_oid(pubids, pub->oid) || |
| 2248 |
list_member_oid(schemaPubids, pub->oid) || |
| 2249 |
ancestor_published) |
| 2250 |
publish = true; |
| 2251 |
} |
| 2252 |
|
| 2253 |
/* |
| 2254 |
* If the relation is to be published, determine actions to |
| 2255 |
* publish, and list of columns, if appropriate. |
| 2256 |
* |
| 2257 |
* Don't publish changes for partitioned tables, because |
| 2258 |
* publishing those of its partitions suffices, unless partition |
| 2259 |
* changes won't be published due to pubviaroot being set. |
| 2260 |
*/ |
| 2261 |
if (publish && |
| 2262 |
(relkind != RELKIND_PARTITIONED_TABLE || pub->pubviaroot)) |
| 2263 |
{ |
| 2264 |
entry->pubactions.pubinsert |= pub->pubactions.pubinsert; |
| 2265 |
entry->pubactions.pubupdate |= pub->pubactions.pubupdate; |
| 2266 |
entry->pubactions.pubdelete |= pub->pubactions.pubdelete; |
| 2267 |
entry->pubactions.pubtruncate |= pub->pubactions.pubtruncate; |
| 2268 |
|
| 2269 |
/* |
| 2270 |
* We want to publish the changes as the top-most ancestor |
| 2271 |
* across all publications. So we need to check if the already |
| 2272 |
* calculated level is higher than the new one. If yes, we can |
| 2273 |
* ignore the new value (as it's a child). Otherwise the new |
| 2274 |
* value is an ancestor, so we keep it. |
| 2275 |
*/ |
| 2276 |
if (publish_ancestor_level > ancestor_level) |
| 2277 |
continue; |
| 2278 |
|
| 2279 |
/* |
| 2280 |
* If we found an ancestor higher up in the tree, discard the |
| 2281 |
* list of publications through which we replicate it, and use |
| 2282 |
* the new ancestor. |
| 2283 |
*/ |
| 2284 |
if (publish_ancestor_level < ancestor_level) |
| 2285 |
{ |
| 2286 |
publish_as_relid = pub_relid; |
| 2287 |
publish_ancestor_level = ancestor_level; |
| 2288 |
|
| 2289 |
/* reset the publication list for this relation */ |
| 2290 |
rel_publications = NIL; |
| 2291 |
} |
| 2292 |
else |
| 2293 |
{ |
| 2294 |
/* Same ancestor level, has to be the same OID. */ |
| 2295 |
Assert(publish_as_relid == pub_relid); |
| 2296 |
} |
| 2297 |
|
| 2298 |
/* Track publications for this ancestor. */ |
| 2299 |
rel_publications = lappend(rel_publications, pub); |
| 2300 |
} |
| 2301 |
} |
| 2302 |
|
| 2303 |
entry->publish_as_relid = publish_as_relid; |
| 2304 |
|
| 2305 |
/* |
| 2306 |
* Initialize the tuple slot, map, and row filter. These are only used |
| 2307 |
* when publishing inserts, updates, or deletes. |
| 2308 |
*/ |
| 2309 |
if (entry->pubactions.pubinsert || entry->pubactions.pubupdate || |
| 2310 |
entry->pubactions.pubdelete) |
| 2311 |
{ |
| 2312 |
/* Initialize the tuple slot and map */ |
| 2313 |
init_tuple_slot(data, relation, entry); |
| 2314 |
|
| 2315 |
/* Initialize the row filter */ |
| 2316 |
pgoutput_row_filter_init(data, rel_publications, entry); |
| 2317 |
|
| 2318 |
/* Check whether to publish generated columns. */ |
| 2319 |
check_and_init_gencol(data, rel_publications, entry); |
| 2320 |
|
| 2321 |
/* Initialize the column list */ |
| 2322 |
pgoutput_column_list_init(data, rel_publications, entry); |
| 2323 |
} |
| 2324 |
|
| 2325 |
list_free(pubids); |
| 2326 |
list_free(schemaPubids); |
| 2327 |
list_free(rel_publications); |
| 2328 |
|
| 2329 |
entry->replicate_valid = true; |
| 2330 |
} |
| 2331 |
|
| 2332 |
return entry; |
| 2333 |
} |
| 2334 |
|
| 2335 |
/* |
| 2336 |
* Cleanup list of streamed transactions and update the schema_sent flag. |
| 2337 |
* |
| 2338 |
* When a streamed transaction commits or aborts, we need to remove the |
| 2339 |
* toplevel XID from the schema cache. If the transaction aborted, the |
| 2340 |
* subscriber will simply throw away the schema records we streamed, so |
| 2341 |
* we don't need to do anything else. |
| 2342 |
* |
| 2343 |
* If the transaction is committed, the subscriber will update the relation |
| 2344 |
* cache - so tweak the schema_sent flag accordingly. |
| 2345 |
*/ |
| 2346 |
static void |
| 2347 |
cleanup_rel_sync_cache(TransactionId xid, bool is_commit) |
| 2348 |
{ |
| 2349 |
HASH_SEQ_STATUS hash_seq; |
| 2350 |
RelationSyncEntry *entry; |
| 2351 |
|
| 2352 |
Assert(RelationSyncCache != NULL); |
| 2353 |
|
| 2354 |
hash_seq_init(&hash_seq, RelationSyncCache); |
| 2355 |
while ((entry = hash_seq_search(&hash_seq)) != NULL) |
| 2356 |
{ |
| 2357 |
/* |
| 2358 |
* We can set the schema_sent flag for an entry that has committed xid |
| 2359 |
* in the list as that ensures that the subscriber would have the |
| 2360 |
* corresponding schema and we don't need to send it unless there is |
| 2361 |
* any invalidation for that relation. |
| 2362 |
*/ |
| 2363 |
foreach_xid(streamed_txn, entry->streamed_txns) |
| 2364 |
{ |
| 2365 |
if (xid == streamed_txn) |
| 2366 |
{ |
| 2367 |
if (is_commit) |
| 2368 |
entry->schema_sent = true; |
| 2369 |
|
| 2370 |
entry->streamed_txns = |
| 2371 |
foreach_delete_current(entry->streamed_txns, streamed_txn); |
| 2372 |
break; |
| 2373 |
} |
| 2374 |
} |
| 2375 |
} |
| 2376 |
} |
| 2377 |
|
| 2378 |
/* |
| 2379 |
* Relcache invalidation callback |
| 2380 |
*/ |
| 2381 |
static void |
| 2382 |
rel_sync_cache_relation_cb(Datum arg, Oid relid) |
| 2383 |
{ |
| 2384 |
RelationSyncEntry *entry; |
| 2385 |
|
| 2386 |
/* |
| 2387 |
* We can get here if the plugin was used in SQL interface as the |
| 2388 |
* RelationSyncCache is destroyed when the decoding finishes, but there is |
| 2389 |
* no way to unregister the relcache invalidation callback. |
| 2390 |
*/ |
| 2391 |
if (RelationSyncCache == NULL) |
| 2392 |
return; |
| 2393 |
|
| 2394 |
/* |
| 2395 |
* Nobody keeps pointers to entries in this hash table around outside |
| 2396 |
* logical decoding callback calls - but invalidation events can come in |
| 2397 |
* *during* a callback if we do any syscache access in the callback. |
| 2398 |
* Because of that we must mark the cache entry as invalid but not damage |
| 2399 |
* any of its substructure here. The next get_rel_sync_entry() call will |
| 2400 |
* rebuild it all. |
| 2401 |
*/ |
| 2402 |
if (OidIsValid(relid)) |
| 2403 |
{ |
| 2404 |
/* |
| 2405 |
* Getting invalidations for relations that aren't in the table is |
| 2406 |
* entirely normal. So we don't care if it's found or not. |
| 2407 |
*/ |
| 2408 |
entry = (RelationSyncEntry *) hash_search(RelationSyncCache, &relid, |
| 2409 |
HASH_FIND, NULL); |
| 2410 |
if (entry != NULL) |
| 2411 |
entry->replicate_valid = false; |
| 2412 |
} |
| 2413 |
else |
| 2414 |
{ |
| 2415 |
/* Whole cache must be flushed. */ |
| 2416 |
HASH_SEQ_STATUS status; |
| 2417 |
|
| 2418 |
hash_seq_init(&status, RelationSyncCache); |
| 2419 |
while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL) |
| 2420 |
{ |
| 2421 |
entry->replicate_valid = false; |
| 2422 |
} |
| 2423 |
} |
| 2424 |
} |
| 2425 |
|
| 2426 |
/* |
| 2427 |
* Publication relation/schema map syscache invalidation callback |
| 2428 |
* |
| 2429 |
* Called for invalidations on pg_namespace. |
| 2430 |
*/ |
| 2431 |
static void |
| 2432 |
rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue) |
| 2433 |
{ |
| 2434 |
HASH_SEQ_STATUS status; |
| 2435 |
RelationSyncEntry *entry; |
| 2436 |
|
| 2437 |
/* |
| 2438 |
* We can get here if the plugin was used in SQL interface as the |
| 2439 |
* RelationSyncCache is destroyed when the decoding finishes, but there is |
| 2440 |
* no way to unregister the invalidation callbacks. |
| 2441 |
*/ |
| 2442 |
if (RelationSyncCache == NULL) |
| 2443 |
return; |
| 2444 |
|
| 2445 |
/* |
| 2446 |
* We have no easy way to identify which cache entries this invalidation |
| 2447 |
* event might have affected, so just mark them all invalid. |
| 2448 |
*/ |
| 2449 |
hash_seq_init(&status, RelationSyncCache); |
| 2450 |
while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL) |
| 2451 |
{ |
| 2452 |
entry->replicate_valid = false; |
| 2453 |
} |
| 2454 |
} |
| 2455 |
|
| 2456 |
/* Send Replication origin */ |
| 2457 |
static void |
| 2458 |
send_repl_origin(LogicalDecodingContext *ctx, RepOriginId origin_id, |
| 2459 |
XLogRecPtr origin_lsn, bool send_origin) |
| 2460 |
{ |
| 2461 |
if (send_origin) |
| 2462 |
{ |
| 2463 |
char *origin; |
| 2464 |
|
| 2465 |
/*---------- |
| 2466 |
* XXX: which behaviour do we want here? |
| 2467 |
* |
| 2468 |
* Alternatives: |
| 2469 |
* - don't send origin message if origin name not found |
| 2470 |
* (that's what we do now) |
| 2471 |
* - throw error - that will break replication, not good |
| 2472 |
* - send some special "unknown" origin |
| 2473 |
*---------- |
| 2474 |
*/ |
| 2475 |
if (replorigin_by_oid(origin_id, true, &origin)) |
| 2476 |
{ |
| 2477 |
/* Message boundary */ |
| 2478 |
OutputPluginWrite(ctx, false); |
| 2479 |
OutputPluginPrepareWrite(ctx, true); |
| 2480 |
|
| 2481 |
logicalrep_write_origin(ctx->out, origin, origin_lsn); |
| 2482 |
} |
| 2483 |
} |
| 2484 |
} |