PostgreSQL Source Code  git master
pgoutput.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pgoutput.c
4  * Logical Replication output plugin
5  *
6  * Copyright (c) 2012-2020, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/backend/replication/pgoutput/pgoutput.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include "access/tupconvert.h"
16 #include "catalog/partition.h"
17 #include "catalog/pg_publication.h"
18 #include "commands/defrem.h"
19 #include "fmgr.h"
20 #include "replication/logical.h"
22 #include "replication/origin.h"
23 #include "replication/pgoutput.h"
24 #include "utils/int8.h"
25 #include "utils/inval.h"
26 #include "utils/lsyscache.h"
27 #include "utils/memutils.h"
28 #include "utils/syscache.h"
29 #include "utils/varlena.h"
30 
32 
34 
36  OutputPluginOptions *opt, bool is_init);
39  ReorderBufferTXN *txn);
41  ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
43  ReorderBufferTXN *txn, Relation rel,
44  ReorderBufferChange *change);
46  ReorderBufferTXN *txn, int nrelations, Relation relations[],
47  ReorderBufferChange *change);
49  RepOriginId origin_id);
50 static void pgoutput_stream_start(struct LogicalDecodingContext *ctx,
51  ReorderBufferTXN *txn);
52 static void pgoutput_stream_stop(struct LogicalDecodingContext *ctx,
53  ReorderBufferTXN *txn);
54 static void pgoutput_stream_abort(struct LogicalDecodingContext *ctx,
55  ReorderBufferTXN *txn,
56  XLogRecPtr abort_lsn);
57 static void pgoutput_stream_commit(struct LogicalDecodingContext *ctx,
58  ReorderBufferTXN *txn,
59  XLogRecPtr commit_lsn);
60 
61 static bool publications_valid;
62 static bool in_streaming;
63 
64 static List *LoadPublications(List *pubnames);
65 static void publication_invalidation_cb(Datum arg, int cacheid,
66  uint32 hashvalue);
67 static void send_relation_and_attrs(Relation relation, TransactionId xid,
69 
70 /*
71  * Entry in the map used to remember which relation schemas we sent.
72  *
73  * The schema_sent flag determines if the current schema record was already
74  * sent to the subscriber (in which case we don't need to send it again).
75  *
76  * The schema cache on downstream is however updated only at commit time,
77  * and with streamed transactions the commit order may be different from
78  * the order the transactions are sent in. Also, the (sub) transactions
79  * might get aborted so we need to send the schema for each (sub) transaction
80  * so that we don't lose the schema information on abort. For handling this,
81  * we maintain the list of xids (streamed_txns) for those we have already sent
82  * the schema.
83  *
84  * For partitions, 'pubactions' considers not only the table's own
85  * publications, but also those of all of its ancestors.
86  */
87 typedef struct RelationSyncEntry
88 {
89  Oid relid; /* relation oid */
90 
91  /*
92  * Did we send the schema? If ancestor relid is set, its schema must also
93  * have been sent for this to be true.
94  */
96  List *streamed_txns; /* streamed toplevel transactions with this
97  * schema */
98 
101 
102  /*
103  * OID of the relation to publish changes as. For a partition, this may
104  * be set to one of its ancestors whose schema will be used when
105  * replicating changes, if publish_via_partition_root is set for the
106  * publication.
107  */
109 
110  /*
111  * Map used when replicating using an ancestor's schema to convert tuples
112  * from partition's type to the ancestor's; NULL if publish_as_relid is
113  * same as 'relid' or if unnecessary due to partition and the ancestor
114  * having identical TupleDesc.
115  */
118 
119 /* Map used to remember which relation schemas we sent. */
120 static HTAB *RelationSyncCache = NULL;
121 
122 static void init_rel_sync_cache(MemoryContext decoding_context);
123 static void cleanup_rel_sync_cache(TransactionId xid, bool is_commit);
126 static void rel_sync_cache_publication_cb(Datum arg, int cacheid,
127  uint32 hashvalue);
129  TransactionId xid);
131  TransactionId xid);
132 
133 /*
134  * Specify output plugin callbacks
135  */
136 void
138 {
140 
148 
149  /* transaction streaming */
156 }
157 
158 static void
160  List **publication_names, bool *binary,
161  bool *enable_streaming)
162 {
163  ListCell *lc;
164  bool protocol_version_given = false;
165  bool publication_names_given = false;
166  bool binary_option_given = false;
167  bool streaming_given = false;
168 
169  *binary = false;
170 
171  foreach(lc, options)
172  {
173  DefElem *defel = (DefElem *) lfirst(lc);
174 
175  Assert(defel->arg == NULL || IsA(defel->arg, String));
176 
177  /* Check each param, whether or not we recognize it */
178  if (strcmp(defel->defname, "proto_version") == 0)
179  {
180  int64 parsed;
181 
182  if (protocol_version_given)
183  ereport(ERROR,
184  (errcode(ERRCODE_SYNTAX_ERROR),
185  errmsg("conflicting or redundant options")));
186  protocol_version_given = true;
187 
188  if (!scanint8(strVal(defel->arg), true, &parsed))
189  ereport(ERROR,
190  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
191  errmsg("invalid proto_version")));
192 
193  if (parsed > PG_UINT32_MAX || parsed < 0)
194  ereport(ERROR,
195  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
196  errmsg("proto_version \"%s\" out of range",
197  strVal(defel->arg))));
198 
199  *protocol_version = (uint32) parsed;
200  }
201  else if (strcmp(defel->defname, "publication_names") == 0)
202  {
203  if (publication_names_given)
204  ereport(ERROR,
205  (errcode(ERRCODE_SYNTAX_ERROR),
206  errmsg("conflicting or redundant options")));
207  publication_names_given = true;
208 
209  if (!SplitIdentifierString(strVal(defel->arg), ',',
210  publication_names))
211  ereport(ERROR,
212  (errcode(ERRCODE_INVALID_NAME),
213  errmsg("invalid publication_names syntax")));
214  }
215  else if (strcmp(defel->defname, "binary") == 0)
216  {
217  if (binary_option_given)
218  ereport(ERROR,
219  (errcode(ERRCODE_SYNTAX_ERROR),
220  errmsg("conflicting or redundant options")));
221  binary_option_given = true;
222 
223  *binary = defGetBoolean(defel);
224  }
225  else if (strcmp(defel->defname, "streaming") == 0)
226  {
227  if (streaming_given)
228  ereport(ERROR,
229  (errcode(ERRCODE_SYNTAX_ERROR),
230  errmsg("conflicting or redundant options")));
231  streaming_given = true;
232 
233  *enable_streaming = defGetBoolean(defel);
234  }
235  else
236  elog(ERROR, "unrecognized pgoutput option: %s", defel->defname);
237  }
238 }
239 
240 /*
241  * Initialize this plugin
242  */
243 static void
245  bool is_init)
246 {
247  bool enable_streaming = false;
248  PGOutputData *data = palloc0(sizeof(PGOutputData));
249 
250  /* Create our memory context for private allocations. */
252  "logical replication output context",
254 
255  ctx->output_plugin_private = data;
256 
257  /* This plugin uses binary protocol. */
259 
260  /*
261  * This is replication start and not slot initialization.
262  *
263  * Parse and validate options passed by the client.
264  */
265  if (!is_init)
266  {
267  /* Parse the params and ERROR if we see any we don't recognize */
269  &data->protocol_version,
270  &data->publication_names,
271  &data->binary,
272  &enable_streaming);
273 
274  /* Check if we support requested protocol */
276  ereport(ERROR,
277  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
278  errmsg("client sent proto_version=%d but we only support protocol %d or lower",
280 
282  ereport(ERROR,
283  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
284  errmsg("client sent proto_version=%d but we only support protocol %d or higher",
286 
287  if (list_length(data->publication_names) < 1)
288  ereport(ERROR,
289  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
290  errmsg("publication_names parameter missing")));
291 
292  /*
293  * Decide whether to enable streaming. It is disabled by default, in
294  * which case we just update the flag in decoding context. Otherwise
295  * we only allow it with sufficient version of the protocol, and when
296  * the output plugin supports it.
297  */
298  if (!enable_streaming)
299  ctx->streaming = false;
301  ereport(ERROR,
302  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
303  errmsg("requested proto_version=%d does not support streaming, need %d or higher",
305  else if (!ctx->streaming)
306  ereport(ERROR,
307  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
308  errmsg("streaming requested, but not supported by output plugin")));
309 
310  /* Also remember we're currently not streaming any transaction. */
311  in_streaming = false;
312 
313  /* Init publication state. */
314  data->publications = NIL;
315  publications_valid = false;
318  (Datum) 0);
319 
320  /* Initialize relation schema cache. */
322  }
323  else
324  {
325  /* Disable the streaming during the slot initialization mode. */
326  ctx->streaming = false;
327  }
328 }
329 
330 /*
331  * BEGIN callback
332  */
333 static void
335 {
336  bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
337 
338  OutputPluginPrepareWrite(ctx, !send_replication_origin);
339  logicalrep_write_begin(ctx->out, txn);
340 
341  if (send_replication_origin)
342  {
343  char *origin;
344 
345  /* Message boundary */
346  OutputPluginWrite(ctx, false);
347  OutputPluginPrepareWrite(ctx, true);
348 
349  /*----------
350  * XXX: which behaviour do we want here?
351  *
352  * Alternatives:
353  * - don't send origin message if origin name not found
354  * (that's what we do now)
355  * - throw error - that will break replication, not good
356  * - send some special "unknown" origin
357  *----------
358  */
359  if (replorigin_by_oid(txn->origin_id, true, &origin))
360  logicalrep_write_origin(ctx->out, origin, txn->origin_lsn);
361  }
362 
363  OutputPluginWrite(ctx, true);
364 }
365 
366 /*
367  * COMMIT callback
368  */
369 static void
371  XLogRecPtr commit_lsn)
372 {
374 
375  OutputPluginPrepareWrite(ctx, true);
376  logicalrep_write_commit(ctx->out, txn, commit_lsn);
377  OutputPluginWrite(ctx, true);
378 }
379 
380 /*
381  * Write the current schema of the relation and its ancestor (if any) if not
382  * done yet.
383  */
384 static void
387  Relation relation, RelationSyncEntry *relentry)
388 {
389  bool schema_sent;
392 
393  /*
394  * Remember XID of the (sub)transaction for the change. We don't care if
395  * it's top-level transaction or not (we have already sent that XID in
396  * start of the current streaming block).
397  *
398  * If we're not in a streaming block, just use InvalidTransactionId and
399  * the write methods will not include it.
400  */
401  if (in_streaming)
402  xid = change->txn->xid;
403 
404  if (change->txn->toptxn)
405  topxid = change->txn->toptxn->xid;
406  else
407  topxid = xid;
408 
409  /*
410  * Do we need to send the schema? We do track streamed transactions
411  * separately, because those may be applied later (and the regular
412  * transactions won't see their effects until then) and in an order that
413  * we don't know at this point.
414  *
415  * XXX There is a scope of optimization here. Currently, we always send
416  * the schema first time in a streaming transaction but we can probably
417  * avoid that by checking 'relentry->schema_sent' flag. However, before
418  * doing that we need to study its impact on the case where we have a mix
419  * of streaming and non-streaming transactions.
420  */
421  if (in_streaming)
422  schema_sent = get_schema_sent_in_streamed_txn(relentry, topxid);
423  else
424  schema_sent = relentry->schema_sent;
425 
426  if (schema_sent)
427  return;
428 
429  /* If needed, send the ancestor's schema first. */
430  if (relentry->publish_as_relid != RelationGetRelid(relation))
431  {
432  Relation ancestor = RelationIdGetRelation(relentry->publish_as_relid);
433  TupleDesc indesc = RelationGetDescr(relation);
434  TupleDesc outdesc = RelationGetDescr(ancestor);
435  MemoryContext oldctx;
436 
437  /* Map must live as long as the session does. */
439  relentry->map = convert_tuples_by_name(CreateTupleDescCopy(indesc),
440  CreateTupleDescCopy(outdesc));
441  MemoryContextSwitchTo(oldctx);
442  send_relation_and_attrs(ancestor, xid, ctx);
443  RelationClose(ancestor);
444  }
445 
446  send_relation_and_attrs(relation, xid, ctx);
447 
448  if (in_streaming)
449  set_schema_sent_in_streamed_txn(relentry, topxid);
450  else
451  relentry->schema_sent = true;
452 }
453 
454 /*
455  * Sends a relation
456  */
457 static void
460 {
461  TupleDesc desc = RelationGetDescr(relation);
462  int i;
463 
464  /*
465  * Write out type info if needed. We do that only for user-created types.
466  * We use FirstGenbkiObjectId as the cutoff, so that we only consider
467  * objects with hand-assigned OIDs to be "built in", not for instance any
468  * function or type defined in the information_schema. This is important
469  * because only hand-assigned OIDs can be expected to remain stable across
470  * major versions.
471  */
472  for (i = 0; i < desc->natts; i++)
473  {
474  Form_pg_attribute att = TupleDescAttr(desc, i);
475 
476  if (att->attisdropped || att->attgenerated)
477  continue;
478 
479  if (att->atttypid < FirstGenbkiObjectId)
480  continue;
481 
482  OutputPluginPrepareWrite(ctx, false);
483  logicalrep_write_typ(ctx->out, xid, att->atttypid);
484  OutputPluginWrite(ctx, false);
485  }
486 
487  OutputPluginPrepareWrite(ctx, false);
488  logicalrep_write_rel(ctx->out, xid, relation);
489  OutputPluginWrite(ctx, false);
490 }
491 
492 /*
493  * Sends the decoded DML over wire.
494  *
495  * This is called both in streaming and non-streaming modes.
496  */
497 static void
499  Relation relation, ReorderBufferChange *change)
500 {
502  MemoryContext old;
503  RelationSyncEntry *relentry;
505 
506  if (!is_publishable_relation(relation))
507  return;
508 
509  /*
510  * Remember the xid for the change in streaming mode. We need to send xid
511  * with each change in the streaming mode so that subscriber can make
512  * their association and on aborts, it can discard the corresponding
513  * changes.
514  */
515  if (in_streaming)
516  xid = change->txn->xid;
517 
518  relentry = get_rel_sync_entry(data, RelationGetRelid(relation));
519 
520  /* First check the table filter */
521  switch (change->action)
522  {
524  if (!relentry->pubactions.pubinsert)
525  return;
526  break;
528  if (!relentry->pubactions.pubupdate)
529  return;
530  break;
532  if (!relentry->pubactions.pubdelete)
533  return;
534  break;
535  default:
536  Assert(false);
537  }
538 
539  /* Avoid leaking memory by using and resetting our own context */
540  old = MemoryContextSwitchTo(data->context);
541 
542  maybe_send_schema(ctx, txn, change, relation, relentry);
543 
544  /* Send the data */
545  switch (change->action)
546  {
548  {
549  HeapTuple tuple = &change->data.tp.newtuple->tuple;
550 
551  /* Switch relation if publishing via root. */
552  if (relentry->publish_as_relid != RelationGetRelid(relation))
553  {
554  Assert(relation->rd_rel->relispartition);
555  relation = RelationIdGetRelation(relentry->publish_as_relid);
556  /* Convert tuple if needed. */
557  if (relentry->map)
558  tuple = execute_attr_map_tuple(tuple, relentry->map);
559  }
560 
561  OutputPluginPrepareWrite(ctx, true);
562  logicalrep_write_insert(ctx->out, xid, relation, tuple,
563  data->binary);
564  OutputPluginWrite(ctx, true);
565  break;
566  }
568  {
569  HeapTuple oldtuple = change->data.tp.oldtuple ?
570  &change->data.tp.oldtuple->tuple : NULL;
571  HeapTuple newtuple = &change->data.tp.newtuple->tuple;
572 
573  /* Switch relation if publishing via root. */
574  if (relentry->publish_as_relid != RelationGetRelid(relation))
575  {
576  Assert(relation->rd_rel->relispartition);
577  relation = RelationIdGetRelation(relentry->publish_as_relid);
578  /* Convert tuples if needed. */
579  if (relentry->map)
580  {
581  oldtuple = execute_attr_map_tuple(oldtuple, relentry->map);
582  newtuple = execute_attr_map_tuple(newtuple, relentry->map);
583  }
584  }
585 
586  OutputPluginPrepareWrite(ctx, true);
587  logicalrep_write_update(ctx->out, xid, relation, oldtuple,
588  newtuple, data->binary);
589  OutputPluginWrite(ctx, true);
590  break;
591  }
593  if (change->data.tp.oldtuple)
594  {
595  HeapTuple oldtuple = &change->data.tp.oldtuple->tuple;
596 
597  /* Switch relation if publishing via root. */
598  if (relentry->publish_as_relid != RelationGetRelid(relation))
599  {
600  Assert(relation->rd_rel->relispartition);
601  relation = RelationIdGetRelation(relentry->publish_as_relid);
602  /* Convert tuple if needed. */
603  if (relentry->map)
604  oldtuple = execute_attr_map_tuple(oldtuple, relentry->map);
605  }
606 
607  OutputPluginPrepareWrite(ctx, true);
608  logicalrep_write_delete(ctx->out, xid, relation, oldtuple,
609  data->binary);
610  OutputPluginWrite(ctx, true);
611  }
612  else
613  elog(DEBUG1, "didn't send DELETE change because of missing oldtuple");
614  break;
615  default:
616  Assert(false);
617  }
618 
619  /* Cleanup */
622 }
623 
624 static void
626  int nrelations, Relation relations[], ReorderBufferChange *change)
627 {
629  MemoryContext old;
630  RelationSyncEntry *relentry;
631  int i;
632  int nrelids;
633  Oid *relids;
635 
636  /* Remember the xid for the change in streaming mode. See pgoutput_change. */
637  if (in_streaming)
638  xid = change->txn->xid;
639 
640  old = MemoryContextSwitchTo(data->context);
641 
642  relids = palloc0(nrelations * sizeof(Oid));
643  nrelids = 0;
644 
645  for (i = 0; i < nrelations; i++)
646  {
647  Relation relation = relations[i];
648  Oid relid = RelationGetRelid(relation);
649 
650  if (!is_publishable_relation(relation))
651  continue;
652 
653  relentry = get_rel_sync_entry(data, relid);
654 
655  if (!relentry->pubactions.pubtruncate)
656  continue;
657 
658  /*
659  * Don't send partitions if the publication wants to send only the
660  * root tables through it.
661  */
662  if (relation->rd_rel->relispartition &&
663  relentry->publish_as_relid != relid)
664  continue;
665 
666  relids[nrelids++] = relid;
667  maybe_send_schema(ctx, txn, change, relation, relentry);
668  }
669 
670  if (nrelids > 0)
671  {
672  OutputPluginPrepareWrite(ctx, true);
674  xid,
675  nrelids,
676  relids,
677  change->data.truncate.cascade,
678  change->data.truncate.restart_seqs);
679  OutputPluginWrite(ctx, true);
680  }
681 
684 }
685 
686 /*
687  * Currently we always forward.
688  */
689 static bool
691  RepOriginId origin_id)
692 {
693  return false;
694 }
695 
696 /*
697  * Shutdown the output plugin.
698  *
699  * Note, we don't need to clean the data->context as it's child context
700  * of the ctx->context so it will be cleaned up by logical decoding machinery.
701  */
702 static void
704 {
705  if (RelationSyncCache)
706  {
707  hash_destroy(RelationSyncCache);
708  RelationSyncCache = NULL;
709  }
710 }
711 
712 /*
713  * Load publications from the list of publication names.
714  */
715 static List *
717 {
718  List *result = NIL;
719  ListCell *lc;
720 
721  foreach(lc, pubnames)
722  {
723  char *pubname = (char *) lfirst(lc);
724  Publication *pub = GetPublicationByName(pubname, false);
725 
726  result = lappend(result, pub);
727  }
728 
729  return result;
730 }
731 
732 /*
733  * Publication cache invalidation callback.
734  */
735 static void
736 publication_invalidation_cb(Datum arg, int cacheid, uint32 hashvalue)
737 {
738  publications_valid = false;
739 
740  /*
741  * Also invalidate per-relation cache so that next time the filtering info
742  * is checked it will be updated with the new publication settings.
743  */
744  rel_sync_cache_publication_cb(arg, cacheid, hashvalue);
745 }
746 
747 /*
748  * START STREAM callback
749  */
750 static void
752  ReorderBufferTXN *txn)
753 {
754  bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
755 
756  /* we can't nest streaming of transactions */
758 
759  /*
760  * If we already sent the first stream for this transaction then don't
761  * send the origin id in the subsequent streams.
762  */
763  if (rbtxn_is_streamed(txn))
764  send_replication_origin = false;
765 
766  OutputPluginPrepareWrite(ctx, !send_replication_origin);
768 
769  if (send_replication_origin)
770  {
771  char *origin;
772 
773  /* Message boundary */
774  OutputPluginWrite(ctx, false);
775  OutputPluginPrepareWrite(ctx, true);
776 
777  if (replorigin_by_oid(txn->origin_id, true, &origin))
779  }
780 
781  OutputPluginWrite(ctx, true);
782 
783  /* we're streaming a chunk of transaction now */
784  in_streaming = true;
785 }
786 
787 /*
788  * STOP STREAM callback
789  */
790 static void
792  ReorderBufferTXN *txn)
793 {
794  /* we should be streaming a trasanction */
796 
797  OutputPluginPrepareWrite(ctx, true);
799  OutputPluginWrite(ctx, true);
800 
801  /* we've stopped streaming a transaction */
802  in_streaming = false;
803 }
804 
805 /*
806  * Notify downstream to discard the streamed transaction (along with all
807  * it's subtransactions, if it's a toplevel transaction).
808  */
809 static void
811  ReorderBufferTXN *txn,
812  XLogRecPtr abort_lsn)
813 {
814  ReorderBufferTXN *toptxn;
815 
816  /*
817  * The abort should happen outside streaming block, even for streamed
818  * transactions. The transaction has to be marked as streamed, though.
819  */
821 
822  /* determine the toplevel transaction */
823  toptxn = (txn->toptxn) ? txn->toptxn : txn;
824 
825  Assert(rbtxn_is_streamed(toptxn));
826 
827  OutputPluginPrepareWrite(ctx, true);
828  logicalrep_write_stream_abort(ctx->out, toptxn->xid, txn->xid);
829  OutputPluginWrite(ctx, true);
830 
831  cleanup_rel_sync_cache(toptxn->xid, false);
832 }
833 
834 /*
835  * Notify downstream to apply the streamed transaction (along with all
836  * it's subtransactions).
837  */
838 static void
840  ReorderBufferTXN *txn,
841  XLogRecPtr commit_lsn)
842 {
843  /*
844  * The commit should happen outside streaming block, even for streamed
845  * transactions. The transaction has to be marked as streamed, though.
846  */
849 
851 
852  OutputPluginPrepareWrite(ctx, true);
853  logicalrep_write_stream_commit(ctx->out, txn, commit_lsn);
854  OutputPluginWrite(ctx, true);
855 
856  cleanup_rel_sync_cache(txn->xid, true);
857 }
858 
859 /*
860  * Initialize the relation schema sync cache for a decoding session.
861  *
862  * The hash table is destroyed at the end of a decoding session. While
863  * relcache invalidations still exist and will still be invoked, they
864  * will just see the null hash table global and take no action.
865  */
866 static void
868 {
869  HASHCTL ctl;
870  MemoryContext old_ctxt;
871 
872  if (RelationSyncCache != NULL)
873  return;
874 
875  /* Make a new hash table for the cache */
876  MemSet(&ctl, 0, sizeof(ctl));
877  ctl.keysize = sizeof(Oid);
878  ctl.entrysize = sizeof(RelationSyncEntry);
879  ctl.hcxt = cachectx;
880 
881  old_ctxt = MemoryContextSwitchTo(cachectx);
882  RelationSyncCache = hash_create("logical replication output relation cache",
883  128, &ctl,
885  (void) MemoryContextSwitchTo(old_ctxt);
886 
887  Assert(RelationSyncCache != NULL);
888 
892  (Datum) 0);
893 }
894 
895 /*
896  * We expect relatively small number of streamed transactions.
897  */
898 static bool
900 {
901  ListCell *lc;
902 
903  foreach(lc, entry->streamed_txns)
904  {
905  if (xid == (uint32) lfirst_int(lc))
906  return true;
907  }
908 
909  return false;
910 }
911 
912 /*
913  * Add the xid in the rel sync entry for which we have already sent the schema
914  * of the relation.
915  */
916 static void
918 {
919  MemoryContext oldctx;
920 
922 
923  entry->streamed_txns = lappend_int(entry->streamed_txns, xid);
924 
925  MemoryContextSwitchTo(oldctx);
926 }
927 
928 /*
929  * Find or create entry in the relation schema cache.
930  *
931  * This looks up publications that the given relation is directly or
932  * indirectly part of (the latter if it's really the relation's ancestor that
933  * is part of a publication) and fills up the found entry with the information
934  * about which operations to publish and whether to use an ancestor's schema
935  * when publishing.
936  */
937 static RelationSyncEntry *
939 {
940  RelationSyncEntry *entry;
941  bool am_partition = get_rel_relispartition(relid);
942  char relkind = get_rel_relkind(relid);
943  bool found;
944  MemoryContext oldctx;
945 
946  Assert(RelationSyncCache != NULL);
947 
948  /* Find cached relation info, creating if not found */
949  entry = (RelationSyncEntry *) hash_search(RelationSyncCache,
950  (void *) &relid,
951  HASH_ENTER, &found);
952  Assert(entry != NULL);
953 
954  /* Not found means schema wasn't sent */
955  if (!found)
956  {
957  /* immediately make a new entry valid enough to satisfy callbacks */
958  entry->schema_sent = false;
959  entry->streamed_txns = NIL;
960  entry->replicate_valid = false;
961  entry->pubactions.pubinsert = entry->pubactions.pubupdate =
962  entry->pubactions.pubdelete = entry->pubactions.pubtruncate = false;
963  entry->publish_as_relid = InvalidOid;
964  }
965 
966  /* Validate the entry */
967  if (!entry->replicate_valid)
968  {
969  List *pubids = GetRelationPublications(relid);
970  ListCell *lc;
972 
973  /* Reload publications if needed before use. */
974  if (!publications_valid)
975  {
977  if (data->publications)
979 
981  MemoryContextSwitchTo(oldctx);
982  publications_valid = true;
983  }
984 
985  /*
986  * Build publication cache. We can't use one provided by relcache as
987  * relcache considers all publications given relation is in, but here
988  * we only need to consider ones that the subscriber requested.
989  */
990  foreach(lc, data->publications)
991  {
992  Publication *pub = lfirst(lc);
993  bool publish = false;
994 
995  if (pub->alltables)
996  {
997  publish = true;
998  if (pub->pubviaroot && am_partition)
999  publish_as_relid = llast_oid(get_partition_ancestors(relid));
1000  }
1001 
1002  if (!publish)
1003  {
1004  bool ancestor_published = false;
1005 
1006  /*
1007  * For a partition, check if any of the ancestors are
1008  * published. If so, note down the topmost ancestor that is
1009  * published via this publication, which will be used as the
1010  * relation via which to publish the partition's changes.
1011  */
1012  if (am_partition)
1013  {
1014  List *ancestors = get_partition_ancestors(relid);
1015  ListCell *lc2;
1016 
1017  /*
1018  * Find the "topmost" ancestor that is in this
1019  * publication.
1020  */
1021  foreach(lc2, ancestors)
1022  {
1023  Oid ancestor = lfirst_oid(lc2);
1024 
1026  pub->oid))
1027  {
1028  ancestor_published = true;
1029  if (pub->pubviaroot)
1030  publish_as_relid = ancestor;
1031  }
1032  }
1033  }
1034 
1035  if (list_member_oid(pubids, pub->oid) || ancestor_published)
1036  publish = true;
1037  }
1038 
1039  /*
1040  * Don't publish changes for partitioned tables, because
1041  * publishing those of its partitions suffices, unless partition
1042  * changes won't be published due to pubviaroot being set.
1043  */
1044  if (publish &&
1045  (relkind != RELKIND_PARTITIONED_TABLE || pub->pubviaroot))
1046  {
1047  entry->pubactions.pubinsert |= pub->pubactions.pubinsert;
1048  entry->pubactions.pubupdate |= pub->pubactions.pubupdate;
1049  entry->pubactions.pubdelete |= pub->pubactions.pubdelete;
1051  }
1052 
1053  if (entry->pubactions.pubinsert && entry->pubactions.pubupdate &&
1054  entry->pubactions.pubdelete && entry->pubactions.pubtruncate)
1055  break;
1056  }
1057 
1058  list_free(pubids);
1059 
1061  entry->replicate_valid = true;
1062  }
1063 
1064  return entry;
1065 }
1066 
1067 /*
1068  * Cleanup list of streamed transactions and update the schema_sent flag.
1069  *
1070  * When a streamed transaction commits or aborts, we need to remove the
1071  * toplevel XID from the schema cache. If the transaction aborted, the
1072  * subscriber will simply throw away the schema records we streamed, so
1073  * we don't need to do anything else.
1074  *
1075  * If the transaction is committed, the subscriber will update the relation
1076  * cache - so tweak the schema_sent flag accordingly.
1077  */
1078 static void
1080 {
1081  HASH_SEQ_STATUS hash_seq;
1082  RelationSyncEntry *entry;
1083  ListCell *lc;
1084 
1085  Assert(RelationSyncCache != NULL);
1086 
1087  hash_seq_init(&hash_seq, RelationSyncCache);
1088  while ((entry = hash_seq_search(&hash_seq)) != NULL)
1089  {
1090  /*
1091  * We can set the schema_sent flag for an entry that has committed xid
1092  * in the list as that ensures that the subscriber would have the
1093  * corresponding schema and we don't need to send it unless there is
1094  * any invalidation for that relation.
1095  */
1096  foreach(lc, entry->streamed_txns)
1097  {
1098  if (xid == (uint32) lfirst_int(lc))
1099  {
1100  if (is_commit)
1101  entry->schema_sent = true;
1102 
1103  entry->streamed_txns =
1105  break;
1106  }
1107  }
1108  }
1109 }
1110 
1111 /*
1112  * Relcache invalidation callback
1113  */
1114 static void
1116 {
1117  RelationSyncEntry *entry;
1118 
1119  /*
1120  * We can get here if the plugin was used in SQL interface as the
1121  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
1122  * is no way to unregister the relcache invalidation callback.
1123  */
1124  if (RelationSyncCache == NULL)
1125  return;
1126 
1127  /*
1128  * Nobody keeps pointers to entries in this hash table around outside
1129  * logical decoding callback calls - but invalidation events can come in
1130  * *during* a callback if we access the relcache in the callback. Because
1131  * of that we must mark the cache entry as invalid but not remove it from
1132  * the hash while it could still be referenced, then prune it at a later
1133  * safe point.
1134  *
1135  * Getting invalidations for relations that aren't in the table is
1136  * entirely normal, since there's no way to unregister for an invalidation
1137  * event. So we don't care if it's found or not.
1138  */
1139  entry = (RelationSyncEntry *) hash_search(RelationSyncCache, &relid,
1140  HASH_FIND, NULL);
1141 
1142  /*
1143  * Reset schema sent status as the relation definition may have changed.
1144  */
1145  if (entry != NULL)
1146  {
1147  entry->schema_sent = false;
1148  list_free(entry->streamed_txns);
1149  entry->streamed_txns = NIL;
1150  }
1151 }
1152 
1153 /*
1154  * Publication relation map syscache invalidation callback
1155  */
1156 static void
1158 {
1160  RelationSyncEntry *entry;
1161 
1162  /*
1163  * We can get here if the plugin was used in SQL interface as the
1164  * RelSchemaSyncCache is destroyed when the decoding finishes, but there
1165  * is no way to unregister the relcache invalidation callback.
1166  */
1167  if (RelationSyncCache == NULL)
1168  return;
1169 
1170  /*
1171  * There is no way to find which entry in our cache the hash belongs to so
1172  * mark the whole cache as invalid.
1173  */
1174  hash_seq_init(&status, RelationSyncCache);
1175  while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
1176  entry->replicate_valid = false;
1177 }
List * streamed_txns
Definition: pgoutput.c:96
LogicalDecodeTruncateCB truncate_cb
#define NIL
Definition: pg_list.h:65
static void pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
Definition: pgoutput.c:498
static void parse_output_parameters(List *options, uint32 *protocol_version, List **publication_names, bool *binary, bool *enable_streaming)
Definition: pgoutput.c:159
PublicationActions pubactions
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:827
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
#define AllocSetContextCreate
Definition: memutils.h:170
#define DEBUG1
Definition: elog.h:25
static bool publications_valid
Definition: pgoutput.c:61
RepOriginId origin_id
static bool in_streaming
Definition: pgoutput.c:62
void logicalrep_write_truncate(StringInfo out, TransactionId xid, int nrelids, Oid relids[], bool cascade, bool restart_seqs)
Definition: proto.c:311
bool replicate_valid
Definition: pgoutput.c:99
#define HASH_CONTEXT
Definition: hsearch.h:91
#define HASH_ELEM
Definition: hsearch.h:85
uint32 TransactionId
Definition: c.h:521
static void publication_invalidation_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:736
MemoryContext hcxt
Definition: hsearch.h:77
#define RelationGetDescr(relation)
Definition: rel.h:482
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
Definition: pgoutput.c:137
void logicalrep_write_stream_stop(StringInfo out)
Definition: proto.c:789
static void maybe_send_schema(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, ReorderBufferChange *change, Relation relation, RelationSyncEntry *relentry)
Definition: pgoutput.c:385
MemoryContext context
Definition: pgoutput.h:20
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static void rel_sync_cache_relation_cb(Datum arg, Oid relid)
Definition: pgoutput.c:1115
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1915
static void pgoutput_stream_start(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:751
TupleConversionMap * map
Definition: pgoutput.c:116
static void pgoutput_shutdown(LogicalDecodingContext *ctx)
Definition: pgoutput.c:703
struct ReorderBufferTXN * txn
Definition: reorderbuffer.h:87
static void pgoutput_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:370
union ReorderBufferChange::@98 data
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
uint16 RepOriginId
Definition: xlogdefs.h:58
Size entrysize
Definition: hsearch.h:72
#define strVal(v)
Definition: value.h:54
void logicalrep_write_stream_abort(StringInfo out, TransactionId xid, TransactionId subxid)
Definition: proto.c:849
int errcode(int sqlerrcode)
Definition: elog.c:610
static void send_relation_and_attrs(Relation relation, TransactionId xid, LogicalDecodingContext *ctx)
Definition: pgoutput.c:458
void * output_plugin_private
Definition: logical.h:75
#define MemSet(start, val, len)
Definition: c.h:950
void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:74
MemoryContext context
Definition: logical.h:35
void logicalrep_write_delete(StringInfo out, TransactionId xid, Relation rel, HeapTuple oldtuple, bool binary)
Definition: proto.c:259
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:137
#define llast_oid(l)
Definition: pg_list.h:196
LogicalDecodeStreamAbortCB stream_abort_cb
List * output_plugin_options
Definition: logical.h:58
void logicalrep_write_origin(StringInfo out, const char *origin, XLogRecPtr origin_lsn)
Definition: proto.c:112
#define PG_UINT32_MAX
Definition: c.h:459
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
bool replorigin_by_oid(RepOriginId roident, bool missing_ok, char **roname)
Definition: origin.c:432
Form_pg_class rd_rel
Definition: rel.h:109
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:1157
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
void list_free_deep(List *list)
Definition: list.c:1390
void logicalrep_write_insert(StringInfo out, TransactionId xid, Relation rel, HeapTuple newtuple, bool binary)
Definition: proto.c:141
Publication * GetPublicationByName(const char *pubname, bool missing_ok)
XLogRecPtr origin_lsn
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1476
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:357
void logicalrep_write_stream_start(StringInfo out, TransactionId xid, bool first_segment)
Definition: proto.c:755
OutputPluginOutputType output_type
Definition: output_plugin.h:28
#define rbtxn_is_streamed(txn)
Definition: dynahash.c:218
bool defGetBoolean(DefElem *def)
Definition: define.c:111
List * GetRelationPublications(Oid relid)
static void pgoutput_stream_commit(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:839
void logicalrep_write_rel(StringInfo out, TransactionId xid, Relation rel)
Definition: proto.c:368
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:170
LogicalDecodeCommitCB commit_cb
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define LOGICALREP_PROTO_MIN_VERSION_NUM
Definition: logicalproto.h:31
TupleConversionMap * convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc)
Definition: tupconvert.c:102
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3702
bool is_publishable_relation(Relation rel)
static void pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: pgoutput.c:244
List * publication_names
Definition: pgoutput.h:25
#define InvalidTransactionId
Definition: transam.h:31
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
unsigned int uint32
Definition: c.h:375
#define LOGICALREP_PROTO_MAX_VERSION_NUM
Definition: logicalproto.h:34
struct ReorderBufferTXN * toptxn
void RelationClose(Relation relation)
Definition: relcache.c:2110
HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:139
void logicalrep_write_stream_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:798
static RelationSyncEntry * get_rel_sync_entry(PGOutputData *data, Oid relid)
Definition: pgoutput.c:938
List * lappend_int(List *list, int datum)
Definition: list.c:339
bool get_rel_relispartition(Oid relid)
Definition: lsyscache.c:1939
Node * arg
Definition: parsenodes.h:734
List * lappend(List *list, void *datum)
Definition: list.c:321
void logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn)
Definition: proto.c:45
static HTAB * RelationSyncCache
Definition: pgoutput.c:120
static bool get_schema_sent_in_streamed_txn(RelationSyncEntry *entry, TransactionId xid)
Definition: pgoutput.c:899
#define HASH_BLOBS
Definition: hsearch.h:86
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx)
Definition: logical.c:611
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1434
void * palloc0(Size size)
Definition: mcxt.c:981
LogicalDecodeChangeCB change_cb
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:326
uintptr_t Datum
Definition: postgres.h:367
LogicalDecodeStreamTruncateCB stream_truncate_cb
TransactionId xid
Size keysize
Definition: hsearch.h:71
PG_MODULE_MAGIC
Definition: pgoutput.c:31
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define LOGICALREP_PROTO_STREAM_VERSION_NUM
Definition: logicalproto.h:33
static void set_schema_sent_in_streamed_txn(RelationSyncEntry *entry, TransactionId xid)
Definition: pgoutput.c:917
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:585
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: pgoutput.c:690
bool binary
Definition: pgoutput.h:27
static int list_length(const List *l)
Definition: pg_list.h:149
LogicalDecodeShutdownCB shutdown_cb
LogicalDecodeStreamCommitCB stream_commit_cb
static List * LoadPublications(List *pubnames)
Definition: pgoutput.c:716
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1401
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1391
LogicalDecodeStartupCB startup_cb
#define InvalidRepOriginId
Definition: origin.h:33
List * publications
Definition: pgoutput.h:26
LogicalDecodeStreamStartCB stream_start_cb
static void init_rel_sync_cache(MemoryContext decoding_context)
Definition: pgoutput.c:867
#define FirstGenbkiObjectId
Definition: transam.h:188
static void cleanup_rel_sync_cache(TransactionId xid, bool is_commit)
Definition: pgoutput.c:1079
int errmsg(const char *fmt,...)
Definition: elog.c:821
static void pgoutput_stream_stop(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:791
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:598
void list_free(List *list)
Definition: list.c:1376
struct ReorderBufferChange::@98::@100 truncate
#define elog(elevel,...)
Definition: elog.h:214
StringInfo out
Definition: logical.h:70
struct ReorderBufferChange::@98::@99 tp
int i
void logicalrep_write_typ(StringInfo out, TransactionId xid, Oid typoid)
Definition: proto.c:422
void * arg
LogicalDecodeBeginCB begin_cb
char * defname
Definition: parsenodes.h:733
void logicalrep_write_update(StringInfo out, TransactionId xid, Relation rel, HeapTuple oldtuple, HeapTuple newtuple, bool binary)
Definition: proto.c:185
LogicalDecodeStreamStopCB stream_stop_cb
static void pgoutput_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: pgoutput.c:625
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
static void pgoutput_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:334
LogicalDecodeFilterByOriginCB filter_by_origin_cb
Definition: pg_list.h:50
LogicalDecodeStreamChangeCB stream_change_cb
List * get_partition_ancestors(Oid relid)
Definition: partition.c:115
#define RelationGetRelid(relation)
Definition: rel.h:456
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:2004
#define lfirst_oid(lc)
Definition: pg_list.h:171
static void pgoutput_stream_abort(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: pgoutput.c:810
MemoryContext CacheMemoryContext
Definition: mcxt.c:47
PublicationActions pubactions
Definition: pgoutput.c:100
bool scanint8(const char *str, bool errorOK, int64 *result)
Definition: int8.c:55
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:905
uint32 protocol_version
Definition: pgoutput.h:24