PostgreSQL Source Code  git master
pgoutput.c File Reference
#include "postgres.h"
#include "catalog/pg_publication.h"
#include "fmgr.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/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 void pgoutput_truncate (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, int nrelations, Relation relations[], 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)
 
static void maybe_send_schema (LogicalDecodingContext *ctx, Relation relation, RelationSyncEntry *relentry)
 

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 78 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(), pgoutput_truncate(), OutputPluginCallbacks::shutdown_cb, OutputPluginCallbacks::startup_cb, and OutputPluginCallbacks::truncate_cb.

79 {
81 
89 }
LogicalDecodeTruncateCB truncate_cb
static void pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
Definition: pgoutput.c:307
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
Definition: pgoutput.c:78
static void pgoutput_shutdown(LogicalDecodingContext *ctx)
Definition: pgoutput.c:448
static void pgoutput_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:249
LogicalDecodeCommitCB commit_cb
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
static void pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: pgoutput.c:152
LogicalDecodeChangeCB change_cb
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: pgoutput.c:435
LogicalDecodeShutdownCB shutdown_cb
LogicalDecodeStartupCB startup_cb
LogicalDecodeBeginCB begin_cb
static void pgoutput_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: pgoutput.c:384
static void pgoutput_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:213
LogicalDecodeFilterByOriginCB filter_by_origin_cb
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:881

◆ get_rel_sync_entry()

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

Definition at line 532 of file pgoutput.c.

References Publication::alltables, Assert, CacheMemoryContext, 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::pubtruncate, PublicationActions::pubupdate, RelationSyncEntry::relid, RelationSyncEntry::replicate_valid, and RelationSyncEntry::schema_sent.

Referenced by pgoutput_change(), and pgoutput_truncate().

533 {
534  RelationSyncEntry *entry;
535  bool found;
536  MemoryContext oldctx;
537 
538  Assert(RelationSyncCache != NULL);
539 
540  /* Find cached function info, creating if not found */
543  (void *) &relid,
544  HASH_ENTER, &found);
545  MemoryContextSwitchTo(oldctx);
546  Assert(entry != NULL);
547 
548  /* Not found means schema wasn't sent */
549  if (!found || !entry->replicate_valid)
550  {
551  List *pubids = GetRelationPublications(relid);
552  ListCell *lc;
553 
554  /* Reload publications if needed before use. */
555  if (!publications_valid)
556  {
558  if (data->publications)
560 
562  MemoryContextSwitchTo(oldctx);
563  publications_valid = true;
564  }
565 
566  /*
567  * Build publication cache. We can't use one provided by relcache as
568  * relcache considers all publications given relation is in, but here
569  * we only need to consider ones that the subscriber requested.
570  */
571  entry->pubactions.pubinsert = entry->pubactions.pubupdate =
572  entry->pubactions.pubdelete = entry->pubactions.pubtruncate = false;
573 
574  foreach(lc, data->publications)
575  {
576  Publication *pub = lfirst(lc);
577 
578  if (pub->alltables || list_member_oid(pubids, pub->oid))
579  {
580  entry->pubactions.pubinsert |= pub->pubactions.pubinsert;
581  entry->pubactions.pubupdate |= pub->pubactions.pubupdate;
582  entry->pubactions.pubdelete |= pub->pubactions.pubdelete;
584  }
585 
586  if (entry->pubactions.pubinsert && entry->pubactions.pubupdate &&
587  entry->pubactions.pubdelete && entry->pubactions.pubtruncate)
588  break;
589  }
590 
591  list_free(pubids);
592 
593  entry->replicate_valid = true;
594  }
595 
596  if (!found)
597  entry->schema_sent = false;
598 
599  return entry;
600 }
PublicationActions pubactions
static bool publications_valid
Definition: pgoutput.c:50
bool replicate_valid
Definition: pgoutput.c:61
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
void list_free_deep(List *list)
Definition: list.c:1391
List * GetRelationPublications(Oid relid)
List * publication_names
Definition: pgoutput.h:26
static HTAB * RelationSyncCache
Definition: pgoutput.c:66
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
static List * LoadPublications(List *pubnames)
Definition: pgoutput.c:461
List * publications
Definition: pgoutput.h:27
void list_free(List *list)
Definition: list.c:1377
Definition: pg_list.h:50
MemoryContext CacheMemoryContext
Definition: mcxt.c:47
PublicationActions pubactions
Definition: pgoutput.c:62

◆ init_rel_sync_cache()

static void init_rel_sync_cache ( MemoryContext  decoding_context)
static

Definition at line 500 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().

501 {
502  HASHCTL ctl;
503  MemoryContext old_ctxt;
504 
505  if (RelationSyncCache != NULL)
506  return;
507 
508  /* Make a new hash table for the cache */
509  MemSet(&ctl, 0, sizeof(ctl));
510  ctl.keysize = sizeof(Oid);
511  ctl.entrysize = sizeof(RelationSyncEntry);
512  ctl.hcxt = cachectx;
513 
514  old_ctxt = MemoryContextSwitchTo(cachectx);
515  RelationSyncCache = hash_create("logical replication output relation cache",
516  128, &ctl,
518  (void) MemoryContextSwitchTo(old_ctxt);
519 
520  Assert(RelationSyncCache != NULL);
521 
525  (Datum) 0);
526 }
#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:606
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:955
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:644
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1468
static HTAB * RelationSyncCache
Definition: pgoutput.c:66
#define HASH_BLOBS
Definition: hsearch.h:88
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1426
uintptr_t Datum
Definition: postgres.h:367
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:732

◆ LoadPublications()

static List * LoadPublications ( List pubnames)
static

Definition at line 461 of file pgoutput.c.

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

Referenced by get_rel_sync_entry().

462 {
463  List *result = NIL;
464  ListCell *lc;
465 
466  foreach(lc, pubnames)
467  {
468  char *pubname = (char *) lfirst(lc);
469  Publication *pub = GetPublicationByName(pubname, false);
470 
471  result = lappend(result, pub);
472  }
473 
474  return result;
475 }
#define NIL
Definition: pg_list.h:65
Publication * GetPublicationByName(const char *pubname, bool missing_ok)
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
Definition: pg_list.h:50

◆ maybe_send_schema()

static void maybe_send_schema ( LogicalDecodingContext ctx,
Relation  relation,
RelationSyncEntry relentry 
)
static

Definition at line 263 of file pgoutput.c.

References FirstGenbkiObjectId, i, logicalrep_write_rel(), logicalrep_write_typ(), TupleDescData::natts, LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginWrite(), RelationGetDescr, RelationSyncEntry::schema_sent, and TupleDescAttr.

Referenced by pgoutput_change(), and pgoutput_truncate().

265 {
266  if (!relentry->schema_sent)
267  {
268  TupleDesc desc;
269  int i;
270 
271  desc = RelationGetDescr(relation);
272 
273  /*
274  * Write out type info if needed. We do that only for user-created
275  * types. We use FirstGenbkiObjectId as the cutoff, so that we only
276  * consider objects with hand-assigned OIDs to be "built in", not for
277  * instance any function or type defined in the information_schema.
278  * This is important because only hand-assigned OIDs can be expected
279  * to remain stable across major versions.
280  */
281  for (i = 0; i < desc->natts; i++)
282  {
283  Form_pg_attribute att = TupleDescAttr(desc, i);
284 
285  if (att->attisdropped || att->attgenerated)
286  continue;
287 
288  if (att->atttypid < FirstGenbkiObjectId)
289  continue;
290 
291  OutputPluginPrepareWrite(ctx, false);
292  logicalrep_write_typ(ctx->out, att->atttypid);
293  OutputPluginWrite(ctx, false);
294  }
295 
296  OutputPluginPrepareWrite(ctx, false);
297  logicalrep_write_rel(ctx->out, relation);
298  OutputPluginWrite(ctx, false);
299  relentry->schema_sent = true;
300  }
301 }
void logicalrep_write_typ(StringInfo out, Oid typoid)
Definition: proto.c:404
#define RelationGetDescr(relation)
Definition: rel.h:445
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
void logicalrep_write_rel(StringInfo out, Relation rel)
Definition: proto.c:354
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:527
#define FirstGenbkiObjectId
Definition: transam.h:139
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:540
StringInfo out
Definition: logical.h:71
int i

◆ parse_output_parameters()

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

Definition at line 92 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().

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

◆ pgoutput_begin_txn()

static void pgoutput_begin_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 213 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().

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

◆ pgoutput_change()

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

Definition at line 307 of file pgoutput.c.

References ReorderBufferChange::action, Assert, PGOutputData::context, ReorderBufferChange::data, DEBUG1, elog, get_rel_sync_entry(), is_publishable_relation(), logicalrep_write_delete(), logicalrep_write_insert(), logicalrep_write_update(), maybe_send_schema(), MemoryContextReset(), MemoryContextSwitchTo(), LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), RelationGetRelid, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, and ReorderBufferChange::tp.

Referenced by _PG_output_plugin_init().

309 {
311  MemoryContext old;
312  RelationSyncEntry *relentry;
313 
314  if (!is_publishable_relation(relation))
315  return;
316 
317  relentry = get_rel_sync_entry(data, RelationGetRelid(relation));
318 
319  /* First check the table filter */
320  switch (change->action)
321  {
323  if (!relentry->pubactions.pubinsert)
324  return;
325  break;
327  if (!relentry->pubactions.pubupdate)
328  return;
329  break;
331  if (!relentry->pubactions.pubdelete)
332  return;
333  break;
334  default:
335  Assert(false);
336  }
337 
338  /* Avoid leaking memory by using and resetting our own context */
339  old = MemoryContextSwitchTo(data->context);
340 
341  maybe_send_schema(ctx, relation, relentry);
342 
343  /* Send the data */
344  switch (change->action)
345  {
347  OutputPluginPrepareWrite(ctx, true);
348  logicalrep_write_insert(ctx->out, relation,
349  &change->data.tp.newtuple->tuple);
350  OutputPluginWrite(ctx, true);
351  break;
353  {
354  HeapTuple oldtuple = change->data.tp.oldtuple ?
355  &change->data.tp.oldtuple->tuple : NULL;
356 
357  OutputPluginPrepareWrite(ctx, true);
358  logicalrep_write_update(ctx->out, relation, oldtuple,
359  &change->data.tp.newtuple->tuple);
360  OutputPluginWrite(ctx, true);
361  break;
362  }
364  if (change->data.tp.oldtuple)
365  {
366  OutputPluginPrepareWrite(ctx, true);
367  logicalrep_write_delete(ctx->out, relation,
368  &change->data.tp.oldtuple->tuple);
369  OutputPluginWrite(ctx, true);
370  }
371  else
372  elog(DEBUG1, "didn't send DELETE change because of missing oldtuple");
373  break;
374  default:
375  Assert(false);
376  }
377 
378  /* Cleanup */
381 }
#define DEBUG1
Definition: elog.h:25
struct ReorderBufferChange::@101::@102 tp
MemoryContext context
Definition: pgoutput.h:20
static void maybe_send_schema(LogicalDecodingContext *ctx, Relation relation, RelationSyncEntry *relentry)
Definition: pgoutput.c:263
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * output_plugin_private
Definition: logical.h:76
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:78
bool is_publishable_relation(Relation rel)
void logicalrep_write_delete(StringInfo out, Relation rel, HeapTuple oldtuple)
Definition: proto.c:255
void logicalrep_write_insert(StringInfo out, Relation rel, HeapTuple newtuple)
Definition: proto.c:142
static RelationSyncEntry * get_rel_sync_entry(PGOutputData *data, Oid relid)
Definition: pgoutput.c:532
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:527
void logicalrep_write_update(StringInfo out, Relation rel, HeapTuple oldtuple, HeapTuple newtuple)
Definition: proto.c:185
#define Assert(condition)
Definition: c.h:732
union ReorderBufferChange::@101 data
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:540
#define elog(elevel,...)
Definition: elog.h:226
StringInfo out
Definition: logical.h:71
#define RelationGetRelid(relation)
Definition: rel.h:419

◆ pgoutput_commit_txn()

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

Definition at line 249 of file pgoutput.c.

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

Referenced by _PG_output_plugin_init().

251 {
253 
254  OutputPluginPrepareWrite(ctx, true);
255  logicalrep_write_commit(ctx->out, txn, commit_lsn);
256  OutputPluginWrite(ctx, true);
257 }
void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:75
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx)
Definition: logical.c:553
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:527
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:540
StringInfo out
Definition: logical.h:71

◆ pgoutput_origin_filter()

static bool pgoutput_origin_filter ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)
static

Definition at line 435 of file pgoutput.c.

Referenced by _PG_output_plugin_init().

437 {
438  return false;
439 }

◆ pgoutput_shutdown()

static void pgoutput_shutdown ( LogicalDecodingContext ctx)
static

Definition at line 448 of file pgoutput.c.

References hash_destroy().

Referenced by _PG_output_plugin_init().

449 {
450  if (RelationSyncCache)
451  {
453  RelationSyncCache = NULL;
454  }
455 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:814
static HTAB * RelationSyncCache
Definition: pgoutput.c:66

◆ pgoutput_startup()

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

Definition at line 152 of file pgoutput.c.

References ALLOCSET_DEFAULT_SIZES, 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().

154 {
155  PGOutputData *data = palloc0(sizeof(PGOutputData));
156 
157  /* Create our memory context for private allocations. */
159  "logical replication output context",
161 
162  ctx->output_plugin_private = data;
163 
164  /* This plugin uses binary protocol. */
166 
167  /*
168  * This is replication start and not slot initialization.
169  *
170  * Parse and validate options passed by the client.
171  */
172  if (!is_init)
173  {
174  /* Parse the params and ERROR if we see any we don't recognize */
176  &data->protocol_version,
177  &data->publication_names);
178 
179  /* Check if we support requested protocol */
181  ereport(ERROR,
182  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
183  errmsg("client sent proto_version=%d but we only support protocol %d or lower",
185 
187  ereport(ERROR,
188  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
189  errmsg("client sent proto_version=%d but we only support protocol %d or higher",
191 
192  if (list_length(data->publication_names) < 1)
193  ereport(ERROR,
194  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
195  errmsg("publication_names parameter missing")));
196 
197  /* Init publication state. */
198  data->publications = NIL;
199  publications_valid = false;
202  (Datum) 0);
203 
204  /* Initialize relation schema cache. */
206  }
207 }
#define NIL
Definition: pg_list.h:65
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
#define AllocSetContextCreate
Definition: memutils.h:170
static bool publications_valid
Definition: pgoutput.c:50
static void publication_invalidation_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:481
MemoryContext context
Definition: pgoutput.h:20
int errcode(int sqlerrcode)
Definition: elog.c:570
void * output_plugin_private
Definition: logical.h:76
MemoryContext context
Definition: logical.h:36
List * output_plugin_options
Definition: logical.h:59
OutputPluginOutputType output_type
Definition: output_plugin.h:28
#define ERROR
Definition: elog.h:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define LOGICALREP_PROTO_MIN_VERSION_NUM
Definition: logicalproto.h:27
List * publication_names
Definition: pgoutput.h:26
#define ereport(elevel, rest)
Definition: elog.h:141
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1426
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
static int list_length(const List *l)
Definition: pg_list.h:169
static void parse_output_parameters(List *options, uint32 *protocol_version, List **publication_names)
Definition: pgoutput.c:92
List * publications
Definition: pgoutput.h:27
static void init_rel_sync_cache(MemoryContext decoding_context)
Definition: pgoutput.c:500
int errmsg(const char *fmt,...)
Definition: elog.c:784
MemoryContext CacheMemoryContext
Definition: mcxt.c:47
uint32 protocol_version
Definition: pgoutput.h:24

◆ pgoutput_truncate()

static void pgoutput_truncate ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
int  nrelations,
Relation  relations[],
ReorderBufferChange change 
)
static

Definition at line 384 of file pgoutput.c.

References PGOutputData::context, ReorderBufferChange::data, get_rel_sync_entry(), i, is_publishable_relation(), logicalrep_write_truncate(), maybe_send_schema(), MemoryContextReset(), MemoryContextSwitchTo(), LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), palloc0(), RelationGetRelid, RelationSyncEntry::relid, and ReorderBufferChange::truncate.

Referenced by _PG_output_plugin_init().

386 {
388  MemoryContext old;
389  RelationSyncEntry *relentry;
390  int i;
391  int nrelids;
392  Oid *relids;
393 
394  old = MemoryContextSwitchTo(data->context);
395 
396  relids = palloc0(nrelations * sizeof(Oid));
397  nrelids = 0;
398 
399  for (i = 0; i < nrelations; i++)
400  {
401  Relation relation = relations[i];
402  Oid relid = RelationGetRelid(relation);
403 
404  if (!is_publishable_relation(relation))
405  continue;
406 
407  relentry = get_rel_sync_entry(data, relid);
408 
409  if (!relentry->pubactions.pubtruncate)
410  continue;
411 
412  relids[nrelids++] = relid;
413  maybe_send_schema(ctx, relation, relentry);
414  }
415 
416  if (nrelids > 0)
417  {
418  OutputPluginPrepareWrite(ctx, true);
420  nrelids,
421  relids,
422  change->data.truncate.cascade,
423  change->data.truncate.restart_seqs);
424  OutputPluginWrite(ctx, true);
425  }
426 
429 }
MemoryContext context
Definition: pgoutput.h:20
static void maybe_send_schema(LogicalDecodingContext *ctx, Relation relation, RelationSyncEntry *relentry)
Definition: pgoutput.c:263
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * output_plugin_private
Definition: logical.h:76
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
unsigned int Oid
Definition: postgres_ext.h:31
bool is_publishable_relation(Relation rel)
void logicalrep_write_truncate(StringInfo out, int nrelids, Oid relids[], bool cascade, bool restart_seqs)
Definition: proto.c:302
static RelationSyncEntry * get_rel_sync_entry(PGOutputData *data, Oid relid)
Definition: pgoutput.c:532
void * palloc0(Size size)
Definition: mcxt.c:980
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:527
union ReorderBufferChange::@101 data
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:540
StringInfo out
Definition: logical.h:71
int i
#define RelationGetRelid(relation)
Definition: rel.h:419
struct ReorderBufferChange::@101::@103 truncate

◆ publication_invalidation_cb()

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

Definition at line 481 of file pgoutput.c.

References publications_valid, and rel_sync_cache_publication_cb().

Referenced by pgoutput_startup().

482 {
483  publications_valid = false;
484 
485  /*
486  * Also invalidate per-relation cache so that next time the filtering info
487  * is checked it will be updated with the new publication settings.
488  */
489  rel_sync_cache_publication_cb(arg, cacheid, hashvalue);
490 }
static bool publications_valid
Definition: pgoutput.c:50
static void rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:644
void * arg

◆ rel_sync_cache_publication_cb()

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

Definition at line 644 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().

645 {
647  RelationSyncEntry *entry;
648 
649  /*
650  * We can get here if the plugin was used in SQL interface as the
651  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
652  * is no way to unregister the relcache invalidation callback.
653  */
654  if (RelationSyncCache == NULL)
655  return;
656 
657  /*
658  * There is no way to find which entry in our cache the hash belongs to so
659  * mark the whole cache as invalid.
660  */
662  while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
663  entry->replicate_valid = false;
664 }
bool replicate_valid
Definition: pgoutput.c:61
static HTAB * RelationSyncCache
Definition: pgoutput.c:66
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1389
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1379
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ rel_sync_cache_relation_cb()

static void rel_sync_cache_relation_cb ( Datum  arg,
Oid  relid 
)
static

Definition at line 606 of file pgoutput.c.

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

Referenced by init_rel_sync_cache().

607 {
608  RelationSyncEntry *entry;
609 
610  /*
611  * We can get here if the plugin was used in SQL interface as the
612  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
613  * is no way to unregister the relcache invalidation callback.
614  */
615  if (RelationSyncCache == NULL)
616  return;
617 
618  /*
619  * Nobody keeps pointers to entries in this hash table around outside
620  * logical decoding callback calls - but invalidation events can come in
621  * *during* a callback if we access the relcache in the callback. Because
622  * of that we must mark the cache entry as invalid but not remove it from
623  * the hash while it could still be referenced, then prune it at a later
624  * safe point.
625  *
626  * Getting invalidations for relations that aren't in the table is
627  * entirely normal, since there's no way to unregister for an invalidation
628  * event. So we don't care if it's found or not.
629  */
630  entry = (RelationSyncEntry *) hash_search(RelationSyncCache, &relid,
631  HASH_FIND, NULL);
632 
633  /*
634  * Reset schema sent status as the relation definition may have changed.
635  */
636  if (entry != NULL)
637  entry->schema_sent = false;
638 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
static HTAB * RelationSyncCache
Definition: pgoutput.c:66

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 30 of file pgoutput.c.

◆ publications_valid

bool publications_valid
static

Definition at line 50 of file pgoutput.c.

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

◆ RelationSyncCache

HTAB* RelationSyncCache = NULL
static

Definition at line 66 of file pgoutput.c.