PostgreSQL Source Code  git master
pgoutput.c File Reference
#include "postgres.h"
#include "access/tupconvert.h"
#include "catalog/partition.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/int8.h"
#include "utils/inval.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 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 send_relation_and_attrs (Relation relation, LogicalDecodingContext *ctx)
 
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 106 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.

107 {
109 
117 }
LogicalDecodeTruncateCB truncate_cb
static void pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
Definition: pgoutput.c:360
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
Definition: pgoutput.c:106
static void pgoutput_shutdown(LogicalDecodingContext *ctx)
Definition: pgoutput.c:546
static void pgoutput_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:277
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:180
LogicalDecodeChangeCB change_cb
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: pgoutput.c:533
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:474
static void pgoutput_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:241
LogicalDecodeFilterByOriginCB filter_by_origin_cb
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:904

◆ get_rel_sync_entry()

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

Definition at line 636 of file pgoutput.c.

References Publication::alltables, Assert, CacheMemoryContext, get_partition_ancestors(), get_rel_relispartition(), get_rel_relkind(), GetRelationPublications(), HASH_ENTER, hash_search(), lfirst, lfirst_oid, list_free(), list_free_deep(), list_member_oid(), llast_oid, LoadPublications(), MemoryContextSwitchTo(), Publication::oid, RelationSyncEntry::pubactions, Publication::pubactions, PublicationActions::pubdelete, PublicationActions::pubinsert, PGOutputData::publication_names, PGOutputData::publications, publications_valid, RelationSyncEntry::publish_as_relid, PublicationActions::pubtruncate, PublicationActions::pubupdate, Publication::pubviaroot, RelationSyncEntry::relid, RelationSyncEntry::replicate_valid, and RelationSyncEntry::schema_sent.

Referenced by pgoutput_change(), and pgoutput_truncate().

637 {
638  RelationSyncEntry *entry;
639  bool am_partition = get_rel_relispartition(relid);
640  char relkind = get_rel_relkind(relid);
641  bool found;
642  MemoryContext oldctx;
643 
644  Assert(RelationSyncCache != NULL);
645 
646  /* Find cached function info, creating if not found */
649  (void *) &relid,
650  HASH_ENTER, &found);
651  MemoryContextSwitchTo(oldctx);
652  Assert(entry != NULL);
653 
654  /* Not found means schema wasn't sent */
655  if (!found || !entry->replicate_valid)
656  {
657  List *pubids = GetRelationPublications(relid);
658  ListCell *lc;
659  Oid publish_as_relid = relid;
660 
661  /* Reload publications if needed before use. */
662  if (!publications_valid)
663  {
665  if (data->publications)
667 
669  MemoryContextSwitchTo(oldctx);
670  publications_valid = true;
671  }
672 
673  /*
674  * Build publication cache. We can't use one provided by relcache as
675  * relcache considers all publications given relation is in, but here
676  * we only need to consider ones that the subscriber requested.
677  */
678  entry->pubactions.pubinsert = entry->pubactions.pubupdate =
679  entry->pubactions.pubdelete = entry->pubactions.pubtruncate = false;
680 
681  foreach(lc, data->publications)
682  {
683  Publication *pub = lfirst(lc);
684  bool publish = false;
685 
686  if (pub->alltables)
687  {
688  publish = true;
689  if (pub->pubviaroot && am_partition)
690  publish_as_relid = llast_oid(get_partition_ancestors(relid));
691  }
692 
693  if (!publish)
694  {
695  bool ancestor_published = false;
696 
697  /*
698  * For a partition, check if any of the ancestors are
699  * published. If so, note down the topmost ancestor that is
700  * published via this publication, which will be used as the
701  * relation via which to publish the partition's changes.
702  */
703  if (am_partition)
704  {
705  List *ancestors = get_partition_ancestors(relid);
706  ListCell *lc2;
707 
708  /*
709  * Find the "topmost" ancestor that is in this
710  * publication.
711  */
712  foreach(lc2, ancestors)
713  {
714  Oid ancestor = lfirst_oid(lc2);
715 
717  pub->oid))
718  {
719  ancestor_published = true;
720  if (pub->pubviaroot)
721  publish_as_relid = ancestor;
722  }
723  }
724  }
725 
726  if (list_member_oid(pubids, pub->oid) || ancestor_published)
727  publish = true;
728  }
729 
730  /*
731  * Don't publish changes for partitioned tables, because
732  * publishing those of its partitions suffices, unless partition
733  * changes won't be published due to pubviaroot being set.
734  */
735  if (publish &&
736  (relkind != RELKIND_PARTITIONED_TABLE || pub->pubviaroot))
737  {
738  entry->pubactions.pubinsert |= pub->pubactions.pubinsert;
739  entry->pubactions.pubupdate |= pub->pubactions.pubupdate;
740  entry->pubactions.pubdelete |= pub->pubactions.pubdelete;
742  }
743 
744  if (entry->pubactions.pubinsert && entry->pubactions.pubupdate &&
745  entry->pubactions.pubdelete && entry->pubactions.pubtruncate)
746  break;
747  }
748 
749  list_free(pubids);
750 
751  entry->publish_as_relid = publish_as_relid;
752  entry->replicate_valid = true;
753  }
754 
755  if (!found)
756  entry->schema_sent = false;
757 
758  return entry;
759 }
PublicationActions pubactions
static bool publications_valid
Definition: pgoutput.c:50
bool replicate_valid
Definition: pgoutput.c:73
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1915
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define llast_oid(l)
Definition: pg_list.h:217
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:917
unsigned int Oid
Definition: postgres_ext.h:31
void list_free_deep(List *list)
Definition: list.c:1390
Oid publish_as_relid
Definition: pgoutput.c:82
List * GetRelationPublications(Oid relid)
List * publication_names
Definition: pgoutput.h:26
bool get_rel_relispartition(Oid relid)
Definition: lsyscache.c:1939
static HTAB * RelationSyncCache
Definition: pgoutput.c:94
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
static List * LoadPublications(List *pubnames)
Definition: pgoutput.c:559
List * publications
Definition: pgoutput.h:27
void list_free(List *list)
Definition: list.c:1376
Definition: pg_list.h:50
List * get_partition_ancestors(Oid relid)
Definition: partition.c:115
#define lfirst_oid(lc)
Definition: pg_list.h:192
MemoryContext CacheMemoryContext
Definition: mcxt.c:47
PublicationActions pubactions
Definition: pgoutput.c:74

◆ init_rel_sync_cache()

static void init_rel_sync_cache ( MemoryContext  decoding_context)
static

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

599 {
600  HASHCTL ctl;
601  MemoryContext old_ctxt;
602 
603  if (RelationSyncCache != NULL)
604  return;
605 
606  /* Make a new hash table for the cache */
607  MemSet(&ctl, 0, sizeof(ctl));
608  ctl.keysize = sizeof(Oid);
609  ctl.entrysize = sizeof(RelationSyncEntry);
610  ctl.hcxt = cachectx;
611 
612  old_ctxt = MemoryContextSwitchTo(cachectx);
613  RelationSyncCache = hash_create("logical replication output relation cache",
614  128, &ctl,
616  (void) MemoryContextSwitchTo(old_ctxt);
617 
618  Assert(RelationSyncCache != NULL);
619 
623  (Datum) 0);
624 }
#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:765
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:978
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:803
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1468
static HTAB * RelationSyncCache
Definition: pgoutput.c:94
#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:318
Size keysize
Definition: hsearch.h:72
#define Assert(condition)
Definition: c.h:745

◆ LoadPublications()

static List * LoadPublications ( List pubnames)
static

Definition at line 559 of file pgoutput.c.

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

Referenced by get_rel_sync_entry().

560 {
561  List *result = NIL;
562  ListCell *lc;
563 
564  foreach(lc, pubnames)
565  {
566  char *pubname = (char *) lfirst(lc);
567  Publication *pub = GetPublicationByName(pubname, false);
568 
569  result = lappend(result, pub);
570  }
571 
572  return result;
573 }
#define NIL
Definition: pg_list.h:65
Publication * GetPublicationByName(const char *pubname, bool missing_ok)
List * lappend(List *list, void *datum)
Definition: list.c:321
#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 292 of file pgoutput.c.

References CacheMemoryContext, convert_tuples_by_name(), CreateTupleDescCopy(), RelationSyncEntry::map, MemoryContextSwitchTo(), RelationSyncEntry::publish_as_relid, RelationClose(), RelationGetDescr, RelationGetRelid, RelationIdGetRelation(), RelationSyncEntry::schema_sent, and send_relation_and_attrs().

Referenced by pgoutput_change(), and pgoutput_truncate().

294 {
295  if (relentry->schema_sent)
296  return;
297 
298  /* If needed, send the ancestor's schema first. */
299  if (relentry->publish_as_relid != RelationGetRelid(relation))
300  {
301  Relation ancestor = RelationIdGetRelation(relentry->publish_as_relid);
302  TupleDesc indesc = RelationGetDescr(relation);
303  TupleDesc outdesc = RelationGetDescr(ancestor);
304  MemoryContext oldctx;
305 
306  /* Map must live as long as the session does. */
308  relentry->map = convert_tuples_by_name(CreateTupleDescCopy(indesc),
309  CreateTupleDescCopy(outdesc));
310  MemoryContextSwitchTo(oldctx);
311  send_relation_and_attrs(ancestor, ctx);
312  RelationClose(ancestor);
313  }
314 
315  send_relation_and_attrs(relation, ctx);
316  relentry->schema_sent = true;
317 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define RelationGetDescr(relation)
Definition: rel.h:482
TupleConversionMap * map
Definition: pgoutput.c:90
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Oid publish_as_relid
Definition: pgoutput.c:82
TupleConversionMap * convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc)
Definition: tupconvert.c:102
void RelationClose(Relation relation)
Definition: relcache.c:2103
static void send_relation_and_attrs(Relation relation, LogicalDecodingContext *ctx)
Definition: pgoutput.c:323
#define RelationGetRelid(relation)
Definition: rel.h:456
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:1997
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ parse_output_parameters()

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

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

122 {
123  ListCell *lc;
124  bool protocol_version_given = false;
125  bool publication_names_given = false;
126 
127  foreach(lc, options)
128  {
129  DefElem *defel = (DefElem *) lfirst(lc);
130 
131  Assert(defel->arg == NULL || IsA(defel->arg, String));
132 
133  /* Check each param, whether or not we recognize it */
134  if (strcmp(defel->defname, "proto_version") == 0)
135  {
136  int64 parsed;
137 
138  if (protocol_version_given)
139  ereport(ERROR,
140  (errcode(ERRCODE_SYNTAX_ERROR),
141  errmsg("conflicting or redundant options")));
142  protocol_version_given = true;
143 
144  if (!scanint8(strVal(defel->arg), true, &parsed))
145  ereport(ERROR,
146  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
147  errmsg("invalid proto_version")));
148 
149  if (parsed > PG_UINT32_MAX || parsed < 0)
150  ereport(ERROR,
151  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
152  errmsg("proto_version \"%s\" out of range",
153  strVal(defel->arg))));
154 
155  *protocol_version = (uint32) parsed;
156  }
157  else if (strcmp(defel->defname, "publication_names") == 0)
158  {
159  if (publication_names_given)
160  ereport(ERROR,
161  (errcode(ERRCODE_SYNTAX_ERROR),
162  errmsg("conflicting or redundant options")));
163  publication_names_given = true;
164 
165  if (!SplitIdentifierString(strVal(defel->arg), ',',
166  publication_names))
167  ereport(ERROR,
168  (errcode(ERRCODE_INVALID_NAME),
169  errmsg("invalid publication_names syntax")));
170  }
171  else
172  elog(ERROR, "unrecognized pgoutput option: %s", defel->defname);
173  }
174 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_UINT32_MAX
Definition: c.h:458
#define ERROR
Definition: elog.h:43
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3686
unsigned int uint32
Definition: c.h:374
Node * arg
Definition: parsenodes.h:733
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
char * defname
Definition: parsenodes.h:732
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 241 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().

242 {
243  bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
244 
245  OutputPluginPrepareWrite(ctx, !send_replication_origin);
246  logicalrep_write_begin(ctx->out, txn);
247 
248  if (send_replication_origin)
249  {
250  char *origin;
251 
252  /* Message boundary */
253  OutputPluginWrite(ctx, false);
254  OutputPluginPrepareWrite(ctx, true);
255 
256  /*----------
257  * XXX: which behaviour do we want here?
258  *
259  * Alternatives:
260  * - don't send origin message if origin name not found
261  * (that's what we do now)
262  * - throw error - that will break replication, not good
263  * - send some special "unknown" origin
264  *----------
265  */
266  if (replorigin_by_oid(txn->origin_id, true, &origin))
267  logicalrep_write_origin(ctx->out, origin, txn->origin_lsn);
268  }
269 
270  OutputPluginWrite(ctx, true);
271 }
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:432
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:526
#define InvalidRepOriginId
Definition: origin.h:33
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:539
StringInfo out
Definition: logical.h:70

◆ pgoutput_change()

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

Definition at line 360 of file pgoutput.c.

References ReorderBufferChange::action, Assert, PGOutputData::context, ReorderBufferChange::data, DEBUG1, elog, execute_attr_map_tuple(), 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(), RelationData::rd_rel, RelationGetRelid, RelationIdGetRelation(), REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, and ReorderBufferChange::tp.

Referenced by _PG_output_plugin_init().

362 {
364  MemoryContext old;
365  RelationSyncEntry *relentry;
366 
367  if (!is_publishable_relation(relation))
368  return;
369 
370  relentry = get_rel_sync_entry(data, RelationGetRelid(relation));
371 
372  /* First check the table filter */
373  switch (change->action)
374  {
376  if (!relentry->pubactions.pubinsert)
377  return;
378  break;
380  if (!relentry->pubactions.pubupdate)
381  return;
382  break;
384  if (!relentry->pubactions.pubdelete)
385  return;
386  break;
387  default:
388  Assert(false);
389  }
390 
391  /* Avoid leaking memory by using and resetting our own context */
392  old = MemoryContextSwitchTo(data->context);
393 
394  maybe_send_schema(ctx, relation, relentry);
395 
396  /* Send the data */
397  switch (change->action)
398  {
400  {
401  HeapTuple tuple = &change->data.tp.newtuple->tuple;
402 
403  /* Switch relation if publishing via root. */
404  if (relentry->publish_as_relid != RelationGetRelid(relation))
405  {
406  Assert(relation->rd_rel->relispartition);
407  relation = RelationIdGetRelation(relentry->publish_as_relid);
408  /* Convert tuple if needed. */
409  if (relentry->map)
410  tuple = execute_attr_map_tuple(tuple, relentry->map);
411  }
412 
413  OutputPluginPrepareWrite(ctx, true);
414  logicalrep_write_insert(ctx->out, relation, tuple);
415  OutputPluginWrite(ctx, true);
416  break;
417  }
419  {
420  HeapTuple oldtuple = change->data.tp.oldtuple ?
421  &change->data.tp.oldtuple->tuple : NULL;
422  HeapTuple newtuple = &change->data.tp.newtuple->tuple;
423 
424  /* Switch relation if publishing via root. */
425  if (relentry->publish_as_relid != RelationGetRelid(relation))
426  {
427  Assert(relation->rd_rel->relispartition);
428  relation = RelationIdGetRelation(relentry->publish_as_relid);
429  /* Convert tuples if needed. */
430  if (relentry->map)
431  {
432  oldtuple = execute_attr_map_tuple(oldtuple, relentry->map);
433  newtuple = execute_attr_map_tuple(newtuple, relentry->map);
434  }
435  }
436 
437  OutputPluginPrepareWrite(ctx, true);
438  logicalrep_write_update(ctx->out, relation, oldtuple, newtuple);
439  OutputPluginWrite(ctx, true);
440  break;
441  }
443  if (change->data.tp.oldtuple)
444  {
445  HeapTuple oldtuple = &change->data.tp.oldtuple->tuple;
446 
447  /* Switch relation if publishing via root. */
448  if (relentry->publish_as_relid != RelationGetRelid(relation))
449  {
450  Assert(relation->rd_rel->relispartition);
451  relation = RelationIdGetRelation(relentry->publish_as_relid);
452  /* Convert tuple if needed. */
453  if (relentry->map)
454  oldtuple = execute_attr_map_tuple(oldtuple, relentry->map);
455  }
456 
457  OutputPluginPrepareWrite(ctx, true);
458  logicalrep_write_delete(ctx->out, relation, oldtuple);
459  OutputPluginWrite(ctx, true);
460  }
461  else
462  elog(DEBUG1, "didn't send DELETE change because of missing oldtuple");
463  break;
464  default:
465  Assert(false);
466  }
467 
468  /* Cleanup */
471 }
#define DEBUG1
Definition: elog.h:25
MemoryContext context
Definition: pgoutput.h:20
static void maybe_send_schema(LogicalDecodingContext *ctx, Relation relation, RelationSyncEntry *relentry)
Definition: pgoutput.c:292
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * output_plugin_private
Definition: logical.h:75
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:83
bool is_publishable_relation(Relation rel)
void logicalrep_write_delete(StringInfo out, Relation rel, HeapTuple oldtuple)
Definition: proto.c:251
HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:139
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:636
union ReorderBufferChange::@99 data
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:526
void logicalrep_write_update(StringInfo out, Relation rel, HeapTuple oldtuple, HeapTuple newtuple)
Definition: proto.c:181
struct ReorderBufferChange::@99::@100 tp
#define Assert(condition)
Definition: c.h:745
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:539
#define elog(elevel,...)
Definition: elog.h:214
StringInfo out
Definition: logical.h:70
#define RelationGetRelid(relation)
Definition: rel.h:456
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:1997

◆ pgoutput_commit_txn()

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

Definition at line 277 of file pgoutput.c.

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

Referenced by _PG_output_plugin_init().

279 {
281 
282  OutputPluginPrepareWrite(ctx, true);
283  logicalrep_write_commit(ctx->out, txn, commit_lsn);
284  OutputPluginWrite(ctx, true);
285 }
void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:75
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx)
Definition: logical.c:552
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:526
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:539
StringInfo out
Definition: logical.h:70

◆ pgoutput_origin_filter()

static bool pgoutput_origin_filter ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)
static

Definition at line 533 of file pgoutput.c.

Referenced by _PG_output_plugin_init().

535 {
536  return false;
537 }

◆ pgoutput_shutdown()

static void pgoutput_shutdown ( LogicalDecodingContext ctx)
static

Definition at line 546 of file pgoutput.c.

References hash_destroy().

Referenced by _PG_output_plugin_init().

547 {
548  if (RelationSyncCache)
549  {
551  RelationSyncCache = NULL;
552  }
553 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:825
static HTAB * RelationSyncCache
Definition: pgoutput.c:94

◆ pgoutput_startup()

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

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

182 {
183  PGOutputData *data = palloc0(sizeof(PGOutputData));
184 
185  /* Create our memory context for private allocations. */
187  "logical replication output context",
189 
190  ctx->output_plugin_private = data;
191 
192  /* This plugin uses binary protocol. */
194 
195  /*
196  * This is replication start and not slot initialization.
197  *
198  * Parse and validate options passed by the client.
199  */
200  if (!is_init)
201  {
202  /* Parse the params and ERROR if we see any we don't recognize */
204  &data->protocol_version,
205  &data->publication_names);
206 
207  /* Check if we support requested protocol */
209  ereport(ERROR,
210  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
211  errmsg("client sent proto_version=%d but we only support protocol %d or lower",
213 
215  ereport(ERROR,
216  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
217  errmsg("client sent proto_version=%d but we only support protocol %d or higher",
219 
220  if (list_length(data->publication_names) < 1)
221  ereport(ERROR,
222  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
223  errmsg("publication_names parameter missing")));
224 
225  /* Init publication state. */
226  data->publications = NIL;
227  publications_valid = false;
230  (Datum) 0);
231 
232  /* Initialize relation schema cache. */
234  }
235 }
#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:579
MemoryContext context
Definition: pgoutput.h:20
int errcode(int sqlerrcode)
Definition: elog.c:610
void * output_plugin_private
Definition: logical.h:75
MemoryContext context
Definition: logical.h:35
List * output_plugin_options
Definition: logical.h:58
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
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
#define ereport(elevel,...)
Definition: elog.h:144
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:120
List * publications
Definition: pgoutput.h:27
static void init_rel_sync_cache(MemoryContext decoding_context)
Definition: pgoutput.c:598
int errmsg(const char *fmt,...)
Definition: elog.c:824
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 474 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(), RelationData::rd_rel, RelationGetRelid, RelationSyncEntry::relid, and ReorderBufferChange::truncate.

Referenced by _PG_output_plugin_init().

476 {
478  MemoryContext old;
479  RelationSyncEntry *relentry;
480  int i;
481  int nrelids;
482  Oid *relids;
483 
484  old = MemoryContextSwitchTo(data->context);
485 
486  relids = palloc0(nrelations * sizeof(Oid));
487  nrelids = 0;
488 
489  for (i = 0; i < nrelations; i++)
490  {
491  Relation relation = relations[i];
492  Oid relid = RelationGetRelid(relation);
493 
494  if (!is_publishable_relation(relation))
495  continue;
496 
497  relentry = get_rel_sync_entry(data, relid);
498 
499  if (!relentry->pubactions.pubtruncate)
500  continue;
501 
502  /*
503  * Don't send partitions if the publication wants to send only the
504  * root tables through it.
505  */
506  if (relation->rd_rel->relispartition &&
507  relentry->publish_as_relid != relid)
508  continue;
509 
510  relids[nrelids++] = relid;
511  maybe_send_schema(ctx, relation, relentry);
512  }
513 
514  if (nrelids > 0)
515  {
516  OutputPluginPrepareWrite(ctx, true);
518  nrelids,
519  relids,
520  change->data.truncate.cascade,
521  change->data.truncate.restart_seqs);
522  OutputPluginWrite(ctx, true);
523  }
524 
527 }
struct ReorderBufferChange::@99::@101 truncate
MemoryContext context
Definition: pgoutput.h:20
static void maybe_send_schema(LogicalDecodingContext *ctx, Relation relation, RelationSyncEntry *relentry)
Definition: pgoutput.c:292
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * output_plugin_private
Definition: logical.h:75
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
Form_pg_class rd_rel
Definition: rel.h:109
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:298
static RelationSyncEntry * get_rel_sync_entry(PGOutputData *data, Oid relid)
Definition: pgoutput.c:636
void * palloc0(Size size)
Definition: mcxt.c:980
union ReorderBufferChange::@99 data
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:526
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:539
StringInfo out
Definition: logical.h:70
int i
#define RelationGetRelid(relation)
Definition: rel.h:456

◆ publication_invalidation_cb()

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

Definition at line 579 of file pgoutput.c.

References publications_valid, and rel_sync_cache_publication_cb().

Referenced by pgoutput_startup().

580 {
581  publications_valid = false;
582 
583  /*
584  * Also invalidate per-relation cache so that next time the filtering info
585  * is checked it will be updated with the new publication settings.
586  */
587  rel_sync_cache_publication_cb(arg, cacheid, hashvalue);
588 }
static bool publications_valid
Definition: pgoutput.c:50
static void rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:803
void * arg

◆ rel_sync_cache_publication_cb()

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

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

804 {
806  RelationSyncEntry *entry;
807 
808  /*
809  * We can get here if the plugin was used in SQL interface as the
810  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
811  * is no way to unregister the relcache invalidation callback.
812  */
813  if (RelationSyncCache == NULL)
814  return;
815 
816  /*
817  * There is no way to find which entry in our cache the hash belongs to so
818  * mark the whole cache as invalid.
819  */
821  while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
822  entry->replicate_valid = false;
823 }
bool replicate_valid
Definition: pgoutput.c:73
static HTAB * RelationSyncCache
Definition: pgoutput.c:94
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1400
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1390
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 765 of file pgoutput.c.

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

Referenced by init_rel_sync_cache().

766 {
767  RelationSyncEntry *entry;
768 
769  /*
770  * We can get here if the plugin was used in SQL interface as the
771  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
772  * is no way to unregister the relcache invalidation callback.
773  */
774  if (RelationSyncCache == NULL)
775  return;
776 
777  /*
778  * Nobody keeps pointers to entries in this hash table around outside
779  * logical decoding callback calls - but invalidation events can come in
780  * *during* a callback if we access the relcache in the callback. Because
781  * of that we must mark the cache entry as invalid but not remove it from
782  * the hash while it could still be referenced, then prune it at a later
783  * safe point.
784  *
785  * Getting invalidations for relations that aren't in the table is
786  * entirely normal, since there's no way to unregister for an invalidation
787  * event. So we don't care if it's found or not.
788  */
789  entry = (RelationSyncEntry *) hash_search(RelationSyncCache, &relid,
790  HASH_FIND, NULL);
791 
792  /*
793  * Reset schema sent status as the relation definition may have changed.
794  */
795  if (entry != NULL)
796  entry->schema_sent = false;
797 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:917
static HTAB * RelationSyncCache
Definition: pgoutput.c:94

◆ send_relation_and_attrs()

static void send_relation_and_attrs ( Relation  relation,
LogicalDecodingContext ctx 
)
static

Definition at line 323 of file pgoutput.c.

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

Referenced by maybe_send_schema().

324 {
325  TupleDesc desc = RelationGetDescr(relation);
326  int i;
327 
328  /*
329  * Write out type info if needed. We do that only for user-created types.
330  * We use FirstGenbkiObjectId as the cutoff, so that we only consider
331  * objects with hand-assigned OIDs to be "built in", not for instance any
332  * function or type defined in the information_schema. This is important
333  * because only hand-assigned OIDs can be expected to remain stable across
334  * major versions.
335  */
336  for (i = 0; i < desc->natts; i++)
337  {
338  Form_pg_attribute att = TupleDescAttr(desc, i);
339 
340  if (att->attisdropped || att->attgenerated)
341  continue;
342 
343  if (att->atttypid < FirstGenbkiObjectId)
344  continue;
345 
346  OutputPluginPrepareWrite(ctx, false);
347  logicalrep_write_typ(ctx->out, att->atttypid);
348  OutputPluginWrite(ctx, false);
349  }
350 
351  OutputPluginPrepareWrite(ctx, false);
352  logicalrep_write_rel(ctx->out, relation);
353  OutputPluginWrite(ctx, false);
354 }
void logicalrep_write_typ(StringInfo out, Oid typoid)
Definition: proto.c:400
#define RelationGetDescr(relation)
Definition: rel.h:482
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void logicalrep_write_rel(StringInfo out, Relation rel)
Definition: proto.c:350
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:526
#define FirstGenbkiObjectId
Definition: transam.h:153
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:539
StringInfo out
Definition: logical.h:70
int i

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 94 of file pgoutput.c.