PostgreSQL Source Code  git master
worker.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  * worker.c
3  * PostgreSQL logical replication worker (apply)
4  *
5  * Copyright (c) 2016-2020, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/backend/replication/logical/worker.c
9  *
10  * NOTES
11  * This file contains the worker which applies logical changes as they come
12  * from remote logical replication stream.
13  *
14  * The main worker (apply) is started by logical replication worker
15  * launcher for every enabled subscription in a database. It uses
16  * walsender protocol to communicate with publisher.
17  *
18  * This module includes server facing code and shares libpqwalreceiver
19  * module with walreceiver for providing the libpq specific functionality.
20  *
21  *-------------------------------------------------------------------------
22  */
23 
24 #include "postgres.h"
25 
26 #include "access/table.h"
27 #include "access/tableam.h"
28 #include "access/xact.h"
29 #include "access/xlog_internal.h"
30 #include "catalog/catalog.h"
31 #include "catalog/namespace.h"
32 #include "catalog/partition.h"
33 #include "catalog/pg_inherits.h"
36 #include "commands/tablecmds.h"
37 #include "commands/trigger.h"
38 #include "executor/executor.h"
39 #include "executor/execPartition.h"
41 #include "funcapi.h"
42 #include "libpq/pqformat.h"
43 #include "libpq/pqsignal.h"
44 #include "mb/pg_wchar.h"
45 #include "miscadmin.h"
46 #include "nodes/makefuncs.h"
47 #include "optimizer/optimizer.h"
48 #include "parser/analyze.h"
49 #include "parser/parse_relation.h"
50 #include "pgstat.h"
51 #include "postmaster/bgworker.h"
52 #include "postmaster/interrupt.h"
53 #include "postmaster/postmaster.h"
54 #include "postmaster/walwriter.h"
55 #include "replication/decode.h"
56 #include "replication/logical.h"
60 #include "replication/origin.h"
62 #include "replication/snapbuild.h"
65 #include "rewrite/rewriteHandler.h"
66 #include "storage/bufmgr.h"
67 #include "storage/ipc.h"
68 #include "storage/lmgr.h"
69 #include "storage/proc.h"
70 #include "storage/procarray.h"
71 #include "tcop/tcopprot.h"
72 #include "utils/builtins.h"
73 #include "utils/catcache.h"
74 #include "utils/datum.h"
75 #include "utils/fmgroids.h"
76 #include "utils/guc.h"
77 #include "utils/inval.h"
78 #include "utils/lsyscache.h"
79 #include "utils/memutils.h"
80 #include "utils/rel.h"
81 #include "utils/syscache.h"
82 #include "utils/timeout.h"
83 
84 #define NAPTIME_PER_CYCLE 1000 /* max sleep time between cycles (1s) */
85 
86 typedef struct FlushPosition
87 {
92 
94 
95 typedef struct SlotErrCallbackArg
96 {
101 
104 
106 
108 bool MySubscriptionValid = false;
109 
112 
113 static void send_feedback(XLogRecPtr recvpos, bool force, bool requestReply);
114 
115 static void store_flush_position(XLogRecPtr remote_lsn);
116 
117 static void maybe_reread_subscription(void);
118 
119 static void apply_handle_insert_internal(ResultRelInfo *relinfo,
120  EState *estate, TupleTableSlot *remoteslot);
121 static void apply_handle_update_internal(ResultRelInfo *relinfo,
122  EState *estate, TupleTableSlot *remoteslot,
123  LogicalRepTupleData *newtup,
124  LogicalRepRelMapEntry *relmapentry);
125 static void apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
126  TupleTableSlot *remoteslot,
127  LogicalRepRelation *remoterel);
128 static bool FindReplTupleInLocalRel(EState *estate, Relation localrel,
129  LogicalRepRelation *remoterel,
130  TupleTableSlot *remoteslot,
131  TupleTableSlot **localslot);
132 static void apply_handle_tuple_routing(ResultRelInfo *relinfo,
133  EState *estate,
134  TupleTableSlot *remoteslot,
135  LogicalRepTupleData *newtup,
136  LogicalRepRelMapEntry *relmapentry,
137  CmdType operation);
138 
139 /*
140  * Should this worker apply changes for given relation.
141  *
142  * This is mainly needed for initial relation data sync as that runs in
143  * separate worker process running in parallel and we need some way to skip
144  * changes coming to the main apply worker during the sync of a table.
145  *
146  * Note we need to do smaller or equals comparison for SYNCDONE state because
147  * it might hold position of end of initial slot consistent point WAL
148  * record + 1 (ie start of next record) and next record can be COMMIT of
149  * transaction we are now processing (which is what we set remote_final_lsn
150  * to in apply_handle_begin).
151  */
152 static bool
154 {
155  if (am_tablesync_worker())
156  return MyLogicalRepWorker->relid == rel->localreloid;
157  else
158  return (rel->state == SUBREL_STATE_READY ||
159  (rel->state == SUBREL_STATE_SYNCDONE &&
160  rel->statelsn <= remote_final_lsn));
161 }
162 
163 /*
164  * Make sure that we started local transaction.
165  *
166  * Also switches to ApplyMessageContext as necessary.
167  */
168 static bool
170 {
171  if (IsTransactionState())
172  {
174 
175  if (CurrentMemoryContext != ApplyMessageContext)
176  MemoryContextSwitchTo(ApplyMessageContext);
177 
178  return false;
179  }
180 
183 
185 
186  MemoryContextSwitchTo(ApplyMessageContext);
187  return true;
188 }
189 
190 
191 /*
192  * Executor state preparation for evaluation of constraint expressions,
193  * indexes and triggers.
194  *
195  * This is based on similar code in copy.c
196  */
197 static EState *
199 {
200  EState *estate;
201  ResultRelInfo *resultRelInfo;
202  RangeTblEntry *rte;
203 
204  estate = CreateExecutorState();
205 
206  rte = makeNode(RangeTblEntry);
207  rte->rtekind = RTE_RELATION;
208  rte->relid = RelationGetRelid(rel->localrel);
209  rte->relkind = rel->localrel->rd_rel->relkind;
211  ExecInitRangeTable(estate, list_make1(rte));
212 
213  resultRelInfo = makeNode(ResultRelInfo);
214  InitResultRelInfo(resultRelInfo, rel->localrel, 1, NULL, 0);
215 
216  estate->es_result_relations = resultRelInfo;
217  estate->es_num_result_relations = 1;
218  estate->es_result_relation_info = resultRelInfo;
219 
220  estate->es_output_cid = GetCurrentCommandId(true);
221 
222  /* Prepare to catch AFTER triggers. */
224 
225  return estate;
226 }
227 
228 /*
229  * Executes default values for columns for which we can't map to remote
230  * relation columns.
231  *
232  * This allows us to support tables which have more columns on the downstream
233  * than on the upstream.
234  */
235 static void
237  TupleTableSlot *slot)
238 {
239  TupleDesc desc = RelationGetDescr(rel->localrel);
240  int num_phys_attrs = desc->natts;
241  int i;
242  int attnum,
243  num_defaults = 0;
244  int *defmap;
245  ExprState **defexprs;
246  ExprContext *econtext;
247 
248  econtext = GetPerTupleExprContext(estate);
249 
250  /* We got all the data via replication, no need to evaluate anything. */
251  if (num_phys_attrs == rel->remoterel.natts)
252  return;
253 
254  defmap = (int *) palloc(num_phys_attrs * sizeof(int));
255  defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
256 
257  Assert(rel->attrmap->maplen == num_phys_attrs);
258  for (attnum = 0; attnum < num_phys_attrs; attnum++)
259  {
260  Expr *defexpr;
261 
262  if (TupleDescAttr(desc, attnum)->attisdropped || TupleDescAttr(desc, attnum)->attgenerated)
263  continue;
264 
265  if (rel->attrmap->attnums[attnum] >= 0)
266  continue;
267 
268  defexpr = (Expr *) build_column_default(rel->localrel, attnum + 1);
269 
270  if (defexpr != NULL)
271  {
272  /* Run the expression through planner */
273  defexpr = expression_planner(defexpr);
274 
275  /* Initialize executable expression in copycontext */
276  defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
277  defmap[num_defaults] = attnum;
278  num_defaults++;
279  }
280 
281  }
282 
283  for (i = 0; i < num_defaults; i++)
284  slot->tts_values[defmap[i]] =
285  ExecEvalExpr(defexprs[i], econtext, &slot->tts_isnull[defmap[i]]);
286 }
287 
288 /*
289  * Error callback to give more context info about type conversion failure.
290  */
291 static void
293 {
294  SlotErrCallbackArg *errarg = (SlotErrCallbackArg *) arg;
296  char *remotetypname;
297  Oid remotetypoid,
298  localtypoid;
299 
300  /* Nothing to do if remote attribute number is not set */
301  if (errarg->remote_attnum < 0)
302  return;
303 
304  rel = errarg->rel;
305  remotetypoid = rel->remoterel.atttyps[errarg->remote_attnum];
306 
307  /* Fetch remote type name from the LogicalRepTypMap cache */
308  remotetypname = logicalrep_typmap_gettypname(remotetypoid);
309 
310  /* Fetch local type OID from the local sys cache */
311  localtypoid = get_atttype(rel->localreloid, errarg->local_attnum + 1);
312 
313  errcontext("processing remote data for replication target relation \"%s.%s\" column \"%s\", "
314  "remote type %s, local type %s",
315  rel->remoterel.nspname, rel->remoterel.relname,
316  rel->remoterel.attnames[errarg->remote_attnum],
317  remotetypname,
318  format_type_be(localtypoid));
319 }
320 
321 /*
322  * Store data in C string form into slot.
323  * This is similar to BuildTupleFromCStrings but TupleTableSlot fits our
324  * use better.
325  */
326 static void
328  char **values)
329 {
330  int natts = slot->tts_tupleDescriptor->natts;
331  int i;
332  SlotErrCallbackArg errarg;
333  ErrorContextCallback errcallback;
334 
335  ExecClearTuple(slot);
336 
337  /* Push callback + info on the error context stack */
338  errarg.rel = rel;
339  errarg.local_attnum = -1;
340  errarg.remote_attnum = -1;
341  errcallback.callback = slot_store_error_callback;
342  errcallback.arg = (void *) &errarg;
343  errcallback.previous = error_context_stack;
344  error_context_stack = &errcallback;
345 
346  /* Call the "in" function for each non-dropped attribute */
347  Assert(natts == rel->attrmap->maplen);
348  for (i = 0; i < natts; i++)
349  {
351  int remoteattnum = rel->attrmap->attnums[i];
352 
353  if (!att->attisdropped && remoteattnum >= 0 &&
354  values[remoteattnum] != NULL)
355  {
356  Oid typinput;
357  Oid typioparam;
358 
359  errarg.local_attnum = i;
360  errarg.remote_attnum = remoteattnum;
361 
362  getTypeInputInfo(att->atttypid, &typinput, &typioparam);
363  slot->tts_values[i] =
364  OidInputFunctionCall(typinput, values[remoteattnum],
365  typioparam, att->atttypmod);
366  slot->tts_isnull[i] = false;
367 
368  errarg.local_attnum = -1;
369  errarg.remote_attnum = -1;
370  }
371  else
372  {
373  /*
374  * We assign NULL to dropped attributes, NULL values, and missing
375  * values (missing values should be later filled using
376  * slot_fill_defaults).
377  */
378  slot->tts_values[i] = (Datum) 0;
379  slot->tts_isnull[i] = true;
380  }
381  }
382 
383  /* Pop the error context stack */
384  error_context_stack = errcallback.previous;
385 
386  ExecStoreVirtualTuple(slot);
387 }
388 
389 /*
390  * Replace selected columns with user data provided as C strings.
391  * This is somewhat similar to heap_modify_tuple but also calls the type
392  * input functions on the user data.
393  * "slot" is filled with a copy of the tuple in "srcslot", with
394  * columns selected by the "replaces" array replaced with data values
395  * from "values".
396  * Caution: unreplaced pass-by-ref columns in "slot" will point into the
397  * storage for "srcslot". This is OK for current usage, but someday we may
398  * need to materialize "slot" at the end to make it independent of "srcslot".
399  */
400 static void
403  char **values, bool *replaces)
404 {
405  int natts = slot->tts_tupleDescriptor->natts;
406  int i;
407  SlotErrCallbackArg errarg;
408  ErrorContextCallback errcallback;
409 
410  /* We'll fill "slot" with a virtual tuple, so we must start with ... */
411  ExecClearTuple(slot);
412 
413  /*
414  * Copy all the column data from srcslot, so that we'll have valid values
415  * for unreplaced columns.
416  */
417  Assert(natts == srcslot->tts_tupleDescriptor->natts);
418  slot_getallattrs(srcslot);
419  memcpy(slot->tts_values, srcslot->tts_values, natts * sizeof(Datum));
420  memcpy(slot->tts_isnull, srcslot->tts_isnull, natts * sizeof(bool));
421 
422  /* For error reporting, push callback + info on the error context stack */
423  errarg.rel = rel;
424  errarg.local_attnum = -1;
425  errarg.remote_attnum = -1;
426  errcallback.callback = slot_store_error_callback;
427  errcallback.arg = (void *) &errarg;
428  errcallback.previous = error_context_stack;
429  error_context_stack = &errcallback;
430 
431  /* Call the "in" function for each replaced attribute */
432  Assert(natts == rel->attrmap->maplen);
433  for (i = 0; i < natts; i++)
434  {
436  int remoteattnum = rel->attrmap->attnums[i];
437 
438  if (remoteattnum < 0)
439  continue;
440 
441  if (!replaces[remoteattnum])
442  continue;
443 
444  if (values[remoteattnum] != NULL)
445  {
446  Oid typinput;
447  Oid typioparam;
448 
449  errarg.local_attnum = i;
450  errarg.remote_attnum = remoteattnum;
451 
452  getTypeInputInfo(att->atttypid, &typinput, &typioparam);
453  slot->tts_values[i] =
454  OidInputFunctionCall(typinput, values[remoteattnum],
455  typioparam, att->atttypmod);
456  slot->tts_isnull[i] = false;
457 
458  errarg.local_attnum = -1;
459  errarg.remote_attnum = -1;
460  }
461  else
462  {
463  slot->tts_values[i] = (Datum) 0;
464  slot->tts_isnull[i] = true;
465  }
466  }
467 
468  /* Pop the error context stack */
469  error_context_stack = errcallback.previous;
470 
471  /* And finally, declare that "slot" contains a valid virtual tuple */
472  ExecStoreVirtualTuple(slot);
473 }
474 
475 /*
476  * Handle BEGIN message.
477  */
478 static void
480 {
481  LogicalRepBeginData begin_data;
482 
483  logicalrep_read_begin(s, &begin_data);
484 
485  remote_final_lsn = begin_data.final_lsn;
486 
487  in_remote_transaction = true;
488 
490 }
491 
492 /*
493  * Handle COMMIT message.
494  *
495  * TODO, support tracking of multiple origins
496  */
497 static void
499 {
500  LogicalRepCommitData commit_data;
501 
502  logicalrep_read_commit(s, &commit_data);
503 
504  Assert(commit_data.commit_lsn == remote_final_lsn);
505 
506  /* The synchronization worker runs in single transaction. */
508  {
509  /*
510  * Update origin state so we can restart streaming from correct
511  * position in case of crash.
512  */
515 
517  pgstat_report_stat(false);
518 
519  store_flush_position(commit_data.end_lsn);
520  }
521  else
522  {
523  /* Process any invalidation messages that might have accumulated. */
526  }
527 
528  in_remote_transaction = false;
529 
530  /* Process any tables that are being synchronized in parallel. */
531  process_syncing_tables(commit_data.end_lsn);
532 
534 }
535 
536 /*
537  * Handle ORIGIN message.
538  *
539  * TODO, support tracking of multiple origins
540  */
541 static void
543 {
544  /*
545  * ORIGIN message can only come inside remote transaction and before any
546  * actual writes.
547  */
548  if (!in_remote_transaction ||
550  ereport(ERROR,
551  (errcode(ERRCODE_PROTOCOL_VIOLATION),
552  errmsg("ORIGIN message sent out of order")));
553 }
554 
555 /*
556  * Handle RELATION message.
557  *
558  * Note we don't do validation against local schema here. The validation
559  * against local schema is postponed until first change for given relation
560  * comes as we only care about it when applying changes for it anyway and we
561  * do less locking this way.
562  */
563 static void
565 {
566  LogicalRepRelation *rel;
567 
568  rel = logicalrep_read_rel(s);
570 }
571 
572 /*
573  * Handle TYPE message.
574  *
575  * Note we don't do local mapping here, that's done when the type is
576  * actually used.
577  */
578 static void
580 {
581  LogicalRepTyp typ;
582 
583  logicalrep_read_typ(s, &typ);
585 }
586 
587 /*
588  * Get replica identity index or if it is not defined a primary key.
589  *
590  * If neither is defined, returns InvalidOid
591  */
592 static Oid
594 {
595  Oid idxoid;
596 
597  idxoid = RelationGetReplicaIndex(rel);
598 
599  if (!OidIsValid(idxoid))
600  idxoid = RelationGetPrimaryKeyIndex(rel);
601 
602  return idxoid;
603 }
604 
605 /*
606  * Handle INSERT message.
607  */
608 
609 static void
611 {
613  LogicalRepTupleData newtup;
614  LogicalRepRelId relid;
615  EState *estate;
616  TupleTableSlot *remoteslot;
617  MemoryContext oldctx;
618 
620 
621  relid = logicalrep_read_insert(s, &newtup);
624  {
625  /*
626  * The relation can't become interesting in the middle of the
627  * transaction so it's safe to unlock it.
628  */
630  return;
631  }
632 
633  /* Initialize the executor state. */
634  estate = create_estate_for_relation(rel);
635  remoteslot = ExecInitExtraTupleSlot(estate,
637  &TTSOpsVirtual);
638 
639  /* Input functions may need an active snapshot, so get one */
641 
642  /* Process and store remote tuple in the slot */
644  slot_store_cstrings(remoteslot, rel, newtup.values);
645  slot_fill_defaults(rel, estate, remoteslot);
646  MemoryContextSwitchTo(oldctx);
647 
648  /* For a partitioned table, insert the tuple into a partition. */
649  if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
651  remoteslot, NULL, rel, CMD_INSERT);
652  else
654  remoteslot);
655 
657 
658  /* Handle queued AFTER triggers. */
659  AfterTriggerEndQuery(estate);
660 
661  ExecResetTupleTable(estate->es_tupleTable, false);
662  FreeExecutorState(estate);
663 
665 
667 }
668 
669 /* Workhorse for apply_handle_insert() */
670 static void
672  EState *estate, TupleTableSlot *remoteslot)
673 {
674  ExecOpenIndices(relinfo, false);
675 
676  /* Do the insert. */
677  ExecSimpleRelationInsert(estate, remoteslot);
678 
679  /* Cleanup. */
680  ExecCloseIndices(relinfo);
681 }
682 
683 /*
684  * Check if the logical replication relation is updatable and throw
685  * appropriate error if it isn't.
686  */
687 static void
689 {
690  /* Updatable, no error. */
691  if (rel->updatable)
692  return;
693 
694  /*
695  * We are in error mode so it's fine this is somewhat slow. It's better to
696  * give user correct error.
697  */
699  {
700  ereport(ERROR,
701  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
702  errmsg("publisher did not send replica identity column "
703  "expected by the logical replication target relation \"%s.%s\"",
704  rel->remoterel.nspname, rel->remoterel.relname)));
705  }
706 
707  ereport(ERROR,
708  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
709  errmsg("logical replication target relation \"%s.%s\" has "
710  "neither REPLICA IDENTITY index nor PRIMARY "
711  "KEY and published relation does not have "
712  "REPLICA IDENTITY FULL",
713  rel->remoterel.nspname, rel->remoterel.relname)));
714 }
715 
716 /*
717  * Handle UPDATE message.
718  *
719  * TODO: FDW support
720  */
721 static void
723 {
725  LogicalRepRelId relid;
726  EState *estate;
727  LogicalRepTupleData oldtup;
728  LogicalRepTupleData newtup;
729  bool has_oldtup;
730  TupleTableSlot *remoteslot;
731  RangeTblEntry *target_rte;
732  MemoryContext oldctx;
733 
735 
736  relid = logicalrep_read_update(s, &has_oldtup, &oldtup,
737  &newtup);
740  {
741  /*
742  * The relation can't become interesting in the middle of the
743  * transaction so it's safe to unlock it.
744  */
746  return;
747  }
748 
749  /* Check if we can do the update. */
751 
752  /* Initialize the executor state. */
753  estate = create_estate_for_relation(rel);
754  remoteslot = ExecInitExtraTupleSlot(estate,
756  &TTSOpsVirtual);
757 
758  /*
759  * Populate updatedCols so that per-column triggers can fire. This could
760  * include more columns than were actually changed on the publisher
761  * because the logical replication protocol doesn't contain that
762  * information. But it would for example exclude columns that only exist
763  * on the subscriber, since we are not touching those.
764  */
765  target_rte = list_nth(estate->es_range_table, 0);
766  for (int i = 0; i < remoteslot->tts_tupleDescriptor->natts; i++)
767  {
768  if (newtup.changed[i])
769  target_rte->updatedCols = bms_add_member(target_rte->updatedCols,
771  }
772 
774 
776 
777  /* Build the search tuple. */
779  slot_store_cstrings(remoteslot, rel,
780  has_oldtup ? oldtup.values : newtup.values);
781  MemoryContextSwitchTo(oldctx);
782 
783  /* For a partitioned table, apply update to correct partition. */
784  if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
786  remoteslot, &newtup, rel, CMD_UPDATE);
787  else
789  remoteslot, &newtup, rel);
790 
792 
793  /* Handle queued AFTER triggers. */
794  AfterTriggerEndQuery(estate);
795 
796  ExecResetTupleTable(estate->es_tupleTable, false);
797  FreeExecutorState(estate);
798 
800 
802 }
803 
804 /* Workhorse for apply_handle_update() */
805 static void
807  EState *estate, TupleTableSlot *remoteslot,
808  LogicalRepTupleData *newtup,
809  LogicalRepRelMapEntry *relmapentry)
810 {
811  Relation localrel = relinfo->ri_RelationDesc;
812  EPQState epqstate;
813  TupleTableSlot *localslot;
814  bool found;
815  MemoryContext oldctx;
816 
817  EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
818  ExecOpenIndices(relinfo, false);
819 
820  found = FindReplTupleInLocalRel(estate, localrel,
821  &relmapentry->remoterel,
822  remoteslot, &localslot);
823  ExecClearTuple(remoteslot);
824 
825  /*
826  * Tuple found.
827  *
828  * Note this will fail if there are other conflicting unique indexes.
829  */
830  if (found)
831  {
832  /* Process and store remote tuple in the slot */
834  slot_modify_cstrings(remoteslot, localslot, relmapentry,
835  newtup->values, newtup->changed);
836  MemoryContextSwitchTo(oldctx);
837 
838  EvalPlanQualSetSlot(&epqstate, remoteslot);
839 
840  /* Do the actual update. */
841  ExecSimpleRelationUpdate(estate, &epqstate, localslot, remoteslot);
842  }
843  else
844  {
845  /*
846  * The tuple to be updated could not be found.
847  *
848  * TODO what to do here, change the log level to LOG perhaps?
849  */
850  elog(DEBUG1,
851  "logical replication did not find row for update "
852  "in replication target relation \"%s\"",
853  RelationGetRelationName(localrel));
854  }
855 
856  /* Cleanup. */
857  ExecCloseIndices(relinfo);
858  EvalPlanQualEnd(&epqstate);
859 }
860 
861 /*
862  * Handle DELETE message.
863  *
864  * TODO: FDW support
865  */
866 static void
868 {
870  LogicalRepTupleData oldtup;
871  LogicalRepRelId relid;
872  EState *estate;
873  TupleTableSlot *remoteslot;
874  MemoryContext oldctx;
875 
877 
878  relid = logicalrep_read_delete(s, &oldtup);
881  {
882  /*
883  * The relation can't become interesting in the middle of the
884  * transaction so it's safe to unlock it.
885  */
887  return;
888  }
889 
890  /* Check if we can do the delete. */
892 
893  /* Initialize the executor state. */
894  estate = create_estate_for_relation(rel);
895  remoteslot = ExecInitExtraTupleSlot(estate,
897  &TTSOpsVirtual);
898 
900 
901  /* Build the search tuple. */
903  slot_store_cstrings(remoteslot, rel, oldtup.values);
904  MemoryContextSwitchTo(oldctx);
905 
906  /* For a partitioned table, apply delete to correct partition. */
907  if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
909  remoteslot, NULL, rel, CMD_DELETE);
910  else
912  remoteslot, &rel->remoterel);
913 
915 
916  /* Handle queued AFTER triggers. */
917  AfterTriggerEndQuery(estate);
918 
919  ExecResetTupleTable(estate->es_tupleTable, false);
920  FreeExecutorState(estate);
921 
923 
925 }
926 
927 /* Workhorse for apply_handle_delete() */
928 static void
930  TupleTableSlot *remoteslot,
931  LogicalRepRelation *remoterel)
932 {
933  Relation localrel = relinfo->ri_RelationDesc;
934  EPQState epqstate;
935  TupleTableSlot *localslot;
936  bool found;
937 
938  EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
939  ExecOpenIndices(relinfo, false);
940 
941  found = FindReplTupleInLocalRel(estate, localrel, remoterel,
942  remoteslot, &localslot);
943 
944  /* If found delete it. */
945  if (found)
946  {
947  EvalPlanQualSetSlot(&epqstate, localslot);
948 
949  /* Do the actual delete. */
950  ExecSimpleRelationDelete(estate, &epqstate, localslot);
951  }
952  else
953  {
954  /* The tuple to be deleted could not be found. */
955  elog(DEBUG1,
956  "logical replication could not find row for delete "
957  "in replication target relation \"%s\"",
958  RelationGetRelationName(localrel));
959  }
960 
961  /* Cleanup. */
962  ExecCloseIndices(relinfo);
963  EvalPlanQualEnd(&epqstate);
964 }
965 
966 /*
967  * Try to find a tuple received from the publication side (in 'remoteslot') in
968  * the corresponding local relation using either replica identity index,
969  * primary key or if needed, sequential scan.
970  *
971  * Local tuple, if found, is returned in '*localslot'.
972  */
973 static bool
975  LogicalRepRelation *remoterel,
976  TupleTableSlot *remoteslot,
977  TupleTableSlot **localslot)
978 {
979  Oid idxoid;
980  bool found;
981 
982  *localslot = table_slot_create(localrel, &estate->es_tupleTable);
983 
984  idxoid = GetRelationIdentityOrPK(localrel);
985  Assert(OidIsValid(idxoid) ||
986  (remoterel->replident == REPLICA_IDENTITY_FULL));
987 
988  if (OidIsValid(idxoid))
989  found = RelationFindReplTupleByIndex(localrel, idxoid,
991  remoteslot, *localslot);
992  else
994  remoteslot, *localslot);
995 
996  return found;
997 }
998 
999 /*
1000  * This handles insert, update, delete on a partitioned table.
1001  */
1002 static void
1004  EState *estate,
1005  TupleTableSlot *remoteslot,
1006  LogicalRepTupleData *newtup,
1007  LogicalRepRelMapEntry *relmapentry,
1008  CmdType operation)
1009 {
1010  Relation parentrel = relinfo->ri_RelationDesc;
1011  ModifyTableState *mtstate = NULL;
1012  PartitionTupleRouting *proute = NULL;
1013  ResultRelInfo *partrelinfo;
1014  Relation partrel;
1015  TupleTableSlot *remoteslot_part;
1016  PartitionRoutingInfo *partinfo;
1017  TupleConversionMap *map;
1018  MemoryContext oldctx;
1019 
1020  /* ModifyTableState is needed for ExecFindPartition(). */
1021  mtstate = makeNode(ModifyTableState);
1022  mtstate->ps.plan = NULL;
1023  mtstate->ps.state = estate;
1024  mtstate->operation = operation;
1025  mtstate->resultRelInfo = relinfo;
1026  proute = ExecSetupPartitionTupleRouting(estate, mtstate, parentrel);
1027 
1028  /*
1029  * Find the partition to which the "search tuple" belongs.
1030  */
1031  Assert(remoteslot != NULL);
1033  partrelinfo = ExecFindPartition(mtstate, relinfo, proute,
1034  remoteslot, estate);
1035  Assert(partrelinfo != NULL);
1036  partrel = partrelinfo->ri_RelationDesc;
1037 
1038  /*
1039  * To perform any of the operations below, the tuple must match the
1040  * partition's rowtype. Convert if needed or just copy, using a dedicated
1041  * slot to store the tuple in any case.
1042  */
1043  partinfo = partrelinfo->ri_PartitionInfo;
1044  remoteslot_part = partinfo->pi_PartitionTupleSlot;
1045  if (remoteslot_part == NULL)
1046  remoteslot_part = table_slot_create(partrel, &estate->es_tupleTable);
1047  map = partinfo->pi_RootToPartitionMap;
1048  if (map != NULL)
1049  remoteslot_part = execute_attr_map_slot(map->attrMap, remoteslot,
1050  remoteslot_part);
1051  else
1052  {
1053  remoteslot_part = ExecCopySlot(remoteslot_part, remoteslot);
1054  slot_getallattrs(remoteslot_part);
1055  }
1056  MemoryContextSwitchTo(oldctx);
1057 
1058  estate->es_result_relation_info = partrelinfo;
1059  switch (operation)
1060  {
1061  case CMD_INSERT:
1062  apply_handle_insert_internal(partrelinfo, estate,
1063  remoteslot_part);
1064  break;
1065 
1066  case CMD_DELETE:
1067  apply_handle_delete_internal(partrelinfo, estate,
1068  remoteslot_part,
1069  &relmapentry->remoterel);
1070  break;
1071 
1072  case CMD_UPDATE:
1073 
1074  /*
1075  * For UPDATE, depending on whether or not the updated tuple
1076  * satisfies the partition's constraint, perform a simple UPDATE
1077  * of the partition or move the updated tuple into a different
1078  * suitable partition.
1079  */
1080  {
1081  AttrMap *attrmap = map ? map->attrMap : NULL;
1082  LogicalRepRelMapEntry *part_entry;
1083  TupleTableSlot *localslot;
1084  ResultRelInfo *partrelinfo_new;
1085  bool found;
1086 
1087  part_entry = logicalrep_partition_open(relmapentry, partrel,
1088  attrmap);
1089 
1090  /* Get the matching local tuple from the partition. */
1091  found = FindReplTupleInLocalRel(estate, partrel,
1092  &part_entry->remoterel,
1093  remoteslot_part, &localslot);
1094 
1096  if (found)
1097  {
1098  /* Apply the update. */
1099  slot_modify_cstrings(remoteslot_part, localslot,
1100  part_entry,
1101  newtup->values, newtup->changed);
1102  MemoryContextSwitchTo(oldctx);
1103  }
1104  else
1105  {
1106  /*
1107  * The tuple to be updated could not be found.
1108  *
1109  * TODO what to do here, change the log level to LOG
1110  * perhaps?
1111  */
1112  elog(DEBUG1,
1113  "logical replication did not find row for update "
1114  "in replication target relation \"%s\"",
1115  RelationGetRelationName(partrel));
1116  }
1117 
1118  /*
1119  * Does the updated tuple still satisfy the current
1120  * partition's constraint?
1121  */
1122  if (partrelinfo->ri_PartitionCheck == NULL ||
1123  ExecPartitionCheck(partrelinfo, remoteslot_part, estate,
1124  false))
1125  {
1126  /*
1127  * Yes, so simply UPDATE the partition. We don't call
1128  * apply_handle_update_internal() here, which would
1129  * normally do the following work, to avoid repeating some
1130  * work already done above to find the local tuple in the
1131  * partition.
1132  */
1133  EPQState epqstate;
1134 
1135  EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
1136  ExecOpenIndices(partrelinfo, false);
1137 
1138  EvalPlanQualSetSlot(&epqstate, remoteslot_part);
1139  ExecSimpleRelationUpdate(estate, &epqstate, localslot,
1140  remoteslot_part);
1141  ExecCloseIndices(partrelinfo);
1142  EvalPlanQualEnd(&epqstate);
1143  }
1144  else
1145  {
1146  /* Move the tuple into the new partition. */
1147 
1148  /*
1149  * New partition will be found using tuple routing, which
1150  * can only occur via the parent table. We might need to
1151  * convert the tuple to the parent's rowtype. Note that
1152  * this is the tuple found in the partition, not the
1153  * original search tuple received by this function.
1154  */
1155  if (map)
1156  {
1157  TupleConversionMap *PartitionToRootMap =
1159  RelationGetDescr(parentrel));
1160 
1161  remoteslot =
1162  execute_attr_map_slot(PartitionToRootMap->attrMap,
1163  remoteslot_part, remoteslot);
1164  }
1165  else
1166  {
1167  remoteslot = ExecCopySlot(remoteslot, remoteslot_part);
1168  slot_getallattrs(remoteslot);
1169  }
1170 
1171 
1172  /* Find the new partition. */
1174  partrelinfo_new = ExecFindPartition(mtstate, relinfo,
1175  proute, remoteslot,
1176  estate);
1177  MemoryContextSwitchTo(oldctx);
1178  Assert(partrelinfo_new != partrelinfo);
1179 
1180  /* DELETE old tuple found in the old partition. */
1181  estate->es_result_relation_info = partrelinfo;
1182  apply_handle_delete_internal(partrelinfo, estate,
1183  localslot,
1184  &relmapentry->remoterel);
1185 
1186  /* INSERT new tuple into the new partition. */
1187 
1188  /*
1189  * Convert the replacement tuple to match the destination
1190  * partition rowtype.
1191  */
1193  partrel = partrelinfo_new->ri_RelationDesc;
1194  partinfo = partrelinfo_new->ri_PartitionInfo;
1195  remoteslot_part = partinfo->pi_PartitionTupleSlot;
1196  if (remoteslot_part == NULL)
1197  remoteslot_part = table_slot_create(partrel,
1198  &estate->es_tupleTable);
1199  map = partinfo->pi_RootToPartitionMap;
1200  if (map != NULL)
1201  {
1202  remoteslot_part = execute_attr_map_slot(map->attrMap,
1203  remoteslot,
1204  remoteslot_part);
1205  }
1206  else
1207  {
1208  remoteslot_part = ExecCopySlot(remoteslot_part,
1209  remoteslot);
1210  slot_getallattrs(remoteslot);
1211  }
1212  MemoryContextSwitchTo(oldctx);
1213  estate->es_result_relation_info = partrelinfo_new;
1214  apply_handle_insert_internal(partrelinfo_new, estate,
1215  remoteslot_part);
1216  }
1217  }
1218  break;
1219 
1220  default:
1221  elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1222  break;
1223  }
1224 
1225  ExecCleanupTupleRouting(mtstate, proute);
1226 }
1227 
1228 /*
1229  * Handle TRUNCATE message.
1230  *
1231  * TODO: FDW support
1232  */
1233 static void
1235 {
1236  bool cascade = false;
1237  bool restart_seqs = false;
1238  List *remote_relids = NIL;
1239  List *remote_rels = NIL;
1240  List *rels = NIL;
1241  List *part_rels = NIL;
1242  List *relids = NIL;
1243  List *relids_logged = NIL;
1244  ListCell *lc;
1245 
1247 
1248  remote_relids = logicalrep_read_truncate(s, &cascade, &restart_seqs);
1249 
1250  foreach(lc, remote_relids)
1251  {
1252  LogicalRepRelId relid = lfirst_oid(lc);
1253  LogicalRepRelMapEntry *rel;
1254 
1255  rel = logicalrep_rel_open(relid, RowExclusiveLock);
1256  if (!should_apply_changes_for_rel(rel))
1257  {
1258  /*
1259  * The relation can't become interesting in the middle of the
1260  * transaction so it's safe to unlock it.
1261  */
1263  continue;
1264  }
1265 
1266  remote_rels = lappend(remote_rels, rel);
1267  rels = lappend(rels, rel->localrel);
1268  relids = lappend_oid(relids, rel->localreloid);
1270  relids_logged = lappend_oid(relids_logged, rel->localreloid);
1271 
1272  /*
1273  * Truncate partitions if we got a message to truncate a partitioned
1274  * table.
1275  */
1276  if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1277  {
1278  ListCell *child;
1279  List *children = find_all_inheritors(rel->localreloid,
1281  NULL);
1282 
1283  foreach(child, children)
1284  {
1285  Oid childrelid = lfirst_oid(child);
1286  Relation childrel;
1287 
1288  if (list_member_oid(relids, childrelid))
1289  continue;
1290 
1291  /* find_all_inheritors already got lock */
1292  childrel = table_open(childrelid, NoLock);
1293 
1294  /*
1295  * Ignore temp tables of other backends. See similar code in
1296  * ExecuteTruncate().
1297  */
1298  if (RELATION_IS_OTHER_TEMP(childrel))
1299  {
1300  table_close(childrel, RowExclusiveLock);
1301  continue;
1302  }
1303 
1304  rels = lappend(rels, childrel);
1305  part_rels = lappend(part_rels, childrel);
1306  relids = lappend_oid(relids, childrelid);
1307  /* Log this relation only if needed for logical decoding */
1308  if (RelationIsLogicallyLogged(childrel))
1309  relids_logged = lappend_oid(relids_logged, childrelid);
1310  }
1311  }
1312  }
1313 
1314  /*
1315  * Even if we used CASCADE on the upstream primary we explicitly default to
1316  * replaying changes without further cascading. This might be later
1317  * changeable with a user specified option.
1318  */
1319  ExecuteTruncateGuts(rels, relids, relids_logged, DROP_RESTRICT, restart_seqs);
1320 
1321  foreach(lc, remote_rels)
1322  {
1323  LogicalRepRelMapEntry *rel = lfirst(lc);
1324 
1326  }
1327  foreach(lc, part_rels)
1328  {
1329  Relation rel = lfirst(lc);
1330 
1331  table_close(rel, NoLock);
1332  }
1333 
1335 }
1336 
1337 
1338 /*
1339  * Logical replication protocol message dispatcher.
1340  */
1341 static void
1343 {
1344  char action = pq_getmsgbyte(s);
1345 
1346  switch (action)
1347  {
1348  /* BEGIN */
1349  case 'B':
1350  apply_handle_begin(s);
1351  break;
1352  /* COMMIT */
1353  case 'C':
1355  break;
1356  /* INSERT */
1357  case 'I':
1359  break;
1360  /* UPDATE */
1361  case 'U':
1363  break;
1364  /* DELETE */
1365  case 'D':
1367  break;
1368  /* TRUNCATE */
1369  case 'T':
1371  break;
1372  /* RELATION */
1373  case 'R':
1375  break;
1376  /* TYPE */
1377  case 'Y':
1378  apply_handle_type(s);
1379  break;
1380  /* ORIGIN */
1381  case 'O':
1383  break;
1384  default:
1385  ereport(ERROR,
1386  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1387  errmsg("invalid logical replication message type \"%c\"", action)));
1388  }
1389 }
1390 
1391 /*
1392  * Figure out which write/flush positions to report to the walsender process.
1393  *
1394  * We can't simply report back the last LSN the walsender sent us because the
1395  * local transaction might not yet be flushed to disk locally. Instead we
1396  * build a list that associates local with remote LSNs for every commit. When
1397  * reporting back the flush position to the sender we iterate that list and
1398  * check which entries on it are already locally flushed. Those we can report
1399  * as having been flushed.
1400  *
1401  * The have_pending_txes is true if there are outstanding transactions that
1402  * need to be flushed.
1403  */
1404 static void
1406  bool *have_pending_txes)
1407 {
1408  dlist_mutable_iter iter;
1409  XLogRecPtr local_flush = GetFlushRecPtr();
1410 
1411  *write = InvalidXLogRecPtr;
1412  *flush = InvalidXLogRecPtr;
1413 
1414  dlist_foreach_modify(iter, &lsn_mapping)
1415  {
1416  FlushPosition *pos =
1418 
1419  *write = pos->remote_end;
1420 
1421  if (pos->local_end <= local_flush)
1422  {
1423  *flush = pos->remote_end;
1424  dlist_delete(iter.cur);
1425  pfree(pos);
1426  }
1427  else
1428  {
1429  /*
1430  * Don't want to uselessly iterate over the rest of the list which
1431  * could potentially be long. Instead get the last element and
1432  * grab the write position from there.
1433  */
1435  &lsn_mapping);
1436  *write = pos->remote_end;
1437  *have_pending_txes = true;
1438  return;
1439  }
1440  }
1441 
1442  *have_pending_txes = !dlist_is_empty(&lsn_mapping);
1443 }
1444 
1445 /*
1446  * Store current remote/local lsn pair in the tracking list.
1447  */
1448 static void
1450 {
1451  FlushPosition *flushpos;
1452 
1453  /* Need to do this in permanent context */
1454  MemoryContextSwitchTo(ApplyContext);
1455 
1456  /* Track commit lsn */
1457  flushpos = (FlushPosition *) palloc(sizeof(FlushPosition));
1458  flushpos->local_end = XactLastCommitEnd;
1459  flushpos->remote_end = remote_lsn;
1460 
1461  dlist_push_tail(&lsn_mapping, &flushpos->node);
1462  MemoryContextSwitchTo(ApplyMessageContext);
1463 }
1464 
1465 
1466 /* Update statistics of the worker. */
1467 static void
1468 UpdateWorkerStats(XLogRecPtr last_lsn, TimestampTz send_time, bool reply)
1469 {
1470  MyLogicalRepWorker->last_lsn = last_lsn;
1471  MyLogicalRepWorker->last_send_time = send_time;
1473  if (reply)
1474  {
1475  MyLogicalRepWorker->reply_lsn = last_lsn;
1476  MyLogicalRepWorker->reply_time = send_time;
1477  }
1478 }
1479 
1480 /*
1481  * Apply main loop.
1482  */
1483 static void
1485 {
1486  TimestampTz last_recv_timestamp = GetCurrentTimestamp();
1487 
1488  /*
1489  * Init the ApplyMessageContext which we clean up after each replication
1490  * protocol message.
1491  */
1492  ApplyMessageContext = AllocSetContextCreate(ApplyContext,
1493  "ApplyMessageContext",
1495 
1496  /* mark as idle, before starting to loop */
1498 
1499  for (;;)
1500  {
1502  int rc;
1503  int len;
1504  char *buf = NULL;
1505  bool endofstream = false;
1506  bool ping_sent = false;
1507  long wait_time;
1508 
1510 
1511  MemoryContextSwitchTo(ApplyMessageContext);
1512 
1513  len = walrcv_receive(wrconn, &buf, &fd);
1514 
1515  if (len != 0)
1516  {
1517  /* Process the data */
1518  for (;;)
1519  {
1521 
1522  if (len == 0)
1523  {
1524  break;
1525  }
1526  else if (len < 0)
1527  {
1528  ereport(LOG,
1529  (errmsg("data stream from publisher has ended")));
1530  endofstream = true;
1531  break;
1532  }
1533  else
1534  {
1535  int c;
1536  StringInfoData s;
1537 
1538  /* Reset timeout. */
1539  last_recv_timestamp = GetCurrentTimestamp();
1540  ping_sent = false;
1541 
1542  /* Ensure we are reading the data into our memory context. */
1543  MemoryContextSwitchTo(ApplyMessageContext);
1544 
1545  s.data = buf;
1546  s.len = len;
1547  s.cursor = 0;
1548  s.maxlen = -1;
1549 
1550  c = pq_getmsgbyte(&s);
1551 
1552  if (c == 'w')
1553  {
1554  XLogRecPtr start_lsn;
1555  XLogRecPtr end_lsn;
1556  TimestampTz send_time;
1557 
1558  start_lsn = pq_getmsgint64(&s);
1559  end_lsn = pq_getmsgint64(&s);
1560  send_time = pq_getmsgint64(&s);
1561 
1562  if (last_received < start_lsn)
1563  last_received = start_lsn;
1564 
1565  if (last_received < end_lsn)
1566  last_received = end_lsn;
1567 
1568  UpdateWorkerStats(last_received, send_time, false);
1569 
1570  apply_dispatch(&s);
1571  }
1572  else if (c == 'k')
1573  {
1574  XLogRecPtr end_lsn;
1576  bool reply_requested;
1577 
1578  end_lsn = pq_getmsgint64(&s);
1579  timestamp = pq_getmsgint64(&s);
1580  reply_requested = pq_getmsgbyte(&s);
1581 
1582  if (last_received < end_lsn)
1583  last_received = end_lsn;
1584 
1585  send_feedback(last_received, reply_requested, false);
1586  UpdateWorkerStats(last_received, timestamp, true);
1587  }
1588  /* other message types are purposefully ignored */
1589 
1590  MemoryContextReset(ApplyMessageContext);
1591  }
1592 
1593  len = walrcv_receive(wrconn, &buf, &fd);
1594  }
1595  }
1596 
1597  /* confirm all writes so far */
1598  send_feedback(last_received, false, false);
1599 
1600  if (!in_remote_transaction)
1601  {
1602  /*
1603  * If we didn't get any transactions for a while there might be
1604  * unconsumed invalidation messages in the queue, consume them
1605  * now.
1606  */
1609 
1610  /* Process any table synchronization changes. */
1611  process_syncing_tables(last_received);
1612  }
1613 
1614  /* Cleanup the memory. */
1615  MemoryContextResetAndDeleteChildren(ApplyMessageContext);
1617 
1618  /* Check if we need to exit the streaming loop. */
1619  if (endofstream)
1620  {
1621  TimeLineID tli;
1622 
1623  walrcv_endstreaming(wrconn, &tli);
1624  break;
1625  }
1626 
1627  /*
1628  * Wait for more data or latch. If we have unflushed transactions,
1629  * wake up after WalWriterDelay to see if they've been flushed yet (in
1630  * which case we should send a feedback message). Otherwise, there's
1631  * no particular urgency about waking up unless we get data or a
1632  * signal.
1633  */
1634  if (!dlist_is_empty(&lsn_mapping))
1635  wait_time = WalWriterDelay;
1636  else
1637  wait_time = NAPTIME_PER_CYCLE;
1638 
1642  fd, wait_time,
1644 
1645  if (rc & WL_LATCH_SET)
1646  {
1649  }
1650 
1651  if (ConfigReloadPending)
1652  {
1653  ConfigReloadPending = false;
1655  }
1656 
1657  if (rc & WL_TIMEOUT)
1658  {
1659  /*
1660  * We didn't receive anything new. If we haven't heard anything
1661  * from the server for more than wal_receiver_timeout / 2, ping
1662  * the server. Also, if it's been longer than
1663  * wal_receiver_status_interval since the last update we sent,
1664  * send a status update to the primary anyway, to report any
1665  * progress in applying WAL.
1666  */
1667  bool requestReply = false;
1668 
1669  /*
1670  * Check if time since last receive from standby has reached the
1671  * configured limit.
1672  */
1673  if (wal_receiver_timeout > 0)
1674  {
1676  TimestampTz timeout;
1677 
1678  timeout =
1679  TimestampTzPlusMilliseconds(last_recv_timestamp,
1681 
1682  if (now >= timeout)
1683  ereport(ERROR,
1684  (errmsg("terminating logical replication worker due to timeout")));
1685 
1686  /*
1687  * We didn't receive anything new, for half of receiver
1688  * replication timeout. Ping the server.
1689  */
1690  if (!ping_sent)
1691  {
1692  timeout = TimestampTzPlusMilliseconds(last_recv_timestamp,
1693  (wal_receiver_timeout / 2));
1694  if (now >= timeout)
1695  {
1696  requestReply = true;
1697  ping_sent = true;
1698  }
1699  }
1700  }
1701 
1702  send_feedback(last_received, requestReply, requestReply);
1703  }
1704  }
1705 }
1706 
1707 /*
1708  * Send a Standby Status Update message to server.
1709  *
1710  * 'recvpos' is the latest LSN we've received data to, force is set if we need
1711  * to send a response to avoid timeouts.
1712  */
1713 static void
1714 send_feedback(XLogRecPtr recvpos, bool force, bool requestReply)
1715 {
1716  static StringInfo reply_message = NULL;
1717  static TimestampTz send_time = 0;
1718 
1719  static XLogRecPtr last_recvpos = InvalidXLogRecPtr;
1720  static XLogRecPtr last_writepos = InvalidXLogRecPtr;
1721  static XLogRecPtr last_flushpos = InvalidXLogRecPtr;
1722 
1723  XLogRecPtr writepos;
1724  XLogRecPtr flushpos;
1725  TimestampTz now;
1726  bool have_pending_txes;
1727 
1728  /*
1729  * If the user doesn't want status to be reported to the publisher, be
1730  * sure to exit before doing anything at all.
1731  */
1732  if (!force && wal_receiver_status_interval <= 0)
1733  return;
1734 
1735  /* It's legal to not pass a recvpos */
1736  if (recvpos < last_recvpos)
1737  recvpos = last_recvpos;
1738 
1739  get_flush_position(&writepos, &flushpos, &have_pending_txes);
1740 
1741  /*
1742  * No outstanding transactions to flush, we can report the latest received
1743  * position. This is important for synchronous replication.
1744  */
1745  if (!have_pending_txes)
1746  flushpos = writepos = recvpos;
1747 
1748  if (writepos < last_writepos)
1749  writepos = last_writepos;
1750 
1751  if (flushpos < last_flushpos)
1752  flushpos = last_flushpos;
1753 
1754  now = GetCurrentTimestamp();
1755 
1756  /* if we've already reported everything we're good */
1757  if (!force &&
1758  writepos == last_writepos &&
1759  flushpos == last_flushpos &&
1760  !TimestampDifferenceExceeds(send_time, now,
1762  return;
1763  send_time = now;
1764 
1765  if (!reply_message)
1766  {
1767  MemoryContext oldctx = MemoryContextSwitchTo(ApplyContext);
1768 
1769  reply_message = makeStringInfo();
1770  MemoryContextSwitchTo(oldctx);
1771  }
1772  else
1773  resetStringInfo(reply_message);
1774 
1775  pq_sendbyte(reply_message, 'r');
1776  pq_sendint64(reply_message, recvpos); /* write */
1777  pq_sendint64(reply_message, flushpos); /* flush */
1778  pq_sendint64(reply_message, writepos); /* apply */
1779  pq_sendint64(reply_message, now); /* sendTime */
1780  pq_sendbyte(reply_message, requestReply); /* replyRequested */
1781 
1782  elog(DEBUG2, "sending feedback (force %d) to recv %X/%X, write %X/%X, flush %X/%X",
1783  force,
1784  (uint32) (recvpos >> 32), (uint32) recvpos,
1785  (uint32) (writepos >> 32), (uint32) writepos,
1786  (uint32) (flushpos >> 32), (uint32) flushpos
1787  );
1788 
1789  walrcv_send(wrconn, reply_message->data, reply_message->len);
1790 
1791  if (recvpos > last_recvpos)
1792  last_recvpos = recvpos;
1793  if (writepos > last_writepos)
1794  last_writepos = writepos;
1795  if (flushpos > last_flushpos)
1796  last_flushpos = flushpos;
1797 }
1798 
1799 /*
1800  * Reread subscription info if needed. Most changes will be exit.
1801  */
1802 static void
1804 {
1805  MemoryContext oldctx;
1807  bool started_tx = false;
1808 
1809  /* When cache state is valid there is nothing to do here. */
1810  if (MySubscriptionValid)
1811  return;
1812 
1813  /* This function might be called inside or outside of transaction. */
1814  if (!IsTransactionState())
1815  {
1817  started_tx = true;
1818  }
1819 
1820  /* Ensure allocations in permanent context. */
1821  oldctx = MemoryContextSwitchTo(ApplyContext);
1822 
1823  newsub = GetSubscription(MyLogicalRepWorker->subid, true);
1824 
1825  /*
1826  * Exit if the subscription was removed. This normally should not happen
1827  * as the worker gets killed during DROP SUBSCRIPTION.
1828  */
1829  if (!newsub)
1830  {
1831  ereport(LOG,
1832  (errmsg("logical replication apply worker for subscription \"%s\" will "
1833  "stop because the subscription was removed",
1834  MySubscription->name)));
1835 
1836  proc_exit(0);
1837  }
1838 
1839  /*
1840  * Exit if the subscription was disabled. This normally should not happen
1841  * as the worker gets killed during ALTER SUBSCRIPTION ... DISABLE.
1842  */
1843  if (!newsub->enabled)
1844  {
1845  ereport(LOG,
1846  (errmsg("logical replication apply worker for subscription \"%s\" will "
1847  "stop because the subscription was disabled",
1848  MySubscription->name)));
1849 
1850  proc_exit(0);
1851  }
1852 
1853  /*
1854  * Exit if connection string was changed. The launcher will start new
1855  * worker.
1856  */
1857  if (strcmp(newsub->conninfo, MySubscription->conninfo) != 0)
1858  {
1859  ereport(LOG,
1860  (errmsg("logical replication apply worker for subscription \"%s\" will "
1861  "restart because the connection information was changed",
1862  MySubscription->name)));
1863 
1864  proc_exit(0);
1865  }
1866 
1867  /*
1868  * Exit if subscription name was changed (it's used for
1869  * fallback_application_name). The launcher will start new worker.
1870  */
1871  if (strcmp(newsub->name, MySubscription->name) != 0)
1872  {
1873  ereport(LOG,
1874  (errmsg("logical replication apply worker for subscription \"%s\" will "
1875  "restart because subscription was renamed",
1876  MySubscription->name)));
1877 
1878  proc_exit(0);
1879  }
1880 
1881  /* !slotname should never happen when enabled is true. */
1882  Assert(newsub->slotname);
1883 
1884  /*
1885  * We need to make new connection to new slot if slot name has changed so
1886  * exit here as well if that's the case.
1887  */
1888  if (strcmp(newsub->slotname, MySubscription->slotname) != 0)
1889  {
1890  ereport(LOG,
1891  (errmsg("logical replication apply worker for subscription \"%s\" will "
1892  "restart because the replication slot name was changed",
1893  MySubscription->name)));
1894 
1895  proc_exit(0);
1896  }
1897 
1898  /*
1899  * Exit if publication list was changed. The launcher will start new
1900  * worker.
1901  */
1902  if (!equal(newsub->publications, MySubscription->publications))
1903  {
1904  ereport(LOG,
1905  (errmsg("logical replication apply worker for subscription \"%s\" will "
1906  "restart because subscription's publications were changed",
1907  MySubscription->name)));
1908 
1909  proc_exit(0);
1910  }
1911 
1912  /* Check for other changes that should never happen too. */
1913  if (newsub->dbid != MySubscription->dbid)
1914  {
1915  elog(ERROR, "subscription %u changed unexpectedly",
1917  }
1918 
1919  /* Clean old subscription info and switch to new one. */
1920  FreeSubscription(MySubscription);
1921  MySubscription = newsub;
1922 
1923  MemoryContextSwitchTo(oldctx);
1924 
1925  /* Change synchronous commit according to the user's wishes */
1926  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1928 
1929  if (started_tx)
1931 
1932  MySubscriptionValid = true;
1933 }
1934 
1935 /*
1936  * Callback from subscription syscache invalidation.
1937  */
1938 static void
1939 subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
1940 {
1941  MySubscriptionValid = false;
1942 }
1943 
1944 /* Logical Replication Apply worker entry point */
1945 void
1947 {
1948  int worker_slot = DatumGetInt32(main_arg);
1949  MemoryContext oldctx;
1950  char originname[NAMEDATALEN];
1951  XLogRecPtr origin_startpos;
1952  char *myslotname;
1954 
1955  /* Attach to slot */
1956  logicalrep_worker_attach(worker_slot);
1957 
1958  /* Setup signal handling */
1960  pqsignal(SIGTERM, die);
1962 
1963  /*
1964  * We don't currently need any ResourceOwner in a walreceiver process, but
1965  * if we did, we could call CreateAuxProcessResourceOwner here.
1966  */
1967 
1968  /* Initialise stats to a sanish value */
1971 
1972  /* Load the libpq-specific functions */
1973  load_file("libpqwalreceiver", false);
1974 
1975  /* Run as replica session replication role. */
1976  SetConfigOption("session_replication_role", "replica",
1978 
1979  /* Connect to our database. */
1982  0);
1983 
1984  /* Load the subscription into persistent memory context. */
1985  ApplyContext = AllocSetContextCreate(TopMemoryContext,
1986  "ApplyContext",
1989  oldctx = MemoryContextSwitchTo(ApplyContext);
1990 
1991  MySubscription = GetSubscription(MyLogicalRepWorker->subid, true);
1992  if (!MySubscription)
1993  {
1994  ereport(LOG,
1995  (errmsg("logical replication apply worker for subscription %u will not "
1996  "start because the subscription was removed during startup",
1998  proc_exit(0);
1999  }
2000 
2001  MySubscriptionValid = true;
2002  MemoryContextSwitchTo(oldctx);
2003 
2004  if (!MySubscription->enabled)
2005  {
2006  ereport(LOG,
2007  (errmsg("logical replication apply worker for subscription \"%s\" will not "
2008  "start because the subscription was disabled during startup",
2009  MySubscription->name)));
2010 
2011  proc_exit(0);
2012  }
2013 
2014  /* Setup synchronous commit according to the user's wishes */
2015  SetConfigOption("synchronous_commit", MySubscription->synccommit,
2017 
2018  /* Keep us informed about subscription changes. */
2021  (Datum) 0);
2022 
2023  if (am_tablesync_worker())
2024  ereport(LOG,
2025  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
2026  MySubscription->name, get_rel_name(MyLogicalRepWorker->relid))));
2027  else
2028  ereport(LOG,
2029  (errmsg("logical replication apply worker for subscription \"%s\" has started",
2030  MySubscription->name)));
2031 
2033 
2034  /* Connect to the origin and start the replication. */
2035  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
2036  MySubscription->conninfo);
2037 
2038  if (am_tablesync_worker())
2039  {
2040  char *syncslotname;
2041 
2042  /* This is table synchronization worker, call initial sync. */
2043  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
2044 
2045  /* The slot name needs to be allocated in permanent memory context. */
2046  oldctx = MemoryContextSwitchTo(ApplyContext);
2047  myslotname = pstrdup(syncslotname);
2048  MemoryContextSwitchTo(oldctx);
2049 
2050  pfree(syncslotname);
2051  }
2052  else
2053  {
2054  /* This is main apply worker */
2055  RepOriginId originid;
2056  TimeLineID startpointTLI;
2057  char *err;
2058 
2059  myslotname = MySubscription->slotname;
2060 
2061  /*
2062  * This shouldn't happen if the subscription is enabled, but guard
2063  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
2064  * crash if slot is NULL.)
2065  */
2066  if (!myslotname)
2067  ereport(ERROR,
2068  (errmsg("subscription has no replication slot set")));
2069 
2070  /* Setup replication origin tracking. */
2072  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
2073  originid = replorigin_by_name(originname, true);
2074  if (!OidIsValid(originid))
2075  originid = replorigin_create(originname);
2076  replorigin_session_setup(originid);
2077  replorigin_session_origin = originid;
2078  origin_startpos = replorigin_session_get_progress(false);
2080 
2081  wrconn = walrcv_connect(MySubscription->conninfo, true, MySubscription->name,
2082  &err);
2083  if (wrconn == NULL)
2084  ereport(ERROR,
2085  (errmsg("could not connect to the publisher: %s", err)));
2086 
2087  /*
2088  * We don't really use the output identify_system for anything but it
2089  * does some initializations on the upstream so let's still call it.
2090  */
2091  (void) walrcv_identify_system(wrconn, &startpointTLI);
2092 
2093  }
2094 
2095  /*
2096  * Setup callback for syscache so that we know when something changes in
2097  * the subscription relation state.
2098  */
2101  (Datum) 0);
2102 
2103  /* Build logical replication streaming options. */
2104  options.logical = true;
2105  options.startpoint = origin_startpos;
2106  options.slotname = myslotname;
2107  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
2108  options.proto.logical.publication_names = MySubscription->publications;
2109 
2110  /* Start normal logical streaming replication. */
2111  walrcv_startstreaming(wrconn, &options);
2112 
2113  /* Run the main loop. */
2114  LogicalRepApplyLoop(origin_startpos);
2115 
2116  proc_exit(0);
2117 }
2118 
2119 /*
2120  * Is current process a logical replication worker?
2121  */
2122 bool
2124 {
2125  return MyLogicalRepWorker != NULL;
2126 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:77
void ExecSimpleRelationInsert(EState *estate, TupleTableSlot *slot)
Subscription * MySubscription
Definition: worker.c:107
static void apply_handle_type(StringInfo s)
Definition: worker.c:579
static TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: tuptable.h:475
static bool should_apply_changes_for_rel(LogicalRepRelMapEntry *rel)
Definition: worker.c:153
#define NIL
Definition: pg_list.h:65
void ExecInitRangeTable(EState *estate, List *rangeTable)
Definition: execUtils.c:765
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
static EState * create_estate_for_relation(LogicalRepRelMapEntry *rel)
Definition: worker.c:198
static Oid GetRelationIdentityOrPK(Relation rel)
Definition: worker.c:593
void InitResultRelInfo(ResultRelInfo *resultRelInfo, Relation resultRelationDesc, Index resultRelationIndex, Relation partition_root, int instrument_options)
Definition: execMain.c:1277
static void send_feedback(XLogRecPtr recvpos, bool force, bool requestReply)
Definition: worker.c:1714
Relation ri_RelationDesc
Definition: execnodes.h:413
WalReceiverConn * wrconn
Definition: worker.c:105
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define AllocSetContextCreate
Definition: memutils.h:170
#define walrcv_endstreaming(conn, next_tli)
Definition: walreceiver.h:412
#define DEBUG1
Definition: elog.h:25
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
dlist_node * cur
Definition: ilist.h:180
static void store_flush_position(XLogRecPtr remote_lsn)
Definition: worker.c:1449
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1801
uint32 TimeLineID
Definition: xlogdefs.h:52
LogicalRepRelId logicalrep_read_delete(StringInfo in, LogicalRepTupleData *oldtup)
Definition: proto.c:276
void AcceptInvalidationMessages(void)
Definition: inval.c:681
CommandId es_output_cid
Definition: execnodes.h:522
static XLogRecPtr remote_final_lsn
Definition: worker.c:111
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:4716
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
static void apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate, TupleTableSlot *remoteslot, LogicalRepRelation *remoterel)
Definition: worker.c:929
static void apply_handle_insert(StringInfo s)
Definition: worker.c:610
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
static dlist_head lsn_mapping
Definition: worker.c:93
#define DatumGetInt32(X)
Definition: postgres.h:472
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3033
#define RelationGetDescr(relation)
Definition: rel.h:482
void process_syncing_tables(XLogRecPtr current_lsn)
Definition: tablesync.c:529
static void apply_handle_update_internal(ResultRelInfo *relinfo, EState *estate, TupleTableSlot *remoteslot, LogicalRepTupleData *newtup, LogicalRepRelMapEntry *relmapentry)
Definition: worker.c:806
dlist_node node
Definition: worker.c:88
#define write(a, b, c)
Definition: win32.h:14
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:3132
int64 timestamp
void logicalrep_read_begin(StringInfo in, LogicalRepBeginData *begin_data)
Definition: proto.c:60
int64 TimestampTz
Definition: timestamp.h:39
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1174
LogicalRepRelMapEntry * rel
Definition: worker.c:97
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define walrcv_identify_system(conn, primary_tli)
Definition: walreceiver.h:404
List * logicalrep_read_truncate(StringInfo in, bool *cascade, bool *restart_seqs)
Definition: proto.c:325
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
struct PartitionRoutingInfo * ri_PartitionInfo
Definition: execnodes.h:490
char * pstrdup(const char *in)
Definition: mcxt.c:1186
void CommitTransactionCommand(void)
Definition: xact.c:2919
static void UpdateWorkerStats(XLogRecPtr last_lsn, TimestampTz send_time, bool reply)
Definition: worker.c:1468
XLogRecPtr XactLastCommitEnd
Definition: xlog.c:364
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
#define walrcv_receive(conn, buffer, wait_fd)
Definition: walreceiver.h:414
LogicalRepRelation * logicalrep_read_rel(StringInfo in)
Definition: proto.c:375
Expr * expression_planner(Expr *expr)
Definition: planner.c:6156
int maplen
Definition: attmap.h:37
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
TimestampTz last_send_time
uint16 RepOriginId
Definition: xlogdefs.h:58
XLogRecPtr last_lsn
#define walrcv_startstreaming(conn, options)
Definition: walreceiver.h:410
union WalRcvStreamOptions::@104 proto
void proc_exit(int code)
Definition: ipc.c:104
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1186
XLogRecPtr remote_end
Definition: worker.c:90
int errcode(int sqlerrcode)
Definition: elog.c:610
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
CmdType operation
Definition: execnodes.h:1166
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:399
Datum * tts_values
Definition: tuptable.h:126
#define WL_SOCKET_READABLE
Definition: latch.h:125
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8424
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
#define RelationIsLogicallyLogged(relation)
Definition: rel.h:635
EState * state
Definition: execnodes.h:947
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1051
List * es_range_table
Definition: execnodes.h:510
#define LOG
Definition: elog.h:26
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
struct SlotErrCallbackArg SlotErrCallbackArg
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:816
bool IsLogicalWorker(void)
Definition: worker.c:2123
void(* callback)(void *arg)
Definition: elog.h:229
int wal_receiver_status_interval
Definition: walreceiver.c:88
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1673
struct ErrorContextCallback * previous
Definition: elog.h:228
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:306
#define OidIsValid(objectId)
Definition: c.h:651
bool RelationFindReplTupleByIndex(Relation rel, Oid idxoid, LockTupleMode lockmode, TupleTableSlot *searchslot, TupleTableSlot *outslot)
static int fd(const char *x, int i)
Definition: preproc-init.c:105
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:209
void ResetLatch(Latch *latch)
Definition: latch.c:540
int wal_receiver_timeout
Definition: walreceiver.c:89
static void get_flush_position(XLogRecPtr *write, XLogRecPtr *flush, bool *have_pending_txes)
Definition: worker.c:1405
Definition: attmap.h:34
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:151
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:2932
LogicalRepRelId logicalrep_read_insert(StringInfo in, LogicalRepTupleData *newtup)
Definition: proto.c:159
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
ErrorContextCallback * error_context_stack
Definition: elog.c:92
static void slot_fill_defaults(LogicalRepRelMapEntry *rel, EState *estate, TupleTableSlot *slot)
Definition: worker.c:236
#define list_make1(x1)
Definition: pg_list.h:227
#define NAMEDATALEN
void FreeExecutorState(EState *estate)
Definition: execUtils.c:191
Subscription * GetSubscription(Oid subid, bool missing_ok)
#define GetPerTupleExprContext(estate)
Definition: executor.h:507
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:172
static StringInfoData reply_message
Definition: walreceiver.c:124
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1939
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
TupleConversionMap * pi_RootToPartitionMap
Definition: execPartition.h:37
void pfree(void *pointer)
Definition: mcxt.c:1056
#define dlist_tail_element(type, membername, lhead)
Definition: ilist.h:496
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define ERROR
Definition: elog.h:43
PlanState ps
Definition: execnodes.h:1165
static bool ensure_transaction(void)
Definition: worker.c:169
void fill_extraUpdatedCols(RangeTblEntry *target_rte, TupleDesc tupdesc)
Definition: analyze.c:2368
LogicalRepRelation remoterel
#define NAPTIME_PER_CYCLE
Definition: worker.c:84
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
bool in_remote_transaction
Definition: worker.c:110
void ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged, DropBehavior behavior, bool restart_seqs)
Definition: tablecmds.c:1671
void logicalrep_read_typ(StringInfo in, LogicalRepTyp *ltyp)
Definition: proto.c:427
Definition: guc.h:75
static void slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel, char **values)
Definition: worker.c:327
XLogRecPtr reply_lsn
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
static bool am_tablesync_worker(void)
static void apply_handle_delete(StringInfo s)
Definition: worker.c:867
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define DEBUG2
Definition: elog.h:24
TupleConversionMap * convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc)
Definition: tupconvert.c:102
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:155
char * c
void logicalrep_worker_attach(int slot)
Definition: launcher.c:625
#define NoLock
Definition: lockdefs.h:34
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7685
static char * buf
Definition: pg_test_fsync.c:67
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:735
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:156
bool changed[MaxTupleAttributeNumber]
Definition: logicalproto.h:36
bool * tts_isnull
Definition: tuptable.h:128
void ExecSimpleRelationDelete(EState *estate, EPQState *epqstate, TupleTableSlot *searchslot)
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:290
ResultRelInfo * es_result_relations
Definition: execnodes.h:525
#define RowExclusiveLock
Definition: lockdefs.h:38
#define SIGHUP
Definition: win32_port.h:153
#define RelationGetRelationName(relation)
Definition: rel.h:490
XLogRecPtr startpoint
Definition: walreceiver.h:168
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:390
unsigned int uint32
Definition: c.h:374
int pgsocket
Definition: port.h:31
List * publications
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void apply_handle_begin(StringInfo s)
Definition: worker.c:479
RepOriginId replorigin_create(char *roname)
Definition: origin.c:240
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:237
Oid get_atttype(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:911
PartitionTupleRouting * ExecSetupPartitionTupleRouting(EState *estate, ModifyTableState *mtstate, Relation rel)
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2751
AttrMap * attrMap
Definition: tupconvert.h:27
MemoryContext TopMemoryContext
Definition: mcxt.c:44
EState * CreateExecutorState(void)
Definition: execUtils.c:89
Definition: guc.h:72
static void check_relation_updatable(LogicalRepRelMapEntry *rel)
Definition: worker.c:688
List * lappend(List *list, void *datum)
Definition: list.c:321
MemoryContext ApplyContext
Definition: worker.c:103
static char ** options
XLogRecPtr final_lsn
Definition: logicalproto.h:67
#define DLIST_STATIC_INIT(name)
Definition: ilist.h:248
static void apply_handle_commit(StringInfo s)
Definition: worker.c:498
void EvalPlanQualInit(EPQState *epqstate, EState *parentestate, Plan *subplan, List *auxrowmarks, int epqParam)
Definition: execMain.c:2486
static void apply_handle_tuple_routing(ResultRelInfo *relinfo, EState *estate, TupleTableSlot *remoteslot, LogicalRepTupleData *newtup, LogicalRepRelMapEntry *relmapentry, CmdType operation)
Definition: worker.c:1003
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
Node * build_column_default(Relation rel, int attrno)
List * es_tupleTable
Definition: execnodes.h:557
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1426
void ExecResetTupleTable(List *tupleTable, bool shouldFree)
Definition: execTuples.c:1161
static void apply_handle_update(StringInfo s)
Definition: worker.c:722
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1006
#define PGINVALID_SOCKET
Definition: port.h:33
static bool FindReplTupleInLocalRel(EState *estate, Relation localrel, LogicalRepRelation *remoterel, TupleTableSlot *remoteslot, TupleTableSlot **localslot)
Definition: worker.c:974
int es_num_result_relations
Definition: execnodes.h:526
List * ri_PartitionCheck
Definition: execnodes.h:481
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5734
Plan * plan
Definition: execnodes.h:945
static void apply_handle_relation(StringInfo s)
Definition: worker.c:564
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
LogicalRepRelMapEntry * logicalrep_partition_open(LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
Definition: relation.c:582
int16 attnum
Definition: pg_attribute.h:79
#define ereport(elevel,...)
Definition: elog.h:144
Bitmapset * updatedCols
Definition: parsenodes.h:1123
XLogRecPtr local_end
Definition: worker.c:89
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4432
static void apply_dispatch(StringInfo s)
Definition: worker.c:1342
static void maybe_reread_subscription(void)
Definition: worker.c:1803
#define makeNode(_type_)
Definition: nodes.h:577
TimestampTz last_recv_time
static MemoryContext ApplyMessageContext
Definition: worker.c:102
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
TupleTableSlot * execute_attr_map_slot(AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
Definition: tupconvert.c:177
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
RepOriginId replorigin_session_origin
Definition: origin.c:154
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:593
static void apply_handle_origin(StringInfo s)
Definition: worker.c:542
void StartTransactionCommand(void)
Definition: xact.c:2818
AttrNumber * attnums
Definition: attmap.h:36
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static void slot_store_error_callback(void *arg)
Definition: worker.c:292
LogicalRepRelId logicalrep_read_update(StringInfo in, bool *has_oldtuple, LogicalRepTupleData *oldtup, LogicalRepTupleData *newtup)
Definition: proto.c:210
bool IsTransactionState(void)
Definition: xact.c:356
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define walrcv_send(conn, buffer, nbytes)
Definition: walreceiver.h:416
void logicalrep_read_commit(StringInfo in, LogicalRepCommitData *commit_data)
Definition: proto.c:95
int WalWriterDelay
Definition: walwriter.c:70
bool MySubscriptionValid
Definition: worker.c:108
char * values[MaxTupleAttributeNumber]
Definition: logicalproto.h:34
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:512
void FreeSubscription(Subscription *sub)
char * logicalrep_typmap_gettypname(Oid remoteid)
Definition: relation.c:453
RTEKind rtekind
Definition: parsenodes.h:976
static Datum values[MAXATTR]
Definition: bootstrap.c:167
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4452
void SetCurrentStatementStartTimestamp(void)
Definition: xact.c:818
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1484
bool RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode, TupleTableSlot *searchslot, TupleTableSlot *outslot)
#define elog(elevel,...)
Definition: elog.h:214
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:26
int i
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
#define errcontext
Definition: elog.h:185
bool ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool emitError)
Definition: execMain.c:1783
void * arg
struct Latch * MyLatch
Definition: globals.c:54
struct FlushPosition FlushPosition
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:123
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:746
ResultRelInfo * ExecFindPartition(ModifyTableState *mtstate, ResultRelInfo *rootResultRelInfo, PartitionTupleRouting *proute, TupleTableSlot *slot, EState *estate)
XLogRecPtr commit_lsn
Definition: logicalproto.h:74
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
static color newsub(struct colormap *cm, color co)
Definition: regc_color.c:389
static void slot_modify_cstrings(TupleTableSlot *slot, TupleTableSlot *srcslot, LogicalRepRelMapEntry *rel, char **values, bool *replaces)
Definition: worker.c:401
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1840
#define snprintf
Definition: port.h:193
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1648
#define WL_LATCH_SET
Definition: latch.h:124
#define RelationGetRelid(relation)
Definition: rel.h:456
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
CmdType
Definition: nodes.h:672
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:226
#define die(msg)
Definition: pg_test_fsync.c:96
Oid RelationGetPrimaryKeyIndex(Relation relation)
Definition: relcache.c:4695
void ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate, TupleTableSlot *searchslot, TupleTableSlot *slot)
TimestampTz committime
Definition: logicalproto.h:76
void logicalrep_typmap_update(LogicalRepTyp *remotetyp)
Definition: relation.c:419
void ApplyWorkerMain(Datum main_arg)
Definition: worker.c:1946
uint32 LogicalRepRelId
Definition: logicalproto.h:39
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522
#define lfirst_oid(lc)
Definition: pg_list.h:192
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
TimestampTz reply_time
#define EvalPlanQualSetSlot(epqstate, slot)
Definition: executor.h:215
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:256
TupleTableSlot * pi_PartitionTupleSlot
Definition: execPartition.h:49
static void apply_handle_insert_internal(ResultRelInfo *relinfo, EState *estate, TupleTableSlot *remoteslot)
Definition: worker.c:671
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5763
void pgstat_report_stat(bool force)
Definition: pgstat.c:839
static void apply_handle_truncate(StringInfo s)
Definition: worker.c:1234
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:396
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:527