PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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/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

Function Documentation

void _PG_output_plugin_init ( OutputPluginCallbacks cb)

Definition at line 73 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.

74 {
76 
83 }
static void pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
Definition: pgoutput.c:256
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:35
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
Definition: pgoutput.c:73
static void pgoutput_shutdown(LogicalDecodingContext *ctx)
Definition: pgoutput.c:379
static void pgoutput_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:244
LogicalDecodeCommitCB commit_cb
static void pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: pgoutput.c:146
LogicalDecodeChangeCB change_cb
Definition: output_plugin.h:99
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: pgoutput.c:366
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:209
LogicalDecodeFilterByOriginCB filter_by_origin_cb
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:779
static RelationSyncEntry * get_rel_sync_entry ( PGOutputData data,
Oid  relid 
)
static

Definition at line 464 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(), NULL, Publication::oid, RelationSyncEntry::pubactions, Publication::pubactions, PublicationActions::pubdelete, PublicationActions::pubinsert, PGOutputData::publication_names, PGOutputData::publications, publications_valid, PublicationActions::pubupdate, RelationSyncEntry::replicate_valid, and RelationSyncEntry::schema_sent.

Referenced by pgoutput_change().

465 {
466  RelationSyncEntry *entry;
467  bool found;
468  MemoryContext oldctx;
469 
471 
472  /* Find cached function info, creating if not found */
475  (void *) &relid,
476  HASH_ENTER, &found);
477  MemoryContextSwitchTo(oldctx);
478  Assert(entry != NULL);
479 
480  /* Not found means schema wasn't sent */
481  if (!found || !entry->replicate_valid)
482  {
483  List *pubids = GetRelationPublications(relid);
484  ListCell *lc;
485 
486  /* Reload publications if needed before use. */
487  if (!publications_valid)
488  {
490  if (data->publications)
492 
494  MemoryContextSwitchTo(oldctx);
495  publications_valid = true;
496  }
497 
498  /*
499  * Build publication cache. We can't use one provided by relcache
500  * as relcache considers all publications given relation is in, but
501  * here we only need to consider ones that the subscriber requested.
502  */
503  entry->pubactions.pubinsert = entry->pubactions.pubupdate =
504  entry->pubactions.pubdelete = false;
505 
506  foreach(lc, data->publications)
507  {
508  Publication *pub = lfirst(lc);
509 
510  if (pub->alltables || list_member_oid(pubids, pub->oid))
511  {
512  entry->pubactions.pubinsert |= pub->pubactions.pubinsert;
513  entry->pubactions.pubupdate |= pub->pubactions.pubupdate;
514  entry->pubactions.pubdelete |= pub->pubactions.pubdelete;
515  }
516 
517  if (entry->pubactions.pubinsert && entry->pubactions.pubupdate &&
518  entry->pubactions.pubdelete)
519  break;
520  }
521 
522  list_free(pubids);
523 
524  entry->replicate_valid = true;
525  }
526 
527  if (!found)
528  entry->schema_sent = false;
529 
530  return entry;
531 }
PublicationActions pubactions
static bool publications_valid
Definition: pgoutput.c:45
bool replicate_valid
Definition: pgoutput.c:56
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
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:61
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
static List * LoadPublications(List *pubnames)
Definition: pgoutput.c:392
List * publications
Definition: pgoutput.h:27
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
PublicationActions pubactions
Definition: pgoutput.c:57
static void init_rel_sync_cache ( MemoryContext  decoding_context)
static

Definition at line 432 of file pgoutput.c.

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

Referenced by pgoutput_startup().

433 {
434  HASHCTL ctl;
435  MemoryContext old_ctxt;
436 
437  if (RelationSyncCache != NULL)
438  return;
439 
440  /* Make a new hash table for the cache */
441  MemSet(&ctl, 0, sizeof(ctl));
442  ctl.keysize = sizeof(Oid);
443  ctl.entrysize = sizeof(RelationSyncEntry);
444  ctl.hcxt = cachectx;
445 
446  old_ctxt = MemoryContextSwitchTo(cachectx);
447  RelationSyncCache = hash_create("logical replication output relation cache",
448  128, &ctl,
450  (void) MemoryContextSwitchTo(old_ctxt);
451 
453 
457  (Datum) 0);
458 }
#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:537
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:576
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1405
static HTAB * RelationSyncCache
Definition: pgoutput.c:61
#define HASH_BLOBS
Definition: hsearch.h:88
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1381
uintptr_t Datum
Definition: postgres.h:374
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static List * LoadPublications ( List pubnames)
static

Definition at line 392 of file pgoutput.c.

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

Referenced by get_rel_sync_entry().

393 {
394  List *result = NIL;
395  ListCell *lc;
396 
397  foreach (lc, pubnames)
398  {
399  char *pubname = (char *) lfirst(lc);
400  Publication *pub = GetPublicationByName(pubname, false);
401 
402  result = lappend(result, pub);
403  }
404 
405  return result;
406 }
#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
static void parse_output_parameters ( List options,
uint32 protocol_version,
List **  publication_names 
)
static

Definition at line 86 of file pgoutput.c.

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

Referenced by pgoutput_startup().

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

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

210 {
211  bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
212 
213  OutputPluginPrepareWrite(ctx, !send_replication_origin);
214  logicalrep_write_begin(ctx->out, txn);
215 
216  if (send_replication_origin)
217  {
218  char *origin;
219 
220  /* Message boundary */
221  OutputPluginWrite(ctx, false);
222  OutputPluginPrepareWrite(ctx, true);
223 
224  /*
225  * XXX: which behaviour we want here?
226  *
227  * Alternatives:
228  * - don't send origin message if origin name not found
229  * (that's what we do now)
230  * - throw error - that will break replication, not good
231  * - send some special "unknown" origin
232  */
233  if (replorigin_by_oid(txn->origin_id, true, &origin))
234  logicalrep_write_origin(ctx->out, origin, txn->origin_lsn);
235  }
236 
237  OutputPluginWrite(ctx, true);
238 }
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:398
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:472
#define InvalidRepOriginId
Definition: origin.h:34
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:485
StringInfo out
Definition: logical.h:57
static void pgoutput_change ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
Relation  rel,
ReorderBufferChange change 
)
static

Definition at line 256 of file pgoutput.c.

References ReorderBufferChange::action, Assert, tupleDesc::attrs, 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, NULL, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), RelationGetDescr, RelationGetRelid, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, and ReorderBufferChange::tp.

Referenced by _PG_output_plugin_init().

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

Definition at line 244 of file pgoutput.c.

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

Referenced by _PG_output_plugin_init().

246 {
247  OutputPluginPrepareWrite(ctx, true);
248  logicalrep_write_commit(ctx->out, txn, commit_lsn);
249  OutputPluginWrite(ctx, true);
250 }
void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:72
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:472
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:485
StringInfo out
Definition: logical.h:57
static bool pgoutput_origin_filter ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)
static

Definition at line 366 of file pgoutput.c.

Referenced by _PG_output_plugin_init().

368 {
369  return false;
370 }
static void pgoutput_shutdown ( LogicalDecodingContext ctx)
static

Definition at line 379 of file pgoutput.c.

References hash_destroy(), and NULL.

Referenced by _PG_output_plugin_init().

380 {
381  if (RelationSyncCache)
382  {
385  }
386 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:793
static HTAB * RelationSyncCache
Definition: pgoutput.c:61
#define NULL
Definition: c.h:226
static void pgoutput_startup ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

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

148 {
149  PGOutputData *data = palloc0(sizeof(PGOutputData));
150 
151  /* Create our memory context for private allocations. */
153  "logical replication output context",
157 
158  ctx->output_plugin_private = data;
159 
160  /* This plugin uses binary protocol. */
162 
163  /*
164  * This is replication start and not slot initialization.
165  *
166  * Parse and validate options passed by the client.
167  */
168  if (!is_init)
169  {
170  /* Parse the params and ERROR if we see any we don't recognise */
172  &data->protocol_version,
173  &data->publication_names);
174 
175  /* Check if we support requested protocol */
177  ereport(ERROR,
178  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
179  errmsg("client sent proto_version=%d but we only support protocol %d or lower",
181 
183  ereport(ERROR,
184  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
185  errmsg("client sent proto_version=%d but we only support protocol %d or higher",
187 
188  if (list_length(data->publication_names) < 1)
189  ereport(ERROR,
190  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
191  errmsg("publication_names parameter missing")));
192 
193  /* Init publication state. */
194  data->publications = NIL;
195  publications_valid = false;
198  (Datum) 0);
199 
200  /* Initialize relation schema cache. */
202  }
203 }
#define NIL
Definition: pg_list.h:69
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
static bool publications_valid
Definition: pgoutput.c:45
static void publication_invalidation_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:412
MemoryContext context
Definition: pgoutput.h:20
int errcode(int sqlerrcode)
Definition: elog.c:575
void * output_plugin_private
Definition: logical.h:62
MemoryContext context
Definition: logical.h:32
List * output_plugin_options
Definition: logical.h:46
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:142
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:440
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1381
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
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:86
List * publications
Definition: pgoutput.h:27
static void init_rel_sync_cache(MemoryContext decoding_context)
Definition: pgoutput.c:432
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:143
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:144
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
uint32 protocol_version
Definition: pgoutput.h:24
static void publication_invalidation_cb ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 412 of file pgoutput.c.

References publications_valid, and rel_sync_cache_publication_cb().

Referenced by pgoutput_startup().

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

Definition at line 576 of file pgoutput.c.

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

Referenced by init_rel_sync_cache(), and publication_invalidation_cb().

577 {
579  RelationSyncEntry *entry;
580 
581  /*
582  * We can get here if the plugin was used in SQL interface as the
583  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
584  * is no way to unregister the relcache invalidation callback.
585  */
586  if (RelationSyncCache == NULL)
587  return;
588 
589  /*
590  * There is no way to find which entry in our cache the hash belongs to
591  * so mark the whole cache as invalid.
592  */
594  while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
595  entry->replicate_valid = false;
596 }
bool replicate_valid
Definition: pgoutput.c:56
static HTAB * RelationSyncCache
Definition: pgoutput.c:61
#define NULL
Definition: c.h:226
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
static void rel_sync_cache_relation_cb ( Datum  arg,
Oid  relid 
)
static

Definition at line 537 of file pgoutput.c.

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

Referenced by init_rel_sync_cache().

538 {
539  RelationSyncEntry *entry;
540 
541  /*
542  * We can get here if the plugin was used in SQL interface as the
543  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
544  * is no way to unregister the relcache invalidation callback.
545  */
546  if (RelationSyncCache == NULL)
547  return;
548 
549  /*
550  * Nobody keeps pointers to entries in this hash table around outside
551  * logical decoding callback calls - but invalidation events can come in
552  * *during* a callback if we access the relcache in the callback. Because
553  * of that we must mark the cache entry as invalid but not remove it from
554  * the hash while it could still be referenced, then prune it at a later
555  * safe point.
556  *
557  * Getting invalidations for relations that aren't in the table is
558  * entirely normal, since there's no way to unregister for an
559  * invalidation event. So we don't care if it's found or not.
560  */
561  entry = (RelationSyncEntry *) hash_search(RelationSyncCache, &relid,
562  HASH_FIND, NULL);
563 
564  /*
565  * Reset schema sent status as the relation definition may have
566  * changed.
567  */
568  if (entry != NULL)
569  entry->schema_sent = false;
570 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
static HTAB * RelationSyncCache
Definition: pgoutput.c:61
#define NULL
Definition: c.h:226

Variable Documentation

PG_MODULE_MAGIC

Definition at line 28 of file pgoutput.c.

bool publications_valid
static

Definition at line 45 of file pgoutput.c.

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

HTAB* RelationSyncCache = NULL
static

Definition at line 61 of file pgoutput.c.