PostgreSQL Source Code  git master
tablesync.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  * tablesync.c
3  * PostgreSQL logical replication: initial table data synchronization
4  *
5  * Copyright (c) 2012-2021, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/backend/replication/logical/tablesync.c
9  *
10  * NOTES
11  * This file contains code for initial table data synchronization for
12  * logical replication.
13  *
14  * The initial data synchronization is done separately for each table,
15  * in a separate apply worker that only fetches the initial snapshot data
16  * from the publisher and then synchronizes the position in the stream with
17  * the main apply worker.
18  *
19  * There are several reasons for doing the synchronization this way:
20  * - It allows us to parallelize the initial data synchronization
21  * which lowers the time needed for it to happen.
22  * - The initial synchronization does not have to hold the xid and LSN
23  * for the time it takes to copy data of all tables, causing less
24  * bloat and lower disk consumption compared to doing the
25  * synchronization in a single process for the whole database.
26  * - It allows us to synchronize any tables added after the initial
27  * synchronization has finished.
28  *
29  * The stream position synchronization works in multiple steps:
30  * - Apply worker requests a tablesync worker to start, setting the new
31  * table state to INIT.
32  * - Tablesync worker starts; changes table state from INIT to DATASYNC while
33  * copying.
34  * - Tablesync worker does initial table copy; there is a FINISHEDCOPY (sync
35  * worker specific) state to indicate when the copy phase has completed, so
36  * if the worker crashes with this (non-memory) state then the copy will not
37  * be re-attempted.
38  * - Tablesync worker then sets table state to SYNCWAIT; waits for state change.
39  * - Apply worker periodically checks for tables in SYNCWAIT state. When
40  * any appear, it sets the table state to CATCHUP and starts loop-waiting
41  * until either the table state is set to SYNCDONE or the sync worker
42  * exits.
43  * - After the sync worker has seen the state change to CATCHUP, it will
44  * read the stream and apply changes (acting like an apply worker) until
45  * it catches up to the specified stream position. Then it sets the
46  * state to SYNCDONE. There might be zero changes applied between
47  * CATCHUP and SYNCDONE, because the sync worker might be ahead of the
48  * apply worker.
49  * - Once the state is set to SYNCDONE, the apply will continue tracking
50  * the table until it reaches the SYNCDONE stream position, at which
51  * point it sets state to READY and stops tracking. Again, there might
52  * be zero changes in between.
53  *
54  * So the state progression is always: INIT -> DATASYNC -> FINISHEDCOPY
55  * -> SYNCWAIT -> CATCHUP -> SYNCDONE -> READY.
56  *
57  * The catalog pg_subscription_rel is used to keep information about
58  * subscribed tables and their state. The catalog holds all states
59  * except SYNCWAIT and CATCHUP which are only in shared memory.
60  *
61  * Example flows look like this:
62  * - Apply is in front:
63  * sync:8
64  * -> set in catalog FINISHEDCOPY
65  * -> set in memory SYNCWAIT
66  * apply:10
67  * -> set in memory CATCHUP
68  * -> enter wait-loop
69  * sync:10
70  * -> set in catalog SYNCDONE
71  * -> exit
72  * apply:10
73  * -> exit wait-loop
74  * -> continue rep
75  * apply:11
76  * -> set in catalog READY
77  *
78  * - Sync is in front:
79  * sync:10
80  * -> set in catalog FINISHEDCOPY
81  * -> set in memory SYNCWAIT
82  * apply:8
83  * -> set in memory CATCHUP
84  * -> continue per-table filtering
85  * sync:10
86  * -> set in catalog SYNCDONE
87  * -> exit
88  * apply:10
89  * -> set in catalog READY
90  * -> stop per-table filtering
91  * -> continue rep
92  *-------------------------------------------------------------------------
93  */
94 
95 #include "postgres.h"
96 
97 #include "access/table.h"
98 #include "access/xact.h"
99 #include "catalog/indexing.h"
101 #include "catalog/pg_type.h"
102 #include "commands/copy.h"
103 #include "miscadmin.h"
104 #include "parser/parse_relation.h"
105 #include "pgstat.h"
108 #include "replication/walreceiver.h"
110 #include "replication/slot.h"
111 #include "replication/origin.h"
112 #include "storage/ipc.h"
113 #include "storage/lmgr.h"
114 #include "utils/builtins.h"
115 #include "utils/lsyscache.h"
116 #include "utils/memutils.h"
117 #include "utils/snapmgr.h"
118 #include "utils/syscache.h"
119 
120 static bool table_states_valid = false;
122 static bool FetchTableStates(bool *started_tx);
123 
125 
126 /*
127  * Exit routine for synchronization worker.
128  */
129 static void
131 finish_sync_worker(void)
132 {
133  /*
134  * Commit any outstanding transaction. This is the usual case, unless
135  * there was nothing to do for the table.
136  */
137  if (IsTransactionState())
138  {
140  pgstat_report_stat(false);
141  }
142 
143  /* And flush all writes. */
145 
147  ereport(LOG,
148  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has finished",
152 
153  /* Find the main apply worker and signal it. */
155 
156  /* Stop gracefully */
157  proc_exit(0);
158 }
159 
160 /*
161  * Wait until the relation sync state is set in the catalog to the expected
162  * one; return true when it happens.
163  *
164  * Returns false if the table sync worker or the table itself have
165  * disappeared, or the table state has been reset.
166  *
167  * Currently, this is used in the apply worker when transitioning from
168  * CATCHUP state to SYNCDONE.
169  */
170 static bool
171 wait_for_relation_state_change(Oid relid, char expected_state)
172 {
173  char state;
174 
175  for (;;)
176  {
177  LogicalRepWorker *worker;
178  XLogRecPtr statelsn;
179 
181 
184  relid, &statelsn);
185 
186  if (state == SUBREL_STATE_UNKNOWN)
187  break;
188 
189  if (state == expected_state)
190  return true;
191 
192  /* Check if the sync worker is still running and bail if not. */
193  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
195  false);
196  LWLockRelease(LogicalRepWorkerLock);
197  if (!worker)
198  break;
199 
200  (void) WaitLatch(MyLatch,
203 
205  }
206 
207  return false;
208 }
209 
210 /*
211  * Wait until the apply worker changes the state of our synchronization
212  * worker to the expected one.
213  *
214  * Used when transitioning from SYNCWAIT state to CATCHUP.
215  *
216  * Returns false if the apply worker has disappeared.
217  */
218 static bool
219 wait_for_worker_state_change(char expected_state)
220 {
221  int rc;
222 
223  for (;;)
224  {
225  LogicalRepWorker *worker;
226 
228 
229  /*
230  * Done if already in correct state. (We assume this fetch is atomic
231  * enough to not give a misleading answer if we do it with no lock.)
232  */
233  if (MyLogicalRepWorker->relstate == expected_state)
234  return true;
235 
236  /*
237  * Bail out if the apply worker has died, else signal it we're
238  * waiting.
239  */
240  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
242  InvalidOid, false);
243  if (worker && worker->proc)
245  LWLockRelease(LogicalRepWorkerLock);
246  if (!worker)
247  break;
248 
249  /*
250  * Wait. We expect to get a latch signal back from the apply worker,
251  * but use a timeout in case it dies without sending one.
252  */
253  rc = WaitLatch(MyLatch,
256 
257  if (rc & WL_LATCH_SET)
259  }
260 
261  return false;
262 }
263 
264 /*
265  * Callback from syscache invalidation.
266  */
267 void
269 {
270  table_states_valid = false;
271 }
272 
273 /*
274  * Handle table synchronization cooperation from the synchronization
275  * worker.
276  *
277  * If the sync worker is in CATCHUP state and reached (or passed) the
278  * predetermined synchronization point in the WAL stream, mark the table as
279  * SYNCDONE and finish.
280  */
281 static void
283 {
285 
286  if (MyLogicalRepWorker->relstate == SUBREL_STATE_CATCHUP &&
287  current_lsn >= MyLogicalRepWorker->relstate_lsn)
288  {
289  TimeLineID tli;
290  char syncslotname[NAMEDATALEN] = {0};
291 
292  MyLogicalRepWorker->relstate = SUBREL_STATE_SYNCDONE;
293  MyLogicalRepWorker->relstate_lsn = current_lsn;
294 
296 
297  /*
298  * UpdateSubscriptionRelState must be called within a transaction.
299  * That transaction will be ended within the finish_sync_worker().
300  */
301  if (!IsTransactionState())
303 
308 
309  /*
310  * End streaming so that LogRepWorkerWalRcvConn can be used to drop
311  * the slot.
312  */
314 
315  /*
316  * Cleanup the tablesync slot.
317  *
318  * This has to be done after updating the state because otherwise if
319  * there is an error while doing the database operations we won't be
320  * able to rollback dropped slot.
321  */
324  syncslotname,
325  sizeof(syncslotname));
326 
327  /*
328  * It is important to give an error if we are unable to drop the slot,
329  * otherwise, it won't be dropped till the corresponding subscription
330  * is dropped. So passing missing_ok = false.
331  */
333 
334  finish_sync_worker();
335  }
336  else
338 }
339 
340 /*
341  * Handle table synchronization cooperation from the apply worker.
342  *
343  * Walk over all subscription tables that are individually tracked by the
344  * apply process (currently, all that have state other than
345  * SUBREL_STATE_READY) and manage synchronization for them.
346  *
347  * If there are tables that need synchronizing and are not being synchronized
348  * yet, start sync workers for them (if there are free slots for sync
349  * workers). To prevent starting the sync worker for the same relation at a
350  * high frequency after a failure, we store its last start time with each sync
351  * state info. We start the sync worker for the same relation after waiting
352  * at least wal_retrieve_retry_interval.
353  *
354  * For tables that are being synchronized already, check if sync workers
355  * either need action from the apply worker or have finished. This is the
356  * SYNCWAIT to CATCHUP transition.
357  *
358  * If the synchronization position is reached (SYNCDONE), then the table can
359  * be marked as READY and is no longer tracked.
360  */
361 static void
363 {
364  struct tablesync_start_time_mapping
365  {
366  Oid relid;
367  TimestampTz last_start_time;
368  };
369  static HTAB *last_start_times = NULL;
370  ListCell *lc;
371  bool started_tx = false;
372 
374 
375  /* We need up-to-date sync state info for subscription tables here. */
376  FetchTableStates(&started_tx);
377 
378  /*
379  * Prepare a hash table for tracking last start times of workers, to avoid
380  * immediate restarts. We don't need it if there are no tables that need
381  * syncing.
382  */
383  if (table_states_not_ready && !last_start_times)
384  {
385  HASHCTL ctl;
386 
387  ctl.keysize = sizeof(Oid);
388  ctl.entrysize = sizeof(struct tablesync_start_time_mapping);
389  last_start_times = hash_create("Logical replication table sync worker start times",
390  256, &ctl, HASH_ELEM | HASH_BLOBS);
391  }
392 
393  /*
394  * Clean up the hash table when we're done with all tables (just to
395  * release the bit of memory).
396  */
397  else if (!table_states_not_ready && last_start_times)
398  {
399  hash_destroy(last_start_times);
400  last_start_times = NULL;
401  }
402 
403  /*
404  * Even when the two_phase mode is requested by the user, it remains as
405  * 'pending' until all tablesyncs have reached READY state.
406  *
407  * When this happens, we restart the apply worker and (if the conditions
408  * are still ok) then the two_phase tri-state will become 'enabled' at
409  * that time.
410  *
411  * Note: If the subscription has no tables then leave the state as
412  * PENDING, which allows ALTER SUBSCRIPTION ... REFRESH PUBLICATION to
413  * work.
414  */
417  {
418  ereport(LOG,
419  (errmsg("logical replication apply worker for subscription \"%s\" will restart so that two_phase can be enabled",
420  MySubscription->name)));
421 
422  proc_exit(0);
423  }
424 
425  /*
426  * Process all tables that are being synchronized.
427  */
428  foreach(lc, table_states_not_ready)
429  {
431 
432  if (rstate->state == SUBREL_STATE_SYNCDONE)
433  {
434  /*
435  * Apply has caught up to the position where the table sync has
436  * finished. Mark the table as ready so that the apply will just
437  * continue to replicate it normally.
438  */
439  if (current_lsn >= rstate->lsn)
440  {
441  char originname[NAMEDATALEN];
442 
443  rstate->state = SUBREL_STATE_READY;
444  rstate->lsn = current_lsn;
445  if (!started_tx)
446  {
448  started_tx = true;
449  }
450 
451  /*
452  * Remove the tablesync origin tracking if exists.
453  *
454  * The normal case origin drop is done here instead of in the
455  * process_syncing_tables_for_sync function because we don't
456  * allow to drop the origin till the process owning the origin
457  * is alive.
458  *
459  * There is a chance that the user is concurrently performing
460  * refresh for the subscription where we remove the table
461  * state and its origin and by this time the origin might be
462  * already removed. So passing missing_ok = true.
463  */
465  rstate->relid,
466  originname,
467  sizeof(originname));
468  replorigin_drop_by_name(originname, true, false);
469 
470  /*
471  * Update the state to READY only after the origin cleanup.
472  */
474  rstate->relid, rstate->state,
475  rstate->lsn);
476  }
477  }
478  else
479  {
480  LogicalRepWorker *syncworker;
481 
482  /*
483  * Look for a sync worker for this relation.
484  */
485  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
486 
488  rstate->relid, false);
489 
490  if (syncworker)
491  {
492  /* Found one, update our copy of its state */
493  SpinLockAcquire(&syncworker->relmutex);
494  rstate->state = syncworker->relstate;
495  rstate->lsn = syncworker->relstate_lsn;
496  if (rstate->state == SUBREL_STATE_SYNCWAIT)
497  {
498  /*
499  * Sync worker is waiting for apply. Tell sync worker it
500  * can catchup now.
501  */
502  syncworker->relstate = SUBREL_STATE_CATCHUP;
503  syncworker->relstate_lsn =
504  Max(syncworker->relstate_lsn, current_lsn);
505  }
506  SpinLockRelease(&syncworker->relmutex);
507 
508  /* If we told worker to catch up, wait for it. */
509  if (rstate->state == SUBREL_STATE_SYNCWAIT)
510  {
511  /* Signal the sync worker, as it may be waiting for us. */
512  if (syncworker->proc)
513  logicalrep_worker_wakeup_ptr(syncworker);
514 
515  /* Now safe to release the LWLock */
516  LWLockRelease(LogicalRepWorkerLock);
517 
518  /*
519  * Enter busy loop and wait for synchronization worker to
520  * reach expected state (or die trying).
521  */
522  if (!started_tx)
523  {
525  started_tx = true;
526  }
527 
529  SUBREL_STATE_SYNCDONE);
530  }
531  else
532  LWLockRelease(LogicalRepWorkerLock);
533  }
534  else
535  {
536  /*
537  * If there is no sync worker for this table yet, count
538  * running sync workers for this subscription, while we have
539  * the lock.
540  */
541  int nsyncworkers =
543 
544  /* Now safe to release the LWLock */
545  LWLockRelease(LogicalRepWorkerLock);
546 
547  /*
548  * If there are free sync worker slot(s), start a new sync
549  * worker for the table.
550  */
551  if (nsyncworkers < max_sync_workers_per_subscription)
552  {
554  struct tablesync_start_time_mapping *hentry;
555  bool found;
556 
557  hentry = hash_search(last_start_times, &rstate->relid,
558  HASH_ENTER, &found);
559 
560  if (!found ||
561  TimestampDifferenceExceeds(hentry->last_start_time, now,
563  {
568  rstate->relid);
569  hentry->last_start_time = now;
570  }
571  }
572  }
573  }
574  }
575 
576  if (started_tx)
577  {
579  pgstat_report_stat(false);
580  }
581 }
582 
583 /*
584  * Process possible state change(s) of tables that are being synchronized.
585  */
586 void
588 {
589  if (am_tablesync_worker())
590  process_syncing_tables_for_sync(current_lsn);
591  else
593 }
594 
595 /*
596  * Create list of columns for COPY based on logical relation mapping.
597  */
598 static List *
600 {
601  List *attnamelist = NIL;
602  int i;
603 
604  for (i = 0; i < rel->remoterel.natts; i++)
605  {
606  attnamelist = lappend(attnamelist,
607  makeString(rel->remoterel.attnames[i]));
608  }
609 
610 
611  return attnamelist;
612 }
613 
614 /*
615  * Data source callback for the COPY FROM, which reads from the remote
616  * connection and passes the data back to our local COPY.
617  */
618 static int
619 copy_read_data(void *outbuf, int minread, int maxread)
620 {
621  int bytesread = 0;
622  int avail;
623 
624  /* If there are some leftover data from previous read, use it. */
625  avail = copybuf->len - copybuf->cursor;
626  if (avail)
627  {
628  if (avail > maxread)
629  avail = maxread;
630  memcpy(outbuf, &copybuf->data[copybuf->cursor], avail);
631  copybuf->cursor += avail;
632  maxread -= avail;
633  bytesread += avail;
634  }
635 
636  while (maxread > 0 && bytesread < minread)
637  {
639  int len;
640  char *buf = NULL;
641 
642  for (;;)
643  {
644  /* Try read the data. */
645  len = walrcv_receive(LogRepWorkerWalRcvConn, &buf, &fd);
646 
648 
649  if (len == 0)
650  break;
651  else if (len < 0)
652  return bytesread;
653  else
654  {
655  /* Process the data */
656  copybuf->data = buf;
657  copybuf->len = len;
658  copybuf->cursor = 0;
659 
660  avail = copybuf->len - copybuf->cursor;
661  if (avail > maxread)
662  avail = maxread;
663  memcpy(outbuf, &copybuf->data[copybuf->cursor], avail);
664  outbuf = (void *) ((char *) outbuf + avail);
665  copybuf->cursor += avail;
666  maxread -= avail;
667  bytesread += avail;
668  }
669 
670  if (maxread <= 0 || bytesread >= minread)
671  return bytesread;
672  }
673 
674  /*
675  * Wait for more data or latch.
676  */
677  (void) WaitLatchOrSocket(MyLatch,
680  fd, 1000L, WAIT_EVENT_LOGICAL_SYNC_DATA);
681 
683  }
684 
685  return bytesread;
686 }
687 
688 
689 /*
690  * Get information about remote relation in similar fashion the RELATION
691  * message provides during replication.
692  */
693 static void
694 fetch_remote_table_info(char *nspname, char *relname,
695  LogicalRepRelation *lrel)
696 {
697  WalRcvExecResult *res;
698  StringInfoData cmd;
699  TupleTableSlot *slot;
700  Oid tableRow[] = {OIDOID, CHAROID, CHAROID};
701  Oid attrRow[] = {TEXTOID, OIDOID, BOOLOID};
702  bool isnull;
703  int natt;
704 
705  lrel->nspname = nspname;
706  lrel->relname = relname;
707 
708  /* First fetch Oid and replica identity. */
709  initStringInfo(&cmd);
710  appendStringInfo(&cmd, "SELECT c.oid, c.relreplident, c.relkind"
711  " FROM pg_catalog.pg_class c"
712  " INNER JOIN pg_catalog.pg_namespace n"
713  " ON (c.relnamespace = n.oid)"
714  " WHERE n.nspname = %s"
715  " AND c.relname = %s",
716  quote_literal_cstr(nspname),
717  quote_literal_cstr(relname));
719  lengthof(tableRow), tableRow);
720 
721  if (res->status != WALRCV_OK_TUPLES)
722  ereport(ERROR,
723  (errcode(ERRCODE_CONNECTION_FAILURE),
724  errmsg("could not fetch table info for table \"%s.%s\" from publisher: %s",
725  nspname, relname, res->err)));
726 
728  if (!tuplestore_gettupleslot(res->tuplestore, true, false, slot))
729  ereport(ERROR,
730  (errcode(ERRCODE_UNDEFINED_OBJECT),
731  errmsg("table \"%s.%s\" not found on publisher",
732  nspname, relname)));
733 
734  lrel->remoteid = DatumGetObjectId(slot_getattr(slot, 1, &isnull));
735  Assert(!isnull);
736  lrel->replident = DatumGetChar(slot_getattr(slot, 2, &isnull));
737  Assert(!isnull);
738  lrel->relkind = DatumGetChar(slot_getattr(slot, 3, &isnull));
739  Assert(!isnull);
740 
742  walrcv_clear_result(res);
743 
744  /* Now fetch columns. */
745  resetStringInfo(&cmd);
746  appendStringInfo(&cmd,
747  "SELECT a.attname,"
748  " a.atttypid,"
749  " a.attnum = ANY(i.indkey)"
750  " FROM pg_catalog.pg_attribute a"
751  " LEFT JOIN pg_catalog.pg_index i"
752  " ON (i.indexrelid = pg_get_replica_identity_index(%u))"
753  " WHERE a.attnum > 0::pg_catalog.int2"
754  " AND NOT a.attisdropped %s"
755  " AND a.attrelid = %u"
756  " ORDER BY a.attnum",
757  lrel->remoteid,
759  "AND a.attgenerated = ''" : ""),
760  lrel->remoteid);
762  lengthof(attrRow), attrRow);
763 
764  if (res->status != WALRCV_OK_TUPLES)
765  ereport(ERROR,
766  (errcode(ERRCODE_CONNECTION_FAILURE),
767  errmsg("could not fetch table info for table \"%s.%s\" from publisher: %s",
768  nspname, relname, res->err)));
769 
770  /* We don't know the number of rows coming, so allocate enough space. */
771  lrel->attnames = palloc0(MaxTupleAttributeNumber * sizeof(char *));
772  lrel->atttyps = palloc0(MaxTupleAttributeNumber * sizeof(Oid));
773  lrel->attkeys = NULL;
774 
775  natt = 0;
777  while (tuplestore_gettupleslot(res->tuplestore, true, false, slot))
778  {
779  lrel->attnames[natt] =
780  TextDatumGetCString(slot_getattr(slot, 1, &isnull));
781  Assert(!isnull);
782  lrel->atttyps[natt] = DatumGetObjectId(slot_getattr(slot, 2, &isnull));
783  Assert(!isnull);
784  if (DatumGetBool(slot_getattr(slot, 3, &isnull)))
785  lrel->attkeys = bms_add_member(lrel->attkeys, natt);
786 
787  /* Should never happen. */
788  if (++natt >= MaxTupleAttributeNumber)
789  elog(ERROR, "too many columns in remote table \"%s.%s\"",
790  nspname, relname);
791 
792  ExecClearTuple(slot);
793  }
795 
796  lrel->natts = natt;
797 
798  walrcv_clear_result(res);
799  pfree(cmd.data);
800 }
801 
802 /*
803  * Copy existing data of a table from publisher.
804  *
805  * Caller is responsible for locking the local relation.
806  */
807 static void
809 {
810  LogicalRepRelMapEntry *relmapentry;
811  LogicalRepRelation lrel;
812  WalRcvExecResult *res;
813  StringInfoData cmd;
814  CopyFromState cstate;
815  List *attnamelist;
816  ParseState *pstate;
817 
818  /* Get the publisher relation info. */
820  RelationGetRelationName(rel), &lrel);
821 
822  /* Put the relation into relmap. */
824 
825  /* Map the publisher relation to local one. */
826  relmapentry = logicalrep_rel_open(lrel.remoteid, NoLock);
827  Assert(rel == relmapentry->localrel);
828 
829  /* Start copy on the publisher. */
830  initStringInfo(&cmd);
831  if (lrel.relkind == RELKIND_RELATION)
832  appendStringInfo(&cmd, "COPY %s TO STDOUT",
834  else
835  {
836  /*
837  * For non-tables, we need to do COPY (SELECT ...), but we can't just
838  * do SELECT * because we need to not copy generated columns.
839  */
840  appendStringInfoString(&cmd, "COPY (SELECT ");
841  for (int i = 0; i < lrel.natts; i++)
842  {
844  if (i < lrel.natts - 1)
845  appendStringInfoString(&cmd, ", ");
846  }
847  appendStringInfo(&cmd, " FROM %s) TO STDOUT",
849  }
850  res = walrcv_exec(LogRepWorkerWalRcvConn, cmd.data, 0, NULL);
851  pfree(cmd.data);
852  if (res->status != WALRCV_OK_COPY_OUT)
853  ereport(ERROR,
854  (errcode(ERRCODE_CONNECTION_FAILURE),
855  errmsg("could not start initial contents copy for table \"%s.%s\": %s",
856  lrel.nspname, lrel.relname, res->err)));
857  walrcv_clear_result(res);
858 
859  copybuf = makeStringInfo();
860 
861  pstate = make_parsestate(NULL);
863  NULL, false, false);
864 
865  attnamelist = make_copy_attnamelist(relmapentry);
866  cstate = BeginCopyFrom(pstate, rel, NULL, NULL, false, copy_read_data, attnamelist, NIL);
867 
868  /* Do the copy */
869  (void) CopyFrom(cstate);
870 
871  logicalrep_rel_close(relmapentry, NoLock);
872 }
873 
874 /*
875  * Determine the tablesync slot name.
876  *
877  * The name must not exceed NAMEDATALEN - 1 because of remote node constraints
878  * on slot name length. We append system_identifier to avoid slot_name
879  * collision with subscriptions in other clusters. With the current scheme
880  * pg_%u_sync_%u_UINT64_FORMAT (3 + 10 + 6 + 10 + 20 + '\0'), the maximum
881  * length of slot_name will be 50.
882  *
883  * The returned slot name is stored in the supplied buffer (syncslotname) with
884  * the given size.
885  *
886  * Note: We don't use the subscription slot name as part of tablesync slot name
887  * because we are responsible for cleaning up these slots and it could become
888  * impossible to recalculate what name to cleanup if the subscription slot name
889  * had changed.
890  */
891 void
893  char *syncslotname, int szslot)
894 {
895  snprintf(syncslotname, szslot, "pg_%u_sync_%u_" UINT64_FORMAT, suboid,
896  relid, GetSystemIdentifier());
897 }
898 
899 /*
900  * Form the origin name for tablesync.
901  *
902  * Return the name in the supplied buffer.
903  */
904 void
906  char *originname, int szorgname)
907 {
908  snprintf(originname, szorgname, "pg_%u_%u", suboid, relid);
909 }
910 
911 /*
912  * Start syncing the table in the sync worker.
913  *
914  * If nothing needs to be done to sync the table, we exit the worker without
915  * any further action.
916  *
917  * The returned slot name is palloc'ed in current memory context.
918  */
919 char *
921 {
922  char *slotname;
923  char *err;
924  char relstate;
925  XLogRecPtr relstate_lsn;
926  Relation rel;
927  WalRcvExecResult *res;
928  char originname[NAMEDATALEN];
929  RepOriginId originid;
930 
931  /* Check the state of the table synchronization. */
935  &relstate_lsn);
937 
939  MyLogicalRepWorker->relstate = relstate;
940  MyLogicalRepWorker->relstate_lsn = relstate_lsn;
942 
943  /*
944  * If synchronization is already done or no longer necessary, exit now
945  * that we've updated shared memory state.
946  */
947  switch (relstate)
948  {
949  case SUBREL_STATE_SYNCDONE:
950  case SUBREL_STATE_READY:
951  case SUBREL_STATE_UNKNOWN:
952  finish_sync_worker(); /* doesn't return */
953  }
954 
955  /* Calculate the name of the tablesync slot. */
956  slotname = (char *) palloc(NAMEDATALEN);
959  slotname,
960  NAMEDATALEN);
961 
962  /*
963  * Here we use the slot name instead of the subscription name as the
964  * application_name, so that it is different from the main apply worker,
965  * so that synchronous replication can distinguish them.
966  */
968  walrcv_connect(MySubscription->conninfo, true, slotname, &err);
969  if (LogRepWorkerWalRcvConn == NULL)
970  ereport(ERROR,
971  (errcode(ERRCODE_CONNECTION_FAILURE),
972  errmsg("could not connect to the publisher: %s", err)));
973 
974  Assert(MyLogicalRepWorker->relstate == SUBREL_STATE_INIT ||
975  MyLogicalRepWorker->relstate == SUBREL_STATE_DATASYNC ||
976  MyLogicalRepWorker->relstate == SUBREL_STATE_FINISHEDCOPY);
977 
978  /* Assign the origin tracking record name. */
981  originname,
982  sizeof(originname));
983 
984  if (MyLogicalRepWorker->relstate == SUBREL_STATE_DATASYNC)
985  {
986  /*
987  * We have previously errored out before finishing the copy so the
988  * replication slot might exist. We want to remove the slot if it
989  * already exists and proceed.
990  *
991  * XXX We could also instead try to drop the slot, last time we failed
992  * but for that, we might need to clean up the copy state as it might
993  * be in the middle of fetching the rows. Also, if there is a network
994  * breakdown then it wouldn't have succeeded so trying it next time
995  * seems like a better bet.
996  */
998  }
999  else if (MyLogicalRepWorker->relstate == SUBREL_STATE_FINISHEDCOPY)
1000  {
1001  /*
1002  * The COPY phase was previously done, but tablesync then crashed
1003  * before it was able to finish normally.
1004  */
1006 
1007  /*
1008  * The origin tracking name must already exist. It was created first
1009  * time this tablesync was launched.
1010  */
1011  originid = replorigin_by_name(originname, false);
1012  replorigin_session_setup(originid);
1013  replorigin_session_origin = originid;
1014  *origin_startpos = replorigin_session_get_progress(false);
1015 
1017 
1018  goto copy_table_done;
1019  }
1020 
1022  MyLogicalRepWorker->relstate = SUBREL_STATE_DATASYNC;
1025 
1026  /* Update the state and make it visible to others. */
1033  pgstat_report_stat(false);
1034 
1036 
1037  /*
1038  * Use a standard write lock here. It might be better to disallow access
1039  * to the table while it's being synchronized. But we don't want to block
1040  * the main apply process from working and it has to open the relation in
1041  * RowExclusiveLock when remapping remote relation id to local one.
1042  */
1044 
1045  /*
1046  * Start a transaction in the remote node in REPEATABLE READ mode. This
1047  * ensures that both the replication slot we create (see below) and the
1048  * COPY are consistent with each other.
1049  */
1051  "BEGIN READ ONLY ISOLATION LEVEL REPEATABLE READ",
1052  0, NULL);
1053  if (res->status != WALRCV_OK_COMMAND)
1054  ereport(ERROR,
1055  (errcode(ERRCODE_CONNECTION_FAILURE),
1056  errmsg("table copy could not start transaction on publisher: %s",
1057  res->err)));
1058  walrcv_clear_result(res);
1059 
1060  /*
1061  * Create a new permanent logical decoding slot. This slot will be used
1062  * for the catchup phase after COPY is done, so tell it to use the
1063  * snapshot to make the final data consistent.
1064  *
1065  * Prevent cancel/die interrupts while creating slot here because it is
1066  * possible that before the server finishes this command, a concurrent
1067  * drop subscription happens which would complete without removing this
1068  * slot leading to a dangling slot on the server.
1069  */
1070  HOLD_INTERRUPTS();
1072  slotname, false /* permanent */ , false /* two_phase */ ,
1073  CRS_USE_SNAPSHOT, origin_startpos);
1075 
1076  /*
1077  * Setup replication origin tracking. The purpose of doing this before the
1078  * copy is to avoid doing the copy again due to any error in setting up
1079  * origin tracking.
1080  */
1081  originid = replorigin_by_name(originname, true);
1082  if (!OidIsValid(originid))
1083  {
1084  /*
1085  * Origin tracking does not exist, so create it now.
1086  *
1087  * Then advance to the LSN got from walrcv_create_slot. This is WAL
1088  * logged for the purpose of recovery. Locks are to prevent the
1089  * replication origin from vanishing while advancing.
1090  */
1091  originid = replorigin_create(originname);
1092 
1093  LockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
1094  replorigin_advance(originid, *origin_startpos, InvalidXLogRecPtr,
1095  true /* go backward */ , true /* WAL log */ );
1096  UnlockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
1097 
1098  replorigin_session_setup(originid);
1099  replorigin_session_origin = originid;
1100  }
1101  else
1102  {
1103  ereport(ERROR,
1105  errmsg("replication origin \"%s\" already exists",
1106  originname)));
1107  }
1108 
1109  /* Now do the initial data copy */
1111  copy_table(rel);
1113 
1114  res = walrcv_exec(LogRepWorkerWalRcvConn, "COMMIT", 0, NULL);
1115  if (res->status != WALRCV_OK_COMMAND)
1116  ereport(ERROR,
1117  (errcode(ERRCODE_CONNECTION_FAILURE),
1118  errmsg("table copy could not finish transaction on publisher: %s",
1119  res->err)));
1120  walrcv_clear_result(res);
1121 
1122  table_close(rel, NoLock);
1123 
1124  /* Make the copy visible. */
1126 
1127  /*
1128  * Update the persisted state to indicate the COPY phase is done; make it
1129  * visible to others.
1130  */
1133  SUBREL_STATE_FINISHEDCOPY,
1135 
1137 
1138 copy_table_done:
1139 
1140  elog(DEBUG1,
1141  "LogicalRepSyncTableStart: '%s' origin_startpos lsn %X/%X",
1142  originname, LSN_FORMAT_ARGS(*origin_startpos));
1143 
1144  /*
1145  * We are done with the initial data synchronization, update the state.
1146  */
1148  MyLogicalRepWorker->relstate = SUBREL_STATE_SYNCWAIT;
1149  MyLogicalRepWorker->relstate_lsn = *origin_startpos;
1151 
1152  /*
1153  * Finally, wait until the main apply worker tells us to catch up and then
1154  * return to let LogicalRepApplyLoop do it.
1155  */
1156  wait_for_worker_state_change(SUBREL_STATE_CATCHUP);
1157  return slotname;
1158 }
1159 
1160 /*
1161  * Common code to fetch the up-to-date sync state info into the static lists.
1162  *
1163  * Returns true if subscription has 1 or more tables, else false.
1164  *
1165  * Note: If this function started the transaction (indicated by the parameter)
1166  * then it is the caller's responsibility to commit it.
1167  */
1168 static bool
1169 FetchTableStates(bool *started_tx)
1170 {
1171  static bool has_subrels = false;
1172 
1173  *started_tx = false;
1174 
1175  if (!table_states_valid)
1176  {
1177  MemoryContext oldctx;
1178  List *rstates;
1179  ListCell *lc;
1180  SubscriptionRelState *rstate;
1181 
1182  /* Clean the old lists. */
1183  list_free_deep(table_states_not_ready);
1184  table_states_not_ready = NIL;
1185 
1186  if (!IsTransactionState())
1187  {
1189  *started_tx = true;
1190  }
1191 
1192  /* Fetch all non-ready tables. */
1194 
1195  /* Allocate the tracking info in a permanent memory context. */
1197  foreach(lc, rstates)
1198  {
1199  rstate = palloc(sizeof(SubscriptionRelState));
1200  memcpy(rstate, lfirst(lc), sizeof(SubscriptionRelState));
1201  table_states_not_ready = lappend(table_states_not_ready, rstate);
1202  }
1203  MemoryContextSwitchTo(oldctx);
1204 
1205  /*
1206  * Does the subscription have tables?
1207  *
1208  * If there were not-READY relations found then we know it does. But
1209  * if table_state_not_ready was empty we still need to check again to
1210  * see if there are 0 tables.
1211  */
1212  has_subrels = (list_length(table_states_not_ready) > 0) ||
1214 
1215  table_states_valid = true;
1216  }
1217 
1218  return has_subrels;
1219 }
1220 
1221 /*
1222  * If the subscription has no tables then return false.
1223  *
1224  * Otherwise, are all tablesyncs READY?
1225  *
1226  * Note: This function is not suitable to be called from outside of apply or
1227  * tablesync workers because MySubscription needs to be already initialized.
1228  */
1229 bool
1231 {
1232  bool started_tx = false;
1233  bool has_subrels = false;
1234 
1235  /* We need up-to-date sync state info for subscription tables here. */
1236  has_subrels = FetchTableStates(&started_tx);
1237 
1238  if (started_tx)
1239  {
1241  pgstat_report_stat(false);
1242  }
1243 
1244  /*
1245  * Return false when there are no tables in subscription or not all tables
1246  * are in ready state; true otherwise.
1247  */
1248  return has_subrels && list_length(table_states_not_ready) == 0;
1249 }
1250 
1251 /*
1252  * Update the two_phase state of the specified subscription in pg_subscription.
1253  */
1254 void
1255 UpdateTwoPhaseState(Oid suboid, char new_state)
1256 {
1257  Relation rel;
1258  HeapTuple tup;
1259  bool nulls[Natts_pg_subscription];
1260  bool replaces[Natts_pg_subscription];
1261  Datum values[Natts_pg_subscription];
1262 
1264  new_state == LOGICALREP_TWOPHASE_STATE_PENDING ||
1265  new_state == LOGICALREP_TWOPHASE_STATE_ENABLED);
1266 
1267  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
1269  if (!HeapTupleIsValid(tup))
1270  elog(ERROR,
1271  "cache lookup failed for subscription oid %u",
1272  suboid);
1273 
1274  /* Form a new tuple. */
1275  memset(values, 0, sizeof(values));
1276  memset(nulls, false, sizeof(nulls));
1277  memset(replaces, false, sizeof(replaces));
1278 
1279  /* And update/set two_phase state */
1280  values[Anum_pg_subscription_subtwophasestate - 1] = CharGetDatum(new_state);
1281  replaces[Anum_pg_subscription_subtwophasestate - 1] = true;
1282 
1283  tup = heap_modify_tuple(tup, RelationGetDescr(rel),
1284  values, nulls, replaces);
1285  CatalogTupleUpdate(rel, &tup->t_self, tup);
1286 
1287  heap_freetuple(tup);
1289 }
Subscription * MySubscription
Definition: worker.c:247
#define NIL
Definition: pg_list.h:65
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define walrcv_endstreaming(conn, next_tli)
Definition: walreceiver.h:420
#define DEBUG1
Definition: elog.h:25
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:245
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
uint32 TimeLineID
Definition: xlogdefs.h:59
#define HASH_ELEM
Definition: hsearch.h:95
#define WL_TIMEOUT
Definition: latch.h:128
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
static bool FetchTableStates(bool *started_tx)
Definition: tablesync.c:1169
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
#define RelationGetDescr(relation)
Definition: rel.h:503
void process_syncing_tables(XLogRecPtr current_lsn)
Definition: tablesync.c:587
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
#define LOGICALREP_TWOPHASE_STATE_DISABLED
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:200
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
void logicalrep_worker_wakeup(Oid subid, Oid relid)
Definition: launcher.c:534
int64 TimestampTz
Definition: timestamp.h:39
static void process_syncing_tables_for_apply(XLogRecPtr current_lsn)
Definition: tablesync.c:362
uint64 CopyFrom(CopyFromState cstate)
Definition: copyfrom.c:525
#define DatumGetObjectId(X)
Definition: postgres.h:544
void CommitTransactionCommand(void)
Definition: xact.c:2949
RepOriginId replorigin_by_name(const char *roname, bool missing_ok)
Definition: origin.c:209
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
#define walrcv_receive(conn, buffer, wait_fd)
Definition: walreceiver.h:422
#define walrcv_server_version(conn)
Definition: walreceiver.h:414
void replorigin_drop_by_name(const char *name, bool missing_ok, bool nowait)
Definition: origin.c:414
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
uint16 RepOriginId
Definition: xlogdefs.h:65
Size entrysize
Definition: hsearch.h:76
static void copy_table(Relation rel)
Definition: tablesync.c:808
void proc_exit(int code)
Definition: ipc.c:104
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1206
int errcode(int sqlerrcode)
Definition: elog.c:698
static bool wait_for_relation_state_change(Oid relid, char expected_state)
Definition: tablesync.c:171
RepOriginId replorigin_create(const char *roname)
Definition: origin.c:240
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:433
#define WL_SOCKET_READABLE
Definition: latch.h:126
String * makeString(char *str)
Definition: value.c:51
void PopActiveSnapshot(void)
Definition: snapmgr.c:774
void replorigin_advance(RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
Definition: origin.c:872
void pgstat_report_stat(bool disconnect)
Definition: pgstat.c:858
#define lengthof(array)
Definition: c.h:734
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1071
#define LOG
Definition: elog.h:26
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
static bool table_states_valid
Definition: tablesync.c:120
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:920
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1711
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
#define OidIsValid(objectId)
Definition: c.h:710
void list_free_deep(List *list)
Definition: list.c:1405
Bitmapset * attkeys
Definition: logicalproto.h:109
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2887
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
LogicalRepRelId remoteid
Definition: logicalproto.h:101
XLogRecPtr relstate_lsn
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
#define LOGICALREP_TWOPHASE_STATE_ENABLED
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:133
#define NAMEDATALEN
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:554
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:157
#define SpinLockAcquire(lock)
Definition: spin.h:62
Definition: dynahash.c:219
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
bool HasSubscriptionRelations(Oid subid)
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:436
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
TupleDesc tupledesc
Definition: walreceiver.h:220
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
LogicalRepRelation remoterel
int max_sync_workers_per_subscription
Definition: launcher.c:55
ItemPointerData t_self
Definition: htup.h:65
static bool am_tablesync_worker(void)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:68
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
#define RowExclusiveLock
Definition: lockdefs.h:38
#define LOGICALREP_TWOPHASE_STATE_PENDING
#define DatumGetBool(X)
Definition: postgres.h:437
#define RelationGetRelationName(relation)
Definition: rel.h:511
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
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:500
unsigned int uint32
Definition: c.h:441
int pgsocket
Definition: port.h:31
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:258
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:456
List * lappend(List *list, void *datum)
Definition: list.c:336
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:11458
static List * table_states_not_ready
Definition: tablesync.c:121
int wal_retrieve_retry_interval
Definition: xlog.c:111
#define SpinLockRelease(lock)
Definition: spin.h:64
#define HASH_BLOBS
Definition: hsearch.h:97
#define TextDatumGetCString(d)
Definition: builtins.h:87
static void pg_attribute_noreturn()
Definition: tablesync.c:130
void * palloc0(Size size)
Definition: mcxt.c:1093
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
#define PGINVALID_SOCKET
Definition: port.h:33
void UpdateTwoPhaseState(Oid suboid, char new_state)
Definition: tablesync.c:1255
#define DatumGetChar(X)
Definition: postgres.h:453
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
Size keysize
Definition: hsearch.h:75
StringInfo copybuf
Definition: tablesync.c:124
static void fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel)
Definition: tablesync.c:694
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:664
#define InvalidOid
Definition: postgres_ext.h:36
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define ereport(elevel,...)
Definition: elog.h:157
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11962
#define Max(x, y)
Definition: c.h:980
CopyFromState BeginCopyFrom(ParseState *pstate, Relation rel, Node *whereClause, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
Definition: copyfrom.c:1186
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Tuplestorestate * tuplestore
Definition: walreceiver.h:219
bool AllTablesyncsReady(void)
Definition: tablesync.c:1230
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:266
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
WalRcvExecStatus status
Definition: walreceiver.h:216
RepOriginId replorigin_session_origin
Definition: origin.c:154
Definition: regguts.h:317
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:215
void StartTransactionCommand(void)
Definition: xact.c:2848
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
static int list_length(const List *l)
Definition: pg_list.h:149
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool IsTransactionState(void)
Definition: xact.c:371
void ReplicationOriginNameForTablesync(Oid suboid, Oid relid, char *originname, int szorgname)
Definition: tablesync.c:905
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define CharGetDatum(X)
Definition: postgres.h:460
static void process_syncing_tables_for_sync(XLogRecPtr current_lsn)
Definition: tablesync.c:282
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4998
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:131
#define elog(elevel,...)
Definition: elog.h:232
int i
void * arg
struct Latch * MyLatch
Definition: globals.c:57
static int copy_read_data(void *outbuf, int minread, int maxread)
Definition: tablesync.c:619
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:109
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static List * make_copy_attnamelist(LogicalRepRelMapEntry *rel)
Definition: tablesync.c:599
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: pg_list.h:50
List * GetSubscriptionNotReadyRelations(Oid subid)
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899
#define snprintf
Definition: port.h:217
#define WL_LATCH_SET
Definition: latch.h:125
static bool wait_for_worker_state_change(char expected_state)
Definition: tablesync.c:219
#define UINT64_FORMAT
Definition: c.h:484
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:430
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:268
void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, int szslot)
Definition: tablesync.c:892
MemoryContext CacheMemoryContext
Definition: mcxt.c:51
#define RelationGetNamespace(relation)
Definition: rel.h:518
#define walrcv_create_slot(conn, slotname, temporary, two_phase, snapshot_action, lsn)
Definition: walreceiver.h:426
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:404