PostgreSQL Source Code  git master
pgoutput.c File Reference
#include "postgres.h"
#include "catalog/pg_publication.h"
#include "replication/logical.h"
#include "replication/logicalproto.h"
#include "replication/origin.h"
#include "replication/pgoutput.h"
#include "utils/inval.h"
#include "utils/int8.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
Include dependency graph for pgoutput.c:

Go to the source code of this file.

Data Structures

struct  RelationSyncEntry
 

Typedefs

typedef struct RelationSyncEntry RelationSyncEntry
 

Functions

void _PG_output_plugin_init (OutputPluginCallbacks *cb)
 
static void pgoutput_startup (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void pgoutput_shutdown (LogicalDecodingContext *ctx)
 
static void pgoutput_begin_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void pgoutput_commit_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void pgoutput_change (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
 
static bool pgoutput_origin_filter (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
static ListLoadPublications (List *pubnames)
 
static void publication_invalidation_cb (Datum arg, int cacheid, uint32 hashvalue)
 
static void init_rel_sync_cache (MemoryContext decoding_context)
 
static RelationSyncEntryget_rel_sync_entry (PGOutputData *data, Oid relid)
 
static void rel_sync_cache_relation_cb (Datum arg, Oid relid)
 
static void rel_sync_cache_publication_cb (Datum arg, int cacheid, uint32 hashvalue)
 
static void parse_output_parameters (List *options, uint32 *protocol_version, List **publication_names)
 

Variables

 PG_MODULE_MAGIC
 
static bool publications_valid
 
static HTABRelationSyncCache = NULL
 

Typedef Documentation

◆ RelationSyncEntry

Function Documentation

◆ _PG_output_plugin_init()

void _PG_output_plugin_init ( OutputPluginCallbacks cb)

Definition at line 74 of file pgoutput.c.

References AssertVariableIsOfType, OutputPluginCallbacks::begin_cb, OutputPluginCallbacks::change_cb, OutputPluginCallbacks::commit_cb, OutputPluginCallbacks::filter_by_origin_cb, pgoutput_begin_txn(), pgoutput_change(), pgoutput_commit_txn(), pgoutput_origin_filter(), pgoutput_shutdown(), pgoutput_startup(), OutputPluginCallbacks::shutdown_cb, and OutputPluginCallbacks::startup_cb.

75 {
77 
84 }
static void pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
Definition: pgoutput.c:260
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
Definition: pgoutput.c:74
static void pgoutput_shutdown(LogicalDecodingContext *ctx)
Definition: pgoutput.c:383
static void pgoutput_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:246
LogicalDecodeCommitCB commit_cb
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:35
static void pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: pgoutput.c:147
LogicalDecodeChangeCB change_cb
Definition: output_plugin.h:99
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: pgoutput.c:370
LogicalDecodeShutdownCB shutdown_cb
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
LogicalDecodeBeginCB begin_cb
Definition: output_plugin.h:98
static void pgoutput_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:210
LogicalDecodeFilterByOriginCB filter_by_origin_cb
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:779

◆ get_rel_sync_entry()

static RelationSyncEntry * get_rel_sync_entry ( PGOutputData data,
Oid  relid 
)
static

Definition at line 467 of file pgoutput.c.

References Publication::alltables, Assert, CacheMemoryContext, get_rel_name(), get_rel_relkind(), GetRelationPublications(), HASH_ENTER, hash_search(), lfirst, list_free(), list_free_deep(), list_member_oid(), LoadPublications(), MemoryContextSwitchTo(), Publication::oid, RelationSyncEntry::pubactions, Publication::pubactions, PublicationActions::pubdelete, PublicationActions::pubinsert, PGOutputData::publication_names, PGOutputData::publications, publications_valid, PublicationActions::pubupdate, RelationSyncEntry::relid, RELKIND_RELATION, RelationSyncEntry::replicate_valid, and RelationSyncEntry::schema_sent.

Referenced by pgoutput_change().

468 {
469  RelationSyncEntry *entry;
470  bool found;
471  MemoryContext oldctx;
472 
473  Assert(RelationSyncCache != NULL);
474 
475  /* Find cached function info, creating if not found */
478  (void *) &relid,
479  HASH_ENTER, &found);
480  MemoryContextSwitchTo(oldctx);
481  Assert(entry != NULL);
482 
483  /* Not found means schema wasn't sent */
484  if (!found || !entry->replicate_valid)
485  {
486  List *pubids = GetRelationPublications(relid);
487  ListCell *lc;
488 
489  /* Reload publications if needed before use. */
490  if (!publications_valid)
491  {
493  if (data->publications)
495 
497  MemoryContextSwitchTo(oldctx);
498  publications_valid = true;
499  }
500 
501  /*
502  * Build publication cache. We can't use one provided by relcache as
503  * relcache considers all publications given relation is in, but here
504  * we only need to consider ones that the subscriber requested.
505  */
506  entry->pubactions.pubinsert = entry->pubactions.pubupdate =
507  entry->pubactions.pubdelete = false;
508 
509  foreach(lc, data->publications)
510  {
511  Publication *pub = lfirst(lc);
512 
513  /*
514  * Skip tables that look like they are from a heap rewrite (see
515  * make_new_heap()). We need to skip them because the subscriber
516  * won't have a table by that name to receive the data. That
517  * means we won't ship the new data in, say, an added column with
518  * a DEFAULT, but if the user applies the same DDL manually on the
519  * subscriber, then this will work out for them.
520  *
521  * We only need to consider the alltables case, because such a
522  * transient heap won't be an explicit member of a publication.
523  */
524  if (pub->alltables)
525  {
526  char *relname = get_rel_name(relid);
527  unsigned int u;
528  int n;
529 
530  if (sscanf(relname, "pg_temp_%u%n", &u, &n) == 1 &&
531  relname[n] == '\0')
532  {
534  break;
535  }
536  }
537 
538  if (pub->alltables || list_member_oid(pubids, pub->oid))
539  {
540  entry->pubactions.pubinsert |= pub->pubactions.pubinsert;
541  entry->pubactions.pubupdate |= pub->pubactions.pubupdate;
542  entry->pubactions.pubdelete |= pub->pubactions.pubdelete;
543  }
544 
545  if (entry->pubactions.pubinsert && entry->pubactions.pubupdate &&
546  entry->pubactions.pubdelete)
547  break;
548  }
549 
550  list_free(pubids);
551 
552  entry->replicate_valid = true;
553  }
554 
555  if (!found)
556  entry->schema_sent = false;
557 
558  return entry;
559 }
PublicationActions pubactions
static bool publications_valid
Definition: pgoutput.c:46
bool replicate_valid
Definition: pgoutput.c:57
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1801
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
void list_free_deep(List *list)
Definition: list.c:1147
List * GetRelationPublications(Oid relid)
List * publication_names
Definition: pgoutput.h:26
static HTAB * RelationSyncCache
Definition: pgoutput.c:62
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
static List * LoadPublications(List *pubnames)
Definition: pgoutput.c:396
List * publications
Definition: pgoutput.h:27
void list_free(List *list)
Definition: list.c:1133
#define RELKIND_RELATION
Definition: pg_class.h:160
Definition: pg_list.h:45
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
PublicationActions pubactions
Definition: pgoutput.c:58

◆ init_rel_sync_cache()

static void init_rel_sync_cache ( MemoryContext  decoding_context)
static

Definition at line 435 of file pgoutput.c.

References Assert, CacheRegisterRelcacheCallback(), CacheRegisterSyscacheCallback(), HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, HASHCTL::keysize, MemoryContextSwitchTo(), MemSet, PUBLICATIONRELMAP, rel_sync_cache_publication_cb(), and rel_sync_cache_relation_cb().

Referenced by pgoutput_startup().

436 {
437  HASHCTL ctl;
438  MemoryContext old_ctxt;
439 
440  if (RelationSyncCache != NULL)
441  return;
442 
443  /* Make a new hash table for the cache */
444  MemSet(&ctl, 0, sizeof(ctl));
445  ctl.keysize = sizeof(Oid);
446  ctl.entrysize = sizeof(RelationSyncEntry);
447  ctl.hcxt = cachectx;
448 
449  old_ctxt = MemoryContextSwitchTo(cachectx);
450  RelationSyncCache = hash_create("logical replication output relation cache",
451  128, &ctl,
453  (void) MemoryContextSwitchTo(old_ctxt);
454 
455  Assert(RelationSyncCache != NULL);
456 
460  (Datum) 0);
461 }
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
static void rel_sync_cache_relation_cb(Datum arg, Oid relid)
Definition: pgoutput.c:565
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:853
unsigned int Oid
Definition: postgres_ext.h:31
struct RelationSyncEntry RelationSyncEntry
static void rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:603
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1431
static HTAB * RelationSyncCache
Definition: pgoutput.c:62
#define HASH_BLOBS
Definition: hsearch.h:88
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:372
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
#define Assert(condition)
Definition: c.h:670

◆ LoadPublications()

static List * LoadPublications ( List pubnames)
static

Definition at line 396 of file pgoutput.c.

References GetPublicationByName(), lappend(), lfirst, and NIL.

Referenced by get_rel_sync_entry().

397 {
398  List *result = NIL;
399  ListCell *lc;
400 
401  foreach(lc, pubnames)
402  {
403  char *pubname = (char *) lfirst(lc);
404  Publication *pub = GetPublicationByName(pubname, false);
405 
406  result = lappend(result, pub);
407  }
408 
409  return result;
410 }
#define NIL
Definition: pg_list.h:69
Publication * GetPublicationByName(const char *pubname, bool missing_ok)
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45

◆ parse_output_parameters()

static void parse_output_parameters ( List options,
uint32 protocol_version,
List **  publication_names 
)
static

Definition at line 87 of file pgoutput.c.

References DefElem::arg, Assert, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, IsA, lfirst, PG_UINT32_MAX, scanint8(), SplitIdentifierString(), and strVal.

Referenced by pgoutput_startup().

89 {
90  ListCell *lc;
91  bool protocol_version_given = false;
92  bool publication_names_given = false;
93 
94  foreach(lc, options)
95  {
96  DefElem *defel = (DefElem *) lfirst(lc);
97 
98  Assert(defel->arg == NULL || IsA(defel->arg, String));
99 
100  /* Check each param, whether or not we recognize it */
101  if (strcmp(defel->defname, "proto_version") == 0)
102  {
103  int64 parsed;
104 
105  if (protocol_version_given)
106  ereport(ERROR,
107  (errcode(ERRCODE_SYNTAX_ERROR),
108  errmsg("conflicting or redundant options")));
109  protocol_version_given = true;
110 
111  if (!scanint8(strVal(defel->arg), true, &parsed))
112  ereport(ERROR,
113  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
114  errmsg("invalid proto_version")));
115 
116  if (parsed > PG_UINT32_MAX || parsed < 0)
117  ereport(ERROR,
118  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
119  errmsg("proto_version \"%s\" out of range",
120  strVal(defel->arg))));
121 
122  *protocol_version = (uint32) parsed;
123  }
124  else if (strcmp(defel->defname, "publication_names") == 0)
125  {
126  if (publication_names_given)
127  ereport(ERROR,
128  (errcode(ERRCODE_SYNTAX_ERROR),
129  errmsg("conflicting or redundant options")));
130  publication_names_given = true;
131 
132  if (!SplitIdentifierString(strVal(defel->arg), ',',
133  publication_names))
134  ereport(ERROR,
135  (errcode(ERRCODE_INVALID_NAME),
136  errmsg("invalid publication_names syntax")));
137  }
138  else
139  elog(ERROR, "unrecognized pgoutput option: %s", defel->defname);
140  }
141 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_UINT32_MAX
Definition: c.h:380
#define ERROR
Definition: elog.h:43
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3264
unsigned int uint32
Definition: c.h:296
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:720
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
#define elog
Definition: elog.h:219
bool scanint8(const char *str, bool errorOK, int64 *result)
Definition: int8.c:55

◆ pgoutput_begin_txn()

static void pgoutput_begin_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 210 of file pgoutput.c.

References InvalidRepOriginId, logicalrep_write_begin(), logicalrep_write_origin(), ReorderBufferTXN::origin_id, ReorderBufferTXN::origin_lsn, LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginWrite(), and replorigin_by_oid().

Referenced by _PG_output_plugin_init().

211 {
212  bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
213 
214  OutputPluginPrepareWrite(ctx, !send_replication_origin);
215  logicalrep_write_begin(ctx->out, txn);
216 
217  if (send_replication_origin)
218  {
219  char *origin;
220 
221  /* Message boundary */
222  OutputPluginWrite(ctx, false);
223  OutputPluginPrepareWrite(ctx, true);
224 
225  /*----------
226  * XXX: which behaviour do we want here?
227  *
228  * Alternatives:
229  * - don't send origin message if origin name not found
230  * (that's what we do now)
231  * - throw error - that will break replication, not good
232  * - send some special "unknown" origin
233  *----------
234  */
235  if (replorigin_by_oid(txn->origin_id, true, &origin))
236  logicalrep_write_origin(ctx->out, origin, txn->origin_lsn);
237  }
238 
239  OutputPluginWrite(ctx, true);
240 }
RepOriginId origin_id
void logicalrep_write_origin(StringInfo out, const char *origin, XLogRecPtr origin_lsn)
Definition: proto.c:110
bool replorigin_by_oid(RepOriginId roident, bool missing_ok, char **roname)
Definition: origin.c:417
XLogRecPtr origin_lsn
void logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn)
Definition: proto.c:43
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:490
#define InvalidRepOriginId
Definition: origin.h:34
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:503
StringInfo out
Definition: logical.h:66

◆ pgoutput_change()

static void pgoutput_change ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
Relation  rel,
ReorderBufferChange change 
)
static

Definition at line 260 of file pgoutput.c.

References ReorderBufferChange::action, Assert, PGOutputData::context, ReorderBufferChange::data, DEBUG1, elog, FirstNormalObjectId, get_rel_sync_entry(), i, logicalrep_write_delete(), logicalrep_write_insert(), logicalrep_write_rel(), logicalrep_write_typ(), logicalrep_write_update(), MemoryContextReset(), MemoryContextSwitchTo(), tupleDesc::natts, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), RelationGetDescr, RelationGetRelid, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, ReorderBufferChange::tp, and TupleDescAttr.

Referenced by _PG_output_plugin_init().

262 {
264  MemoryContext old;
265  RelationSyncEntry *relentry;
266 
267  relentry = get_rel_sync_entry(data, RelationGetRelid(relation));
268 
269  /* First check the table filter */
270  switch (change->action)
271  {
273  if (!relentry->pubactions.pubinsert)
274  return;
275  break;
277  if (!relentry->pubactions.pubupdate)
278  return;
279  break;
281  if (!relentry->pubactions.pubdelete)
282  return;
283  break;
284  default:
285  Assert(false);
286  }
287 
288  /* Avoid leaking memory by using and resetting our own context */
289  old = MemoryContextSwitchTo(data->context);
290 
291  /*
292  * Write the relation schema if the current schema haven't been sent yet.
293  */
294  if (!relentry->schema_sent)
295  {
296  TupleDesc desc;
297  int i;
298 
299  desc = RelationGetDescr(relation);
300 
301  /*
302  * Write out type info if needed. We do that only for user created
303  * types.
304  */
305  for (i = 0; i < desc->natts; i++)
306  {
307  Form_pg_attribute att = TupleDescAttr(desc, i);
308 
309  if (att->attisdropped)
310  continue;
311 
312  if (att->atttypid < FirstNormalObjectId)
313  continue;
314 
315  OutputPluginPrepareWrite(ctx, false);
316  logicalrep_write_typ(ctx->out, att->atttypid);
317  OutputPluginWrite(ctx, false);
318  }
319 
320  OutputPluginPrepareWrite(ctx, false);
321  logicalrep_write_rel(ctx->out, relation);
322  OutputPluginWrite(ctx, false);
323  relentry->schema_sent = true;
324  }
325 
326  /* Send the data */
327  switch (change->action)
328  {
330  OutputPluginPrepareWrite(ctx, true);
331  logicalrep_write_insert(ctx->out, relation,
332  &change->data.tp.newtuple->tuple);
333  OutputPluginWrite(ctx, true);
334  break;
336  {
337  HeapTuple oldtuple = change->data.tp.oldtuple ?
338  &change->data.tp.oldtuple->tuple : NULL;
339 
340  OutputPluginPrepareWrite(ctx, true);
341  logicalrep_write_update(ctx->out, relation, oldtuple,
342  &change->data.tp.newtuple->tuple);
343  OutputPluginWrite(ctx, true);
344  break;
345  }
347  if (change->data.tp.oldtuple)
348  {
349  OutputPluginPrepareWrite(ctx, true);
350  logicalrep_write_delete(ctx->out, relation,
351  &change->data.tp.oldtuple->tuple);
352  OutputPluginWrite(ctx, true);
353  }
354  else
355  elog(DEBUG1, "didn't send DELETE change because of missing oldtuple");
356  break;
357  default:
358  Assert(false);
359  }
360 
361  /* Cleanup */
364 }
void logicalrep_write_typ(StringInfo out, Oid typoid)
Definition: proto.c:349
#define DEBUG1
Definition: elog.h:25
#define RelationGetDescr(relation)
Definition: rel.h:437
MemoryContext context
Definition: pgoutput.h:20
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
union ReorderBufferChange::@93 data
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * output_plugin_private
Definition: logical.h:71
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:77
int natts
Definition: tupdesc.h:79
#define FirstNormalObjectId
Definition: transam.h:94
struct ReorderBufferChange::@93::@94 tp
void logicalrep_write_delete(StringInfo out, Relation rel, HeapTuple oldtuple)
Definition: proto.c:252
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
void logicalrep_write_insert(StringInfo out, Relation rel, HeapTuple newtuple)
Definition: proto.c:139
static RelationSyncEntry * get_rel_sync_entry(PGOutputData *data, Oid relid)
Definition: pgoutput.c:467
void logicalrep_write_rel(StringInfo out, Relation rel)
Definition: proto.c:299
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:490
void logicalrep_write_update(StringInfo out, Relation rel, HeapTuple oldtuple, HeapTuple newtuple)
Definition: proto.c:182
#define Assert(condition)
Definition: c.h:670
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:503
StringInfo out
Definition: logical.h:66
int i
#define elog
Definition: elog.h:219
#define RelationGetRelid(relation)
Definition: rel.h:425

◆ pgoutput_commit_txn()

static void pgoutput_commit_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 246 of file pgoutput.c.

References logicalrep_write_commit(), LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginUpdateProgress(), and OutputPluginWrite().

Referenced by _PG_output_plugin_init().

248 {
250 
251  OutputPluginPrepareWrite(ctx, true);
252  logicalrep_write_commit(ctx->out, txn, commit_lsn);
253  OutputPluginWrite(ctx, true);
254 }
void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:72
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx)
Definition: logical.c:516
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:490
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:503
StringInfo out
Definition: logical.h:66

◆ pgoutput_origin_filter()

static bool pgoutput_origin_filter ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)
static

Definition at line 370 of file pgoutput.c.

Referenced by _PG_output_plugin_init().

372 {
373  return false;
374 }

◆ pgoutput_shutdown()

static void pgoutput_shutdown ( LogicalDecodingContext ctx)
static

Definition at line 383 of file pgoutput.c.

References hash_destroy().

Referenced by _PG_output_plugin_init().

384 {
385  if (RelationSyncCache)
386  {
388  RelationSyncCache = NULL;
389  }
390 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:810
static HTAB * RelationSyncCache
Definition: pgoutput.c:62

◆ pgoutput_startup()

static void pgoutput_startup ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

Definition at line 147 of file pgoutput.c.

References ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE, ALLOCSET_DEFAULT_MINSIZE, AllocSetContextCreate(), CacheMemoryContext, CacheRegisterSyscacheCallback(), PGOutputData::context, LogicalDecodingContext::context, ereport, errcode(), errmsg(), ERROR, init_rel_sync_cache(), list_length(), LOGICALREP_PROTO_MIN_VERSION_NUM, LOGICALREP_PROTO_VERSION_NUM, NIL, OUTPUT_PLUGIN_BINARY_OUTPUT, LogicalDecodingContext::output_plugin_options, LogicalDecodingContext::output_plugin_private, OutputPluginOptions::output_type, palloc0(), parse_output_parameters(), PGOutputData::protocol_version, publication_invalidation_cb(), PGOutputData::publication_names, PUBLICATIONOID, PGOutputData::publications, and publications_valid.

Referenced by _PG_output_plugin_init().

149 {
150  PGOutputData *data = palloc0(sizeof(PGOutputData));
151 
152  /* Create our memory context for private allocations. */
154  "logical replication output context",
158 
159  ctx->output_plugin_private = data;
160 
161  /* This plugin uses binary protocol. */
163 
164  /*
165  * This is replication start and not slot initialization.
166  *
167  * Parse and validate options passed by the client.
168  */
169  if (!is_init)
170  {
171  /* Parse the params and ERROR if we see any we don't recognize */
173  &data->protocol_version,
174  &data->publication_names);
175 
176  /* Check if we support requested protocol */
178  ereport(ERROR,
179  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
180  errmsg("client sent proto_version=%d but we only support protocol %d or lower",
182 
184  ereport(ERROR,
185  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
186  errmsg("client sent proto_version=%d but we only support protocol %d or higher",
188 
189  if (list_length(data->publication_names) < 1)
190  ereport(ERROR,
191  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
192  errmsg("publication_names parameter missing")));
193 
194  /* Init publication state. */
195  data->publications = NIL;
196  publications_valid = false;
199  (Datum) 0);
200 
201  /* Initialize relation schema cache. */
203  }
204 }
#define NIL
Definition: pg_list.h:69
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
static bool publications_valid
Definition: pgoutput.c:46
static void publication_invalidation_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:416
MemoryContext context
Definition: pgoutput.h:20
int errcode(int sqlerrcode)
Definition: elog.c:575
void * output_plugin_private
Definition: logical.h:71
MemoryContext context
Definition: logical.h:38
List * output_plugin_options
Definition: logical.h:54
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:167
OutputPluginOutputType output_type
Definition: output_plugin.h:28
#define ERROR
Definition: elog.h:43
#define LOGICALREP_PROTO_MIN_VERSION_NUM
Definition: logicalproto.h:27
List * publication_names
Definition: pgoutput.h:26
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
void * palloc0(Size size)
Definition: mcxt.c:877
uintptr_t Datum
Definition: postgres.h:372
static int list_length(const List *l)
Definition: pg_list.h:89
static void parse_output_parameters(List *options, uint32 *protocol_version, List **publication_names)
Definition: pgoutput.c:87
List * publications
Definition: pgoutput.h:27
static void init_rel_sync_cache(MemoryContext decoding_context)
Definition: pgoutput.c:435
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:168
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:169
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
uint32 protocol_version
Definition: pgoutput.h:24

◆ publication_invalidation_cb()

static void publication_invalidation_cb ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 416 of file pgoutput.c.

References publications_valid, and rel_sync_cache_publication_cb().

Referenced by pgoutput_startup().

417 {
418  publications_valid = false;
419 
420  /*
421  * Also invalidate per-relation cache so that next time the filtering info
422  * is checked it will be updated with the new publication settings.
423  */
424  rel_sync_cache_publication_cb(arg, cacheid, hashvalue);
425 }
static bool publications_valid
Definition: pgoutput.c:46
static void rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:603
void * arg

◆ rel_sync_cache_publication_cb()

static void rel_sync_cache_publication_cb ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 603 of file pgoutput.c.

References hash_seq_init(), hash_seq_search(), RelationSyncEntry::replicate_valid, and status().

Referenced by init_rel_sync_cache(), and publication_invalidation_cb().

604 {
606  RelationSyncEntry *entry;
607 
608  /*
609  * We can get here if the plugin was used in SQL interface as the
610  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
611  * is no way to unregister the relcache invalidation callback.
612  */
613  if (RelationSyncCache == NULL)
614  return;
615 
616  /*
617  * There is no way to find which entry in our cache the hash belongs to so
618  * mark the whole cache as invalid.
619  */
621  while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
622  entry->replicate_valid = false;
623 }
bool replicate_valid
Definition: pgoutput.c:57
static HTAB * RelationSyncCache
Definition: pgoutput.c:62
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1385
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1375
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ rel_sync_cache_relation_cb()

static void rel_sync_cache_relation_cb ( Datum  arg,
Oid  relid 
)
static

Definition at line 565 of file pgoutput.c.

References HASH_FIND, hash_search(), and RelationSyncEntry::schema_sent.

Referenced by init_rel_sync_cache().

566 {
567  RelationSyncEntry *entry;
568 
569  /*
570  * We can get here if the plugin was used in SQL interface as the
571  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
572  * is no way to unregister the relcache invalidation callback.
573  */
574  if (RelationSyncCache == NULL)
575  return;
576 
577  /*
578  * Nobody keeps pointers to entries in this hash table around outside
579  * logical decoding callback calls - but invalidation events can come in
580  * *during* a callback if we access the relcache in the callback. Because
581  * of that we must mark the cache entry as invalid but not remove it from
582  * the hash while it could still be referenced, then prune it at a later
583  * safe point.
584  *
585  * Getting invalidations for relations that aren't in the table is
586  * entirely normal, since there's no way to unregister for an invalidation
587  * event. So we don't care if it's found or not.
588  */
589  entry = (RelationSyncEntry *) hash_search(RelationSyncCache, &relid,
590  HASH_FIND, NULL);
591 
592  /*
593  * Reset schema sent status as the relation definition may have changed.
594  */
595  if (entry != NULL)
596  entry->schema_sent = false;
597 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
static HTAB * RelationSyncCache
Definition: pgoutput.c:62

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 29 of file pgoutput.c.

◆ publications_valid

bool publications_valid
static

Definition at line 46 of file pgoutput.c.

Referenced by get_rel_sync_entry(), pgoutput_startup(), and publication_invalidation_cb().

◆ RelationSyncCache

HTAB* RelationSyncCache = NULL
static

Definition at line 62 of file pgoutput.c.