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-2018, 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 "miscadmin.h"
27 #include "pgstat.h"
28 #include "funcapi.h"
29 
30 #include "access/xact.h"
31 #include "access/xlog_internal.h"
32 
33 #include "catalog/catalog.h"
34 #include "catalog/namespace.h"
37 
38 #include "commands/tablecmds.h"
39 #include "commands/trigger.h"
40 
41 #include "executor/executor.h"
43 
44 #include "libpq/pqformat.h"
45 #include "libpq/pqsignal.h"
46 
47 #include "mb/pg_wchar.h"
48 
49 #include "nodes/makefuncs.h"
50 
51 #include "optimizer/planner.h"
52 
53 #include "parser/parse_relation.h"
54 
55 #include "postmaster/bgworker.h"
56 #include "postmaster/postmaster.h"
57 #include "postmaster/walwriter.h"
58 
59 #include "replication/decode.h"
60 #include "replication/logical.h"
65 #include "replication/origin.h"
66 #include "replication/snapbuild.h"
69 
70 #include "rewrite/rewriteHandler.h"
71 
72 #include "storage/bufmgr.h"
73 #include "storage/ipc.h"
74 #include "storage/lmgr.h"
75 #include "storage/proc.h"
76 #include "storage/procarray.h"
77 
78 #include "tcop/tcopprot.h"
79 
80 #include "utils/builtins.h"
81 #include "utils/catcache.h"
82 #include "utils/datum.h"
83 #include "utils/fmgroids.h"
84 #include "utils/guc.h"
85 #include "utils/inval.h"
86 #include "utils/lsyscache.h"
87 #include "utils/memutils.h"
88 #include "utils/rel.h"
89 #include "utils/timeout.h"
90 #include "utils/tqual.h"
91 #include "utils/syscache.h"
92 
93 #define NAPTIME_PER_CYCLE 1000 /* max sleep time between cycles (1s) */
94 
95 typedef struct FlushPosition
96 {
100 } FlushPosition;
101 
103 
104 typedef struct SlotErrCallbackArg
105 {
110 
113 
115 
117 bool MySubscriptionValid = false;
118 
121 
122 static void send_feedback(XLogRecPtr recvpos, bool force, bool requestReply);
123 
124 static void store_flush_position(XLogRecPtr remote_lsn);
125 
126 static void maybe_reread_subscription(void);
127 
128 /* Flags set by signal handlers */
129 static volatile sig_atomic_t got_SIGHUP = false;
130 
131 /*
132  * Should this worker apply changes for given relation.
133  *
134  * This is mainly needed for initial relation data sync as that runs in
135  * separate worker process running in parallel and we need some way to skip
136  * changes coming to the main apply worker during the sync of a table.
137  *
138  * Note we need to do smaller or equals comparison for SYNCDONE state because
139  * it might hold position of end of initial slot consistent point WAL
140  * record + 1 (ie start of next record) and next record can be COMMIT of
141  * transaction we are now processing (which is what we set remote_final_lsn
142  * to in apply_handle_begin).
143  */
144 static bool
146 {
147  if (am_tablesync_worker())
148  return MyLogicalRepWorker->relid == rel->localreloid;
149  else
150  return (rel->state == SUBREL_STATE_READY ||
151  (rel->state == SUBREL_STATE_SYNCDONE &&
152  rel->statelsn <= remote_final_lsn));
153 }
154 
155 /*
156  * Make sure that we started local transaction.
157  *
158  * Also switches to ApplyMessageContext as necessary.
159  */
160 static bool
162 {
163  if (IsTransactionState())
164  {
166 
167  if (CurrentMemoryContext != ApplyMessageContext)
168  MemoryContextSwitchTo(ApplyMessageContext);
169 
170  return false;
171  }
172 
175 
177 
178  MemoryContextSwitchTo(ApplyMessageContext);
179  return true;
180 }
181 
182 
183 /*
184  * Executor state preparation for evaluation of constraint expressions,
185  * indexes and triggers.
186  *
187  * This is based on similar code in copy.c
188  */
189 static EState *
191 {
192  EState *estate;
193  ResultRelInfo *resultRelInfo;
194  RangeTblEntry *rte;
195 
196  estate = CreateExecutorState();
197 
198  rte = makeNode(RangeTblEntry);
199  rte->rtekind = RTE_RELATION;
200  rte->relid = RelationGetRelid(rel->localrel);
201  rte->relkind = rel->localrel->rd_rel->relkind;
202  estate->es_range_table = list_make1(rte);
203 
204  resultRelInfo = makeNode(ResultRelInfo);
205  InitResultRelInfo(resultRelInfo, rel->localrel, 1, NULL, 0);
206 
207  estate->es_result_relations = resultRelInfo;
208  estate->es_num_result_relations = 1;
209  estate->es_result_relation_info = resultRelInfo;
210 
211  estate->es_output_cid = GetCurrentCommandId(true);
212 
213  /* Triggers might need a slot */
214  if (resultRelInfo->ri_TrigDesc)
215  estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate, NULL);
216 
217  /* Prepare to catch AFTER triggers. */
219 
220  return estate;
221 }
222 
223 /*
224  * Executes default values for columns for which we can't map to remote
225  * relation columns.
226  *
227  * This allows us to support tables which have more columns on the downstream
228  * than on the upstream.
229  */
230 static void
232  TupleTableSlot *slot)
233 {
234  TupleDesc desc = RelationGetDescr(rel->localrel);
235  int num_phys_attrs = desc->natts;
236  int i;
237  int attnum,
238  num_defaults = 0;
239  int *defmap;
240  ExprState **defexprs;
241  ExprContext *econtext;
242 
243  econtext = GetPerTupleExprContext(estate);
244 
245  /* We got all the data via replication, no need to evaluate anything. */
246  if (num_phys_attrs == rel->remoterel.natts)
247  return;
248 
249  defmap = (int *) palloc(num_phys_attrs * sizeof(int));
250  defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
251 
252  for (attnum = 0; attnum < num_phys_attrs; attnum++)
253  {
254  Expr *defexpr;
255 
256  if (TupleDescAttr(desc, attnum)->attisdropped)
257  continue;
258 
259  if (rel->attrmap[attnum] >= 0)
260  continue;
261 
262  defexpr = (Expr *) build_column_default(rel->localrel, attnum + 1);
263 
264  if (defexpr != NULL)
265  {
266  /* Run the expression through planner */
267  defexpr = expression_planner(defexpr);
268 
269  /* Initialize executable expression in copycontext */
270  defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
271  defmap[num_defaults] = attnum;
272  num_defaults++;
273  }
274 
275  }
276 
277  for (i = 0; i < num_defaults; i++)
278  slot->tts_values[defmap[i]] =
279  ExecEvalExpr(defexprs[i], econtext, &slot->tts_isnull[defmap[i]]);
280 }
281 
282 /*
283  * Error callback to give more context info about type conversion failure.
284  */
285 static void
287 {
288  SlotErrCallbackArg *errarg = (SlotErrCallbackArg *) arg;
290  char *remotetypname;
291  Oid remotetypoid,
292  localtypoid;
293 
294  /* Nothing to do if remote attribute number is not set */
295  if (errarg->remote_attnum < 0)
296  return;
297 
298  rel = errarg->rel;
299  remotetypoid = rel->remoterel.atttyps[errarg->remote_attnum];
300 
301  /* Fetch remote type name from the LogicalRepTypMap cache */
302  remotetypname = logicalrep_typmap_gettypname(remotetypoid);
303 
304  /* Fetch local type OID from the local sys cache */
305  localtypoid = get_atttype(rel->localreloid, errarg->local_attnum + 1);
306 
307  errcontext("processing remote data for replication target relation \"%s.%s\" column \"%s\", "
308  "remote type %s, local type %s",
309  rel->remoterel.nspname, rel->remoterel.relname,
310  rel->remoterel.attnames[errarg->remote_attnum],
311  remotetypname,
312  format_type_be(localtypoid));
313 }
314 
315 /*
316  * Store data in C string form into slot.
317  * This is similar to BuildTupleFromCStrings but TupleTableSlot fits our
318  * use better.
319  */
320 static void
322  char **values)
323 {
324  int natts = slot->tts_tupleDescriptor->natts;
325  int i;
326  SlotErrCallbackArg errarg;
327  ErrorContextCallback errcallback;
328 
329  ExecClearTuple(slot);
330 
331  /* Push callback + info on the error context stack */
332  errarg.rel = rel;
333  errarg.local_attnum = -1;
334  errarg.remote_attnum = -1;
335  errcallback.callback = slot_store_error_callback;
336  errcallback.arg = (void *) &errarg;
337  errcallback.previous = error_context_stack;
338  error_context_stack = &errcallback;
339 
340  /* Call the "in" function for each non-dropped attribute */
341  for (i = 0; i < natts; i++)
342  {
344  int remoteattnum = rel->attrmap[i];
345 
346  if (!att->attisdropped && remoteattnum >= 0 &&
347  values[remoteattnum] != NULL)
348  {
349  Oid typinput;
350  Oid typioparam;
351 
352  errarg.local_attnum = i;
353  errarg.remote_attnum = remoteattnum;
354 
355  getTypeInputInfo(att->atttypid, &typinput, &typioparam);
356  slot->tts_values[i] =
357  OidInputFunctionCall(typinput, values[remoteattnum],
358  typioparam, att->atttypmod);
359  slot->tts_isnull[i] = false;
360 
361  errarg.local_attnum = -1;
362  errarg.remote_attnum = -1;
363  }
364  else
365  {
366  /*
367  * We assign NULL to dropped attributes, NULL values, and missing
368  * values (missing values should be later filled using
369  * slot_fill_defaults).
370  */
371  slot->tts_values[i] = (Datum) 0;
372  slot->tts_isnull[i] = true;
373  }
374  }
375 
376  /* Pop the error context stack */
377  error_context_stack = errcallback.previous;
378 
379  ExecStoreVirtualTuple(slot);
380 }
381 
382 /*
383  * Modify slot with user data provided as C strings.
384  * This is somewhat similar to heap_modify_tuple but also calls the type
385  * input function on the user data as the input is the text representation
386  * of the types.
387  */
388 static void
390  char **values, bool *replaces)
391 {
392  int natts = slot->tts_tupleDescriptor->natts;
393  int i;
394  SlotErrCallbackArg errarg;
395  ErrorContextCallback errcallback;
396 
397  slot_getallattrs(slot);
398  ExecClearTuple(slot);
399 
400  /* Push callback + info on the error context stack */
401  errarg.rel = rel;
402  errarg.local_attnum = -1;
403  errarg.remote_attnum = -1;
404  errcallback.callback = slot_store_error_callback;
405  errcallback.arg = (void *) &errarg;
406  errcallback.previous = error_context_stack;
407  error_context_stack = &errcallback;
408 
409  /* Call the "in" function for each replaced attribute */
410  for (i = 0; i < natts; i++)
411  {
413  int remoteattnum = rel->attrmap[i];
414 
415  if (remoteattnum < 0)
416  continue;
417 
418  if (!replaces[remoteattnum])
419  continue;
420 
421  if (values[remoteattnum] != NULL)
422  {
423  Oid typinput;
424  Oid typioparam;
425 
426  errarg.local_attnum = i;
427  errarg.remote_attnum = remoteattnum;
428 
429  getTypeInputInfo(att->atttypid, &typinput, &typioparam);
430  slot->tts_values[i] =
431  OidInputFunctionCall(typinput, values[remoteattnum],
432  typioparam, att->atttypmod);
433  slot->tts_isnull[i] = false;
434 
435  errarg.local_attnum = -1;
436  errarg.remote_attnum = -1;
437  }
438  else
439  {
440  slot->tts_values[i] = (Datum) 0;
441  slot->tts_isnull[i] = true;
442  }
443  }
444 
445  /* Pop the error context stack */
446  error_context_stack = errcallback.previous;
447 
448  ExecStoreVirtualTuple(slot);
449 }
450 
451 /*
452  * Handle BEGIN message.
453  */
454 static void
456 {
457  LogicalRepBeginData begin_data;
458 
459  logicalrep_read_begin(s, &begin_data);
460 
461  remote_final_lsn = begin_data.final_lsn;
462 
463  in_remote_transaction = true;
464 
466 }
467 
468 /*
469  * Handle COMMIT message.
470  *
471  * TODO, support tracking of multiple origins
472  */
473 static void
475 {
476  LogicalRepCommitData commit_data;
477 
478  logicalrep_read_commit(s, &commit_data);
479 
480  Assert(commit_data.commit_lsn == remote_final_lsn);
481 
482  /* The synchronization worker runs in single transaction. */
484  {
485  /*
486  * Update origin state so we can restart streaming from correct
487  * position in case of crash.
488  */
491 
493  pgstat_report_stat(false);
494 
495  store_flush_position(commit_data.end_lsn);
496  }
497  else
498  {
499  /* Process any invalidation messages that might have accumulated. */
502  }
503 
504  in_remote_transaction = false;
505 
506  /* Process any tables that are being synchronized in parallel. */
507  process_syncing_tables(commit_data.end_lsn);
508 
510 }
511 
512 /*
513  * Handle ORIGIN message.
514  *
515  * TODO, support tracking of multiple origins
516  */
517 static void
519 {
520  /*
521  * ORIGIN message can only come inside remote transaction and before any
522  * actual writes.
523  */
524  if (!in_remote_transaction ||
526  ereport(ERROR,
527  (errcode(ERRCODE_PROTOCOL_VIOLATION),
528  errmsg("ORIGIN message sent out of order")));
529 }
530 
531 /*
532  * Handle RELATION message.
533  *
534  * Note we don't do validation against local schema here. The validation
535  * against local schema is postponed until first change for given relation
536  * comes as we only care about it when applying changes for it anyway and we
537  * do less locking this way.
538  */
539 static void
541 {
542  LogicalRepRelation *rel;
543 
544  rel = logicalrep_read_rel(s);
546 }
547 
548 /*
549  * Handle TYPE message.
550  *
551  * Note we don't do local mapping here, that's done when the type is
552  * actually used.
553  */
554 static void
556 {
557  LogicalRepTyp typ;
558 
559  logicalrep_read_typ(s, &typ);
561 }
562 
563 /*
564  * Get replica identity index or if it is not defined a primary key.
565  *
566  * If neither is defined, returns InvalidOid
567  */
568 static Oid
570 {
571  Oid idxoid;
572 
573  idxoid = RelationGetReplicaIndex(rel);
574 
575  if (!OidIsValid(idxoid))
576  idxoid = RelationGetPrimaryKeyIndex(rel);
577 
578  return idxoid;
579 }
580 
581 /*
582  * Handle INSERT message.
583  */
584 static void
586 {
588  LogicalRepTupleData newtup;
589  LogicalRepRelId relid;
590  EState *estate;
591  TupleTableSlot *remoteslot;
592  MemoryContext oldctx;
593 
595 
596  relid = logicalrep_read_insert(s, &newtup);
599  {
600  /*
601  * The relation can't become interesting in the middle of the
602  * transaction so it's safe to unlock it.
603  */
605  return;
606  }
607 
608  /* Initialize the executor state. */
609  estate = create_estate_for_relation(rel);
610  remoteslot = ExecInitExtraTupleSlot(estate,
611  RelationGetDescr(rel->localrel));
612 
613  /* Process and store remote tuple in the slot */
615  slot_store_cstrings(remoteslot, rel, newtup.values);
616  slot_fill_defaults(rel, estate, remoteslot);
617  MemoryContextSwitchTo(oldctx);
618 
621 
622  /* Do the insert. */
623  ExecSimpleRelationInsert(estate, remoteslot);
624 
625  /* Cleanup. */
628 
629  /* Handle queued AFTER triggers. */
630  AfterTriggerEndQuery(estate);
631 
632  ExecResetTupleTable(estate->es_tupleTable, false);
633  FreeExecutorState(estate);
634 
636 
638 }
639 
640 /*
641  * Check if the logical replication relation is updatable and throw
642  * appropriate error if it isn't.
643  */
644 static void
646 {
647  /* Updatable, no error. */
648  if (rel->updatable)
649  return;
650 
651  /*
652  * We are in error mode so it's fine this is somewhat slow. It's better to
653  * give user correct error.
654  */
656  {
657  ereport(ERROR,
658  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
659  errmsg("publisher did not send replica identity column "
660  "expected by the logical replication target relation \"%s.%s\"",
661  rel->remoterel.nspname, rel->remoterel.relname)));
662  }
663 
664  ereport(ERROR,
665  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
666  errmsg("logical replication target relation \"%s.%s\" has "
667  "neither REPLICA IDENTITY index nor PRIMARY "
668  "KEY and published relation does not have "
669  "REPLICA IDENTITY FULL",
670  rel->remoterel.nspname, rel->remoterel.relname)));
671 }
672 
673 /*
674  * Handle UPDATE message.
675  *
676  * TODO: FDW support
677  */
678 static void
680 {
682  LogicalRepRelId relid;
683  Oid idxoid;
684  EState *estate;
685  EPQState epqstate;
686  LogicalRepTupleData oldtup;
687  LogicalRepTupleData newtup;
688  bool has_oldtup;
689  TupleTableSlot *localslot;
690  TupleTableSlot *remoteslot;
691  bool found;
692  MemoryContext oldctx;
693 
695 
696  relid = logicalrep_read_update(s, &has_oldtup, &oldtup,
697  &newtup);
700  {
701  /*
702  * The relation can't become interesting in the middle of the
703  * transaction so it's safe to unlock it.
704  */
706  return;
707  }
708 
709  /* Check if we can do the update. */
711 
712  /* Initialize the executor state. */
713  estate = create_estate_for_relation(rel);
714  remoteslot = ExecInitExtraTupleSlot(estate,
715  RelationGetDescr(rel->localrel));
716  localslot = ExecInitExtraTupleSlot(estate,
717  RelationGetDescr(rel->localrel));
718  EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
719 
722 
723  /* Build the search tuple. */
725  slot_store_cstrings(remoteslot, rel,
726  has_oldtup ? oldtup.values : newtup.values);
727  MemoryContextSwitchTo(oldctx);
728 
729  /*
730  * Try to find tuple using either replica identity index, primary key or
731  * if needed, sequential scan.
732  */
733  idxoid = GetRelationIdentityOrPK(rel->localrel);
734  Assert(OidIsValid(idxoid) ||
735  (rel->remoterel.replident == REPLICA_IDENTITY_FULL && has_oldtup));
736 
737  if (OidIsValid(idxoid))
738  found = RelationFindReplTupleByIndex(rel->localrel, idxoid,
740  remoteslot, localslot);
741  else
743  remoteslot, localslot);
744 
745  ExecClearTuple(remoteslot);
746 
747  /*
748  * Tuple found.
749  *
750  * Note this will fail if there are other conflicting unique indexes.
751  */
752  if (found)
753  {
754  /* Process and store remote tuple in the slot */
756  ExecStoreTuple(localslot->tts_tuple, remoteslot, InvalidBuffer, false);
757  slot_modify_cstrings(remoteslot, rel, newtup.values, newtup.changed);
758  MemoryContextSwitchTo(oldctx);
759 
760  EvalPlanQualSetSlot(&epqstate, remoteslot);
761 
762  /* Do the actual update. */
763  ExecSimpleRelationUpdate(estate, &epqstate, localslot, remoteslot);
764  }
765  else
766  {
767  /*
768  * The tuple to be updated could not be found.
769  *
770  * TODO what to do here, change the log level to LOG perhaps?
771  */
772  elog(DEBUG1,
773  "logical replication did not find row for update "
774  "in replication target relation \"%s\"",
776  }
777 
778  /* Cleanup. */
781 
782  /* Handle queued AFTER triggers. */
783  AfterTriggerEndQuery(estate);
784 
785  EvalPlanQualEnd(&epqstate);
786  ExecResetTupleTable(estate->es_tupleTable, false);
787  FreeExecutorState(estate);
788 
790 
792 }
793 
794 /*
795  * Handle DELETE message.
796  *
797  * TODO: FDW support
798  */
799 static void
801 {
803  LogicalRepTupleData oldtup;
804  LogicalRepRelId relid;
805  Oid idxoid;
806  EState *estate;
807  EPQState epqstate;
808  TupleTableSlot *remoteslot;
809  TupleTableSlot *localslot;
810  bool found;
811  MemoryContext oldctx;
812 
814 
815  relid = logicalrep_read_delete(s, &oldtup);
818  {
819  /*
820  * The relation can't become interesting in the middle of the
821  * transaction so it's safe to unlock it.
822  */
824  return;
825  }
826 
827  /* Check if we can do the delete. */
829 
830  /* Initialize the executor state. */
831  estate = create_estate_for_relation(rel);
832  remoteslot = ExecInitExtraTupleSlot(estate,
833  RelationGetDescr(rel->localrel));
834  localslot = ExecInitExtraTupleSlot(estate,
835  RelationGetDescr(rel->localrel));
836  EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
837 
840 
841  /* Find the tuple using the replica identity index. */
843  slot_store_cstrings(remoteslot, rel, oldtup.values);
844  MemoryContextSwitchTo(oldctx);
845 
846  /*
847  * Try to find tuple using either replica identity index, primary key or
848  * if needed, sequential scan.
849  */
850  idxoid = GetRelationIdentityOrPK(rel->localrel);
851  Assert(OidIsValid(idxoid) ||
852  (rel->remoterel.replident == REPLICA_IDENTITY_FULL));
853 
854  if (OidIsValid(idxoid))
855  found = RelationFindReplTupleByIndex(rel->localrel, idxoid,
857  remoteslot, localslot);
858  else
860  remoteslot, localslot);
861  /* If found delete it. */
862  if (found)
863  {
864  EvalPlanQualSetSlot(&epqstate, localslot);
865 
866  /* Do the actual delete. */
867  ExecSimpleRelationDelete(estate, &epqstate, localslot);
868  }
869  else
870  {
871  /* The tuple to be deleted could not be found. */
872  ereport(DEBUG1,
873  (errmsg("logical replication could not find row for delete "
874  "in replication target relation \"%s\"",
876  }
877 
878  /* Cleanup. */
881 
882  /* Handle queued AFTER triggers. */
883  AfterTriggerEndQuery(estate);
884 
885  EvalPlanQualEnd(&epqstate);
886  ExecResetTupleTable(estate->es_tupleTable, false);
887  FreeExecutorState(estate);
888 
890 
892 }
893 
894 /*
895  * Handle TRUNCATE message.
896  *
897  * TODO: FDW support
898  */
899 static void
901 {
902  bool cascade = false;
903  bool restart_seqs = false;
904  List *remote_relids = NIL;
905  List *remote_rels = NIL;
906  List *rels = NIL;
907  List *relids = NIL;
908  List *relids_logged = NIL;
909  ListCell *lc;
910 
912 
913  remote_relids = logicalrep_read_truncate(s, &cascade, &restart_seqs);
914 
915  foreach(lc, remote_relids)
916  {
917  LogicalRepRelId relid = lfirst_oid(lc);
919 
922  {
923  /*
924  * The relation can't become interesting in the middle of the
925  * transaction so it's safe to unlock it.
926  */
928  continue;
929  }
930 
931  remote_rels = lappend(remote_rels, rel);
932  rels = lappend(rels, rel->localrel);
933  relids = lappend_oid(relids, rel->localreloid);
935  relids_logged = lappend_oid(relids_logged, rel->localreloid);
936  }
937 
938  /*
939  * Even if we used CASCADE on the upstream master we explicitly default to
940  * replaying changes without further cascading. This might be later
941  * changeable with a user specified option.
942  */
943  ExecuteTruncateGuts(rels, relids, relids_logged, DROP_RESTRICT, restart_seqs);
944 
945  foreach(lc, remote_rels)
946  {
947  LogicalRepRelMapEntry *rel = lfirst(lc);
948 
950  }
951 
953 }
954 
955 
956 /*
957  * Logical replication protocol message dispatcher.
958  */
959 static void
961 {
962  char action = pq_getmsgbyte(s);
963 
964  switch (action)
965  {
966  /* BEGIN */
967  case 'B':
969  break;
970  /* COMMIT */
971  case 'C':
973  break;
974  /* INSERT */
975  case 'I':
977  break;
978  /* UPDATE */
979  case 'U':
981  break;
982  /* DELETE */
983  case 'D':
985  break;
986  /* TRUNCATE */
987  case 'T':
989  break;
990  /* RELATION */
991  case 'R':
993  break;
994  /* TYPE */
995  case 'Y':
997  break;
998  /* ORIGIN */
999  case 'O':
1001  break;
1002  default:
1003  ereport(ERROR,
1004  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1005  errmsg("invalid logical replication message type \"%c\"", action)));
1006  }
1007 }
1008 
1009 /*
1010  * Figure out which write/flush positions to report to the walsender process.
1011  *
1012  * We can't simply report back the last LSN the walsender sent us because the
1013  * local transaction might not yet be flushed to disk locally. Instead we
1014  * build a list that associates local with remote LSNs for every commit. When
1015  * reporting back the flush position to the sender we iterate that list and
1016  * check which entries on it are already locally flushed. Those we can report
1017  * as having been flushed.
1018  *
1019  * The have_pending_txes is true if there are outstanding transactions that
1020  * need to be flushed.
1021  */
1022 static void
1024  bool *have_pending_txes)
1025 {
1026  dlist_mutable_iter iter;
1027  XLogRecPtr local_flush = GetFlushRecPtr();
1028 
1029  *write = InvalidXLogRecPtr;
1030  *flush = InvalidXLogRecPtr;
1031 
1032  dlist_foreach_modify(iter, &lsn_mapping)
1033  {
1034  FlushPosition *pos =
1036 
1037  *write = pos->remote_end;
1038 
1039  if (pos->local_end <= local_flush)
1040  {
1041  *flush = pos->remote_end;
1042  dlist_delete(iter.cur);
1043  pfree(pos);
1044  }
1045  else
1046  {
1047  /*
1048  * Don't want to uselessly iterate over the rest of the list which
1049  * could potentially be long. Instead get the last element and
1050  * grab the write position from there.
1051  */
1053  &lsn_mapping);
1054  *write = pos->remote_end;
1055  *have_pending_txes = true;
1056  return;
1057  }
1058  }
1059 
1060  *have_pending_txes = !dlist_is_empty(&lsn_mapping);
1061 }
1062 
1063 /*
1064  * Store current remote/local lsn pair in the tracking list.
1065  */
1066 static void
1068 {
1069  FlushPosition *flushpos;
1070 
1071  /* Need to do this in permanent context */
1072  MemoryContextSwitchTo(ApplyContext);
1073 
1074  /* Track commit lsn */
1075  flushpos = (FlushPosition *) palloc(sizeof(FlushPosition));
1076  flushpos->local_end = XactLastCommitEnd;
1077  flushpos->remote_end = remote_lsn;
1078 
1079  dlist_push_tail(&lsn_mapping, &flushpos->node);
1080  MemoryContextSwitchTo(ApplyMessageContext);
1081 }
1082 
1083 
1084 /* Update statistics of the worker. */
1085 static void
1087 {
1089  MyLogicalRepWorker->last_send_time = send_time;
1091  if (reply)
1092  {
1094  MyLogicalRepWorker->reply_time = send_time;
1095  }
1096 }
1097 
1098 /*
1099  * Apply main loop.
1100  */
1101 static void
1103 {
1104  /*
1105  * Init the ApplyMessageContext which we clean up after each replication
1106  * protocol message.
1107  */
1108  ApplyMessageContext = AllocSetContextCreate(ApplyContext,
1109  "ApplyMessageContext",
1111 
1112  /* mark as idle, before starting to loop */
1114 
1115  for (;;)
1116  {
1118  int rc;
1119  int len;
1120  char *buf = NULL;
1121  bool endofstream = false;
1122  TimestampTz last_recv_timestamp = GetCurrentTimestamp();
1123  bool ping_sent = false;
1124  long wait_time;
1125 
1127 
1128  MemoryContextSwitchTo(ApplyMessageContext);
1129 
1130  len = walrcv_receive(wrconn, &buf, &fd);
1131 
1132  if (len != 0)
1133  {
1134  /* Process the data */
1135  for (;;)
1136  {
1138 
1139  if (len == 0)
1140  {
1141  break;
1142  }
1143  else if (len < 0)
1144  {
1145  ereport(LOG,
1146  (errmsg("data stream from publisher has ended")));
1147  endofstream = true;
1148  break;
1149  }
1150  else
1151  {
1152  int c;
1153  StringInfoData s;
1154 
1155  /* Reset timeout. */
1156  last_recv_timestamp = GetCurrentTimestamp();
1157  ping_sent = false;
1158 
1159  /* Ensure we are reading the data into our memory context. */
1160  MemoryContextSwitchTo(ApplyMessageContext);
1161 
1162  s.data = buf;
1163  s.len = len;
1164  s.cursor = 0;
1165  s.maxlen = -1;
1166 
1167  c = pq_getmsgbyte(&s);
1168 
1169  if (c == 'w')
1170  {
1171  XLogRecPtr start_lsn;
1172  XLogRecPtr end_lsn;
1173  TimestampTz send_time;
1174 
1175  start_lsn = pq_getmsgint64(&s);
1176  end_lsn = pq_getmsgint64(&s);
1177  send_time = pq_getmsgint64(&s);
1178 
1179  if (last_received < start_lsn)
1180  last_received = start_lsn;
1181 
1182  if (last_received < end_lsn)
1183  last_received = end_lsn;
1184 
1185  UpdateWorkerStats(last_received, send_time, false);
1186 
1187  apply_dispatch(&s);
1188  }
1189  else if (c == 'k')
1190  {
1191  XLogRecPtr end_lsn;
1193  bool reply_requested;
1194 
1195  end_lsn = pq_getmsgint64(&s);
1196  timestamp = pq_getmsgint64(&s);
1197  reply_requested = pq_getmsgbyte(&s);
1198 
1199  if (last_received < end_lsn)
1200  last_received = end_lsn;
1201 
1202  send_feedback(last_received, reply_requested, false);
1203  UpdateWorkerStats(last_received, timestamp, true);
1204  }
1205  /* other message types are purposefully ignored */
1206 
1207  MemoryContextReset(ApplyMessageContext);
1208  }
1209 
1210  len = walrcv_receive(wrconn, &buf, &fd);
1211  }
1212  }
1213 
1214  /* confirm all writes so far */
1215  send_feedback(last_received, false, false);
1216 
1217  if (!in_remote_transaction)
1218  {
1219  /*
1220  * If we didn't get any transactions for a while there might be
1221  * unconsumed invalidation messages in the queue, consume them
1222  * now.
1223  */
1226 
1227  /* Process any table synchronization changes. */
1228  process_syncing_tables(last_received);
1229  }
1230 
1231  /* Cleanup the memory. */
1232  MemoryContextResetAndDeleteChildren(ApplyMessageContext);
1234 
1235  /* Check if we need to exit the streaming loop. */
1236  if (endofstream)
1237  {
1238  TimeLineID tli;
1239 
1240  walrcv_endstreaming(wrconn, &tli);
1241  break;
1242  }
1243 
1244  /*
1245  * Wait for more data or latch. If we have unflushed transactions,
1246  * wake up after WalWriterDelay to see if they've been flushed yet (in
1247  * which case we should send a feedback message). Otherwise, there's
1248  * no particular urgency about waking up unless we get data or a
1249  * signal.
1250  */
1251  if (!dlist_is_empty(&lsn_mapping))
1252  wait_time = WalWriterDelay;
1253  else
1254  wait_time = NAPTIME_PER_CYCLE;
1255 
1259  fd, wait_time,
1261 
1262  /* Emergency bailout if postmaster has died */
1263  if (rc & WL_POSTMASTER_DEATH)
1264  proc_exit(1);
1265 
1266  if (rc & WL_LATCH_SET)
1267  {
1270  }
1271 
1272  if (got_SIGHUP)
1273  {
1274  got_SIGHUP = false;
1276  }
1277 
1278  if (rc & WL_TIMEOUT)
1279  {
1280  /*
1281  * We didn't receive anything new. If we haven't heard anything
1282  * from the server for more than wal_receiver_timeout / 2, ping
1283  * the server. Also, if it's been longer than
1284  * wal_receiver_status_interval since the last update we sent,
1285  * send a status update to the master anyway, to report any
1286  * progress in applying WAL.
1287  */
1288  bool requestReply = false;
1289 
1290  /*
1291  * Check if time since last receive from standby has reached the
1292  * configured limit.
1293  */
1294  if (wal_receiver_timeout > 0)
1295  {
1297  TimestampTz timeout;
1298 
1299  timeout =
1300  TimestampTzPlusMilliseconds(last_recv_timestamp,
1302 
1303  if (now >= timeout)
1304  ereport(ERROR,
1305  (errmsg("terminating logical replication worker due to timeout")));
1306 
1307  /*
1308  * We didn't receive anything new, for half of receiver
1309  * replication timeout. Ping the server.
1310  */
1311  if (!ping_sent)
1312  {
1313  timeout = TimestampTzPlusMilliseconds(last_recv_timestamp,
1314  (wal_receiver_timeout / 2));
1315  if (now >= timeout)
1316  {
1317  requestReply = true;
1318  ping_sent = true;
1319  }
1320  }
1321  }
1322 
1323  send_feedback(last_received, requestReply, requestReply);
1324  }
1325  }
1326 }
1327 
1328 /*
1329  * Send a Standby Status Update message to server.
1330  *
1331  * 'recvpos' is the latest LSN we've received data to, force is set if we need
1332  * to send a response to avoid timeouts.
1333  */
1334 static void
1335 send_feedback(XLogRecPtr recvpos, bool force, bool requestReply)
1336 {
1337  static StringInfo reply_message = NULL;
1338  static TimestampTz send_time = 0;
1339 
1340  static XLogRecPtr last_recvpos = InvalidXLogRecPtr;
1341  static XLogRecPtr last_writepos = InvalidXLogRecPtr;
1342  static XLogRecPtr last_flushpos = InvalidXLogRecPtr;
1343 
1344  XLogRecPtr writepos;
1345  XLogRecPtr flushpos;
1346  TimestampTz now;
1347  bool have_pending_txes;
1348 
1349  /*
1350  * If the user doesn't want status to be reported to the publisher, be
1351  * sure to exit before doing anything at all.
1352  */
1353  if (!force && wal_receiver_status_interval <= 0)
1354  return;
1355 
1356  /* It's legal to not pass a recvpos */
1357  if (recvpos < last_recvpos)
1358  recvpos = last_recvpos;
1359 
1360  get_flush_position(&writepos, &flushpos, &have_pending_txes);
1361 
1362  /*
1363  * No outstanding transactions to flush, we can report the latest received
1364  * position. This is important for synchronous replication.
1365  */
1366  if (!have_pending_txes)
1367  flushpos = writepos = recvpos;
1368 
1369  if (writepos < last_writepos)
1370  writepos = last_writepos;
1371 
1372  if (flushpos < last_flushpos)
1373  flushpos = last_flushpos;
1374 
1375  now = GetCurrentTimestamp();
1376 
1377  /* if we've already reported everything we're good */
1378  if (!force &&
1379  writepos == last_writepos &&
1380  flushpos == last_flushpos &&
1381  !TimestampDifferenceExceeds(send_time, now,
1383  return;
1384  send_time = now;
1385 
1386  if (!reply_message)
1387  {
1388  MemoryContext oldctx = MemoryContextSwitchTo(ApplyContext);
1389 
1390  reply_message = makeStringInfo();
1391  MemoryContextSwitchTo(oldctx);
1392  }
1393  else
1394  resetStringInfo(reply_message);
1395 
1396  pq_sendbyte(reply_message, 'r');
1397  pq_sendint64(reply_message, recvpos); /* write */
1398  pq_sendint64(reply_message, flushpos); /* flush */
1399  pq_sendint64(reply_message, writepos); /* apply */
1400  pq_sendint64(reply_message, now); /* sendTime */
1401  pq_sendbyte(reply_message, requestReply); /* replyRequested */
1402 
1403  elog(DEBUG2, "sending feedback (force %d) to recv %X/%X, write %X/%X, flush %X/%X",
1404  force,
1405  (uint32) (recvpos >> 32), (uint32) recvpos,
1406  (uint32) (writepos >> 32), (uint32) writepos,
1407  (uint32) (flushpos >> 32), (uint32) flushpos
1408  );
1409 
1410  walrcv_send(wrconn, reply_message->data, reply_message->len);
1411 
1412  if (recvpos > last_recvpos)
1413  last_recvpos = recvpos;
1414  if (writepos > last_writepos)
1415  last_writepos = writepos;
1416  if (flushpos > last_flushpos)
1417  last_flushpos = flushpos;
1418 }
1419 
1420 /*
1421  * Reread subscription info if needed. Most changes will be exit.
1422  */
1423 static void
1425 {
1426  MemoryContext oldctx;
1428  bool started_tx = false;
1429 
1430  /* When cache state is valid there is nothing to do here. */
1431  if (MySubscriptionValid)
1432  return;
1433 
1434  /* This function might be called inside or outside of transaction. */
1435  if (!IsTransactionState())
1436  {
1438  started_tx = true;
1439  }
1440 
1441  /* Ensure allocations in permanent context. */
1442  oldctx = MemoryContextSwitchTo(ApplyContext);
1443 
1444  newsub = GetSubscription(MyLogicalRepWorker->subid, true);
1445 
1446  /*
1447  * Exit if the subscription was removed. This normally should not happen
1448  * as the worker gets killed during DROP SUBSCRIPTION.
1449  */
1450  if (!newsub)
1451  {
1452  ereport(LOG,
1453  (errmsg("logical replication apply worker for subscription \"%s\" will "
1454  "stop because the subscription was removed",
1455  MySubscription->name)));
1456 
1457  proc_exit(0);
1458  }
1459 
1460  /*
1461  * Exit if the subscription was disabled. This normally should not happen
1462  * as the worker gets killed during ALTER SUBSCRIPTION ... DISABLE.
1463  */
1464  if (!newsub->enabled)
1465  {
1466  ereport(LOG,
1467  (errmsg("logical replication apply worker for subscription \"%s\" will "
1468  "stop because the subscription was disabled",
1469  MySubscription->name)));
1470 
1471  proc_exit(0);
1472  }
1473 
1474  /*
1475  * Exit if connection string was changed. The launcher will start new
1476  * worker.
1477  */
1478  if (strcmp(newsub->conninfo, MySubscription->conninfo) != 0)
1479  {
1480  ereport(LOG,
1481  (errmsg("logical replication apply worker for subscription \"%s\" will "
1482  "restart because the connection information was changed",
1483  MySubscription->name)));
1484 
1485  proc_exit(0);
1486  }
1487 
1488  /*
1489  * Exit if subscription name was changed (it's used for
1490  * fallback_application_name). The launcher will start new worker.
1491  */
1492  if (strcmp(newsub->name, MySubscription->name) != 0)
1493  {
1494  ereport(LOG,
1495  (errmsg("logical replication apply worker for subscription \"%s\" will "
1496  "restart because subscription was renamed",
1497  MySubscription->name)));
1498 
1499  proc_exit(0);
1500  }
1501 
1502  /* !slotname should never happen when enabled is true. */
1503  Assert(newsub->slotname);
1504 
1505  /*
1506  * We need to make new connection to new slot if slot name has changed so
1507  * exit here as well if that's the case.
1508  */
1509  if (strcmp(newsub->slotname, MySubscription->slotname) != 0)
1510  {
1511  ereport(LOG,
1512  (errmsg("logical replication apply worker for subscription \"%s\" will "
1513  "restart because the replication slot name was changed",
1514  MySubscription->name)));
1515 
1516  proc_exit(0);
1517  }
1518 
1519  /*
1520  * Exit if publication list was changed. The launcher will start new
1521  * worker.
1522  */
1523  if (!equal(newsub->publications, MySubscription->publications))
1524  {
1525  ereport(LOG,
1526  (errmsg("logical replication apply worker for subscription \"%s\" will "
1527  "restart because subscription's publications were changed",
1528  MySubscription->name)));
1529 
1530  proc_exit(0);
1531  }
1532 
1533  /* Check for other changes that should never happen too. */
1534  if (newsub->dbid != MySubscription->dbid)
1535  {
1536  elog(ERROR, "subscription %u changed unexpectedly",
1538  }
1539 
1540  /* Clean old subscription info and switch to new one. */
1541  FreeSubscription(MySubscription);
1542  MySubscription = newsub;
1543 
1544  MemoryContextSwitchTo(oldctx);
1545 
1546  /* Change synchronous commit according to the user's wishes */
1547  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1549 
1550  if (started_tx)
1552 
1553  MySubscriptionValid = true;
1554 }
1555 
1556 /*
1557  * Callback from subscription syscache invalidation.
1558  */
1559 static void
1560 subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
1561 {
1562  MySubscriptionValid = false;
1563 }
1564 
1565 /* SIGHUP: set flag to reload configuration at next convenient time */
1566 static void
1568 {
1569  int save_errno = errno;
1570 
1571  got_SIGHUP = true;
1572 
1573  /* Waken anything waiting on the process latch */
1574  SetLatch(MyLatch);
1575 
1576  errno = save_errno;
1577 }
1578 
1579 /* Logical Replication Apply worker entry point */
1580 void
1582 {
1583  int worker_slot = DatumGetInt32(main_arg);
1584  MemoryContext oldctx;
1585  char originname[NAMEDATALEN];
1586  XLogRecPtr origin_startpos;
1587  char *myslotname;
1589 
1590  /* Attach to slot */
1591  logicalrep_worker_attach(worker_slot);
1592 
1593  /* Setup signal handling */
1595  pqsignal(SIGTERM, die);
1597 
1598  /* Initialise stats to a sanish value */
1601 
1602  /* Load the libpq-specific functions */
1603  load_file("libpqwalreceiver", false);
1604 
1605  Assert(CurrentResourceOwner == NULL);
1607  "logical replication apply");
1608 
1609  /* Run as replica session replication role. */
1610  SetConfigOption("session_replication_role", "replica",
1612 
1613  /* Connect to our database. */
1616  0);
1617 
1618  /* Load the subscription into persistent memory context. */
1619  ApplyContext = AllocSetContextCreate(TopMemoryContext,
1620  "ApplyContext",
1623  oldctx = MemoryContextSwitchTo(ApplyContext);
1624 
1625  MySubscription = GetSubscription(MyLogicalRepWorker->subid, true);
1626  if (!MySubscription)
1627  {
1628  ereport(LOG,
1629  (errmsg("logical replication apply worker for subscription %u will not "
1630  "start because the subscription was removed during startup",
1632  proc_exit(0);
1633  }
1634 
1635  MySubscriptionValid = true;
1636  MemoryContextSwitchTo(oldctx);
1637 
1638  if (!MySubscription->enabled)
1639  {
1640  ereport(LOG,
1641  (errmsg("logical replication apply worker for subscription \"%s\" will not "
1642  "start because the subscription was disabled during startup",
1643  MySubscription->name)));
1644 
1645  proc_exit(0);
1646  }
1647 
1648  /* Setup synchronous commit according to the user's wishes */
1649  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1651 
1652  /* Keep us informed about subscription changes. */
1655  (Datum) 0);
1656 
1657  if (am_tablesync_worker())
1658  ereport(LOG,
1659  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
1660  MySubscription->name, get_rel_name(MyLogicalRepWorker->relid))));
1661  else
1662  ereport(LOG,
1663  (errmsg("logical replication apply worker for subscription \"%s\" has started",
1664  MySubscription->name)));
1665 
1667 
1668  /* Connect to the origin and start the replication. */
1669  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1670  MySubscription->conninfo);
1671 
1672  if (am_tablesync_worker())
1673  {
1674  char *syncslotname;
1675 
1676  /* This is table synchroniation worker, call initial sync. */
1677  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1678 
1679  /* The slot name needs to be allocated in permanent memory context. */
1680  oldctx = MemoryContextSwitchTo(ApplyContext);
1681  myslotname = pstrdup(syncslotname);
1682  MemoryContextSwitchTo(oldctx);
1683 
1684  pfree(syncslotname);
1685  }
1686  else
1687  {
1688  /* This is main apply worker */
1689  RepOriginId originid;
1690  TimeLineID startpointTLI;
1691  char *err;
1692  int server_version;
1693 
1694  myslotname = MySubscription->slotname;
1695 
1696  /*
1697  * This shouldn't happen if the subscription is enabled, but guard
1698  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
1699  * crash if slot is NULL.)
1700  */
1701  if (!myslotname)
1702  ereport(ERROR,
1703  (errmsg("subscription has no replication slot set")));
1704 
1705  /* Setup replication origin tracking. */
1707  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1708  originid = replorigin_by_name(originname, true);
1709  if (!OidIsValid(originid))
1710  originid = replorigin_create(originname);
1711  replorigin_session_setup(originid);
1712  replorigin_session_origin = originid;
1713  origin_startpos = replorigin_session_get_progress(false);
1715 
1716  wrconn = walrcv_connect(MySubscription->conninfo, true, MySubscription->name,
1717  &err);
1718  if (wrconn == NULL)
1719  ereport(ERROR,
1720  (errmsg("could not connect to the publisher: %s", err)));
1721 
1722  /*
1723  * We don't really use the output identify_system for anything but it
1724  * does some initializations on the upstream so let's still call it.
1725  */
1726  (void) walrcv_identify_system(wrconn, &startpointTLI,
1727  &server_version);
1728 
1729  }
1730 
1731  /*
1732  * Setup callback for syscache so that we know when something changes in
1733  * the subscription relation state.
1734  */
1737  (Datum) 0);
1738 
1739  /* Build logical replication streaming options. */
1740  options.logical = true;
1741  options.startpoint = origin_startpos;
1742  options.slotname = myslotname;
1743  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1744  options.proto.logical.publication_names = MySubscription->publications;
1745 
1746  /* Start normal logical streaming replication. */
1747  walrcv_startstreaming(wrconn, &options);
1748 
1749  /* Run the main loop. */
1750  LogicalRepApplyLoop(origin_startpos);
1751 
1752  proc_exit(0);
1753 }
1754 
1755 /*
1756  * Is current process a logical replication worker?
1757  */
1758 bool
1760 {
1761  return MyLogicalRepWorker != NULL;
1762 }
void ExecSimpleRelationInsert(EState *estate, TupleTableSlot *slot)
Subscription * MySubscription
Definition: worker.c:116
static void apply_handle_type(StringInfo s)
Definition: worker.c:555
static bool should_apply_changes_for_rel(LogicalRepRelMapEntry *rel)
Definition: worker.c:145
#define NIL
Definition: pg_list.h:69
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:356
static EState * create_estate_for_relation(LogicalRepRelMapEntry *rel)
Definition: worker.c:190
static Oid GetRelationIdentityOrPK(Relation rel)
Definition: worker.c:569
void InitResultRelInfo(ResultRelInfo *resultRelInfo, Relation resultRelationDesc, Index resultRelationIndex, Relation partition_root, int instrument_options)
Definition: execMain.c:1305
static void send_feedback(XLogRecPtr recvpos, bool force, bool requestReply)
Definition: worker.c:1335
WalReceiverConn * wrconn
Definition: worker.c:114
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define walrcv_endstreaming(conn, next_tli)
Definition: walreceiver.h:269
#define DEBUG1
Definition: elog.h:25
dlist_node * cur
Definition: ilist.h:180
static void store_flush_position(XLogRecPtr remote_lsn)
Definition: worker.c:1067
uint32 TimeLineID
Definition: xlogdefs.h:45
LogicalRepRelId logicalrep_read_delete(StringInfo in, LogicalRepTupleData *oldtup)
Definition: proto.c:280
void AcceptInvalidationMessages(void)
Definition: inval.c:679
CommandId es_output_cid
Definition: execnodes.h:487
static XLogRecPtr remote_final_lsn
Definition: worker.c:120
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:4521
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
static void apply_handle_insert(StringInfo s)
Definition: worker.c:585
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
static dlist_head lsn_mapping
Definition: worker.c:102
#define DatumGetInt32(X)
Definition: postgres.h:455
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2986
#define RelationGetDescr(relation)
Definition: rel.h:433
void process_syncing_tables(XLogRecPtr current_lsn)
Definition: tablesync.c:544
dlist_node node
Definition: worker.c:97
#define write(a, b, c)
Definition: win32.h:14
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:2994
int64 timestamp
void logicalrep_read_begin(StringInfo in, LogicalRepBeginData *begin_data)
Definition: proto.c:60
int64 TimestampTz
Definition: timestamp.h:39
LogicalRepRelMapEntry * rel
Definition: worker.c:106
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
List * logicalrep_read_truncate(StringInfo in, bool *cascade, bool *restart_seqs)
Definition: proto.c:329
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void CommitTransactionCommand(void)
Definition: xact.c:2744
static void UpdateWorkerStats(XLogRecPtr last_lsn, TimestampTz send_time, bool reply)
Definition: worker.c:1086
XLogRecPtr XactLastCommitEnd
Definition: xlog.c:340
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
#define walrcv_receive(conn, buffer, wait_fd)
Definition: walreceiver.h:271
LogicalRepRelation * logicalrep_read_rel(StringInfo in)
Definition: proto.c:379
Expr * expression_planner(Expr *expr)
Definition: planner.c:5888
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
TimestampTz last_send_time
#define InvalidBuffer
Definition: buf.h:25
uint16 RepOriginId
Definition: xlogdefs.h:51
XLogRecPtr last_lsn
#define walrcv_startstreaming(conn, options)
Definition: walreceiver.h:267
void proc_exit(int code)
Definition: ipc.c:104
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1165
static void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.h:156
XLogRecPtr remote_end
Definition: worker.c:99
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:368
Datum * tts_values
Definition: tuptable.h:130
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define WL_SOCKET_READABLE
Definition: latch.h:125
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8271
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
#define RelationIsLogicallyLogged(relation)
Definition: rel.h:580
static void pq_sendbyte(StringInfo buf, int8 byt)
Definition: pqformat.h:164
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1030
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
List * es_range_table
Definition: execnodes.h:480
#define LOG
Definition: elog.h:26
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
struct SlotErrCallbackArg SlotErrCallbackArg
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:815
bool IsLogicalWorker(void)
Definition: worker.c:1759
void(* callback)(void *arg)
Definition: elog.h:239
int wal_receiver_status_interval
Definition: walreceiver.c:75
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
struct ErrorContextCallback * previous
Definition: elog.h:238
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
#define OidIsValid(objectId)
Definition: c.h:605
XLogRecPtr last_lsn
Definition: walsender.c:214
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
int natts
Definition: tupdesc.h:82
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc)
Definition: execTuples.c:931
int wal_receiver_timeout
Definition: walreceiver.c:76
static void get_flush_position(XLogRecPtr *write, XLogRecPtr *flush, bool *have_pending_txes)
Definition: worker.c:1023
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:149
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:3252
LogicalRepRelId logicalrep_read_insert(StringInfo in, LogicalRepTupleData *newtup)
Definition: proto.c:163
ErrorContextCallback * error_context_stack
Definition: elog.c:88
union WalRcvStreamOptions::@107 proto
static void slot_fill_defaults(LogicalRepRelMapEntry *rel, EState *estate, TupleTableSlot *slot)
Definition: worker.c:231
#define list_make1(x1)
Definition: pg_list.h:139
#define NAMEDATALEN
void FreeExecutorState(EState *estate)
Definition: execUtils.c:188
Subscription * GetSubscription(Oid subid, bool missing_ok)
#define GetPerTupleExprContext(estate)
Definition: executor.h:489
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:154
static StringInfoData reply_message
Definition: walreceiver.c:112
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1560
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
void pfree(void *pointer)
Definition: mcxt.c:1031
#define dlist_tail_element(type, membername, lhead)
Definition: ilist.h:496
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
#define ERROR
Definition: elog.h:43
static bool ensure_transaction(void)
Definition: worker.c:161
LogicalRepRelation remoterel
#define NAPTIME_PER_CYCLE
Definition: worker.c:93
bool in_remote_transaction
Definition: worker.c:119
void ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged, DropBehavior behavior, bool restart_seqs)
Definition: tablecmds.c:1411
void logicalrep_read_typ(StringInfo in, LogicalRepTyp *ltyp)
Definition: proto.c:431
Definition: guc.h:75
static void slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel, char **values)
Definition: worker.c:321
XLogRecPtr reply_lsn
static bool am_tablesync_worker(void)
static void apply_handle_delete(StringInfo s)
Definition: worker.c:800
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define DEBUG2
Definition: elog.h:24
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:156
char * c
#define walrcv_identify_system(conn, primary_tli, server_version)
Definition: walreceiver.h:263
void logicalrep_worker_attach(int slot)
Definition: launcher.c:612
#define NoLock
Definition: lockdefs.h:34
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6905
static char * buf
Definition: pg_test_fsync.c:67
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:157
bool changed[MaxTupleAttributeNumber]
Definition: logicalproto.h:36
bool * tts_isnull
Definition: tuptable.h:132
void ExecSimpleRelationDelete(EState *estate, EPQState *epqstate, TupleTableSlot *searchslot)
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:281
ResultRelInfo * es_result_relations
Definition: execnodes.h:490
#define RowExclusiveLock
Definition: lockdefs.h:38
#define SIGHUP
Definition: win32_port.h:163
#define RelationGetRelationName(relation)
Definition: rel.h:441
XLogRecPtr startpoint
Definition: walreceiver.h:154
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
unsigned int uint32
Definition: c.h:325
int pgsocket
Definition: port.h:31
List * publications
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void apply_handle_begin(StringInfo s)
Definition: worker.c:455
RepOriginId replorigin_create(char *roname)
Definition: origin.c:242
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:219
TupleTableSlot * es_trig_tuple_slot
Definition: execnodes.h:512
Oid get_atttype(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:863
#define ereport(elevel, rest)
Definition: elog.h:122
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2617
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1612
MemoryContext TopMemoryContext
Definition: mcxt.c:44
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:409
EState * CreateExecutorState(void)
Definition: execUtils.c:80
Definition: guc.h:72
static void check_relation_updatable(LogicalRepRelMapEntry *rel)
Definition: worker.c:645
List * lappend(List *list, void *datum)
Definition: list.c:128
MemoryContext ApplyContext
Definition: worker.c:112
static char ** options
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
XLogRecPtr final_lsn
Definition: logicalproto.h:66
#define DLIST_STATIC_INIT(name)
Definition: ilist.h:248
static void apply_handle_commit(StringInfo s)
Definition: worker.c:474
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
static void logicalrep_worker_sighup(SIGNAL_ARGS)
Definition: worker.c:1567
Node * build_column_default(Relation rel, int attrno)
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
List * es_tupleTable
Definition: execnodes.h:525
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
void ExecResetTupleTable(List *tupleTable, bool shouldFree)
Definition: execTuples.c:186
static void apply_handle_update(StringInfo s)
Definition: worker.c:679
uintptr_t Datum
Definition: postgres.h:365
void CommandCounterIncrement(void)
Definition: xact.c:914
#define PGINVALID_SOCKET
Definition: port.h:33
int es_num_result_relations
Definition: execnodes.h:491
void EvalPlanQualInit(EPQState *epqstate, EState *estate, Plan *subplan, List *auxrowmarks, int epqParam)
Definition: execMain.c:2829
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5560
static void apply_handle_relation(StringInfo s)
Definition: worker.c:540
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
int16 attnum
Definition: pg_attribute.h:79
XLogRecPtr local_end
Definition: worker.c:98
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4765
static void apply_dispatch(StringInfo s)
Definition: worker.c:960
static void maybe_reread_subscription(void)
Definition: worker.c:1424
#define makeNode(_type_)
Definition: nodes.h:565
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
TimestampTz last_recv_time
static MemoryContext ApplyMessageContext
Definition: worker.c:111
#define SIGNAL_ARGS
Definition: c.h:1121
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:356
RepOriginId replorigin_session_origin
Definition: origin.c:155
static void apply_handle_origin(StringInfo s)
Definition: worker.c:518
void StartTransactionCommand(void)
Definition: xact.c:2673
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:137
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static int server_version
Definition: pg_dumpall.c:79
static void slot_store_error_callback(void *arg)
Definition: worker.c:286
LogicalRepRelId logicalrep_read_update(StringInfo in, bool *has_oldtuple, LogicalRepTupleData *oldtup, LogicalRepTupleData *newtup)
Definition: proto.c:214
bool IsTransactionState(void)
Definition: xact.c:350
#define walrcv_send(conn, buffer, nbytes)
Definition: walreceiver.h:273
void logicalrep_read_commit(StringInfo in, LogicalRepCommitData *commit_data)
Definition: proto.c:95
int WalWriterDelay
Definition: walwriter.c:68
bool MySubscriptionValid
Definition: worker.c:117
char * values[MaxTupleAttributeNumber]
Definition: logicalproto.h:34
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:494
void FreeSubscription(Subscription *sub)
char * logicalrep_typmap_gettypname(Oid remoteid)
Definition: relation.c:422
RTEKind rtekind
Definition: parsenodes.h:962
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4785
void SetCurrentStatementStartTimestamp(void)
Definition: xact.c:732
static void slot_modify_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel, char **values, bool *replaces)
Definition: worker.c:389
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
void die(SIGNAL_ARGS)
Definition: postgres.c:2657
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1102
bool RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode, TupleTableSlot *searchslot, TupleTableSlot *outslot)
int i
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
#define errcontext
Definition: elog.h:164
void * arg
struct Latch * MyLatch
Definition: globals.c:55
struct FlushPosition FlushPosition
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:119
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
HeapTuple tts_tuple
Definition: tuptable.h:122
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:679
#define elog
Definition: elog.h:219
XLogRecPtr commit_lsn
Definition: logicalproto.h:73
static color newsub(struct colormap *cm, color co)
Definition: regc_color.c:389
Definition: pg_list.h:45
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1824
#define WL_LATCH_SET
Definition: latch.h:124
#define RelationGetRelid(relation)
Definition: rel.h:407
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:224
Oid RelationGetPrimaryKeyIndex(Relation relation)
Definition: relcache.c:4500
void ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate, TupleTableSlot *searchslot, TupleTableSlot *slot)
TimestampTz committime
Definition: logicalproto.h:75
void logicalrep_typmap_update(LogicalRepTyp *remotetyp)
Definition: relation.c:388
void ApplyWorkerMain(Datum main_arg)
Definition: worker.c:1581
uint32 LogicalRepRelId
Definition: logicalproto.h:39
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:524
#define lfirst_oid(lc)
Definition: pg_list.h:108
TimestampTz reply_time
#define EvalPlanQualSetSlot(epqstate, slot)
Definition: executor.h:208
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:271
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:418
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5589
void pgstat_report_stat(bool force)
Definition: pgstat.c:812
static volatile sig_atomic_t got_SIGHUP
Definition: worker.c:129
static void apply_handle_truncate(StringInfo s)
Definition: worker.c:900
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:255
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:492