PostgreSQL Source Code  git master
tablesync.c File Reference
Include dependency graph for tablesync.c:

Go to the source code of this file.

Functions

static void pg_attribute_noreturn ()
 
static bool wait_for_relation_state_change (Oid relid, char expected_state)
 
static bool wait_for_worker_state_change (char expected_state)
 
void invalidate_syncing_table_states (Datum arg, int cacheid, uint32 hashvalue)
 
static void process_syncing_tables_for_sync (XLogRecPtr current_lsn)
 
static void process_syncing_tables_for_apply (XLogRecPtr current_lsn)
 
void process_syncing_tables (XLogRecPtr current_lsn)
 
static Listmake_copy_attnamelist (LogicalRepRelMapEntry *rel)
 
static int copy_read_data (void *outbuf, int minread, int maxread)
 
static void fetch_remote_table_info (char *nspname, char *relname, LogicalRepRelation *lrel)
 
static void copy_table (Relation rel)
 
char * LogicalRepSyncTableStart (XLogRecPtr *origin_startpos)
 

Variables

static bool table_states_valid = false
 
StringInfo copybuf = NULL
 

Function Documentation

◆ copy_read_data()

static int copy_read_data ( void *  outbuf,
int  minread,
int  maxread 
)
static

Definition at line 561 of file tablesync.c.

References buf, CHECK_FOR_INTERRUPTS, StringInfoData::cursor, StringInfoData::data, fd(), StringInfoData::len, MyLatch, PGINVALID_SOCKET, ResetLatch(), WAIT_EVENT_LOGICAL_SYNC_DATA, WaitLatchOrSocket(), walrcv_receive, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_SOCKET_READABLE, WL_TIMEOUT, and wrconn.

Referenced by copy_table().

562 {
563  int bytesread = 0;
564  int avail;
565 
566  /* If there are some leftover data from previous read, use it. */
567  avail = copybuf->len - copybuf->cursor;
568  if (avail)
569  {
570  if (avail > maxread)
571  avail = maxread;
572  memcpy(outbuf, &copybuf->data[copybuf->cursor], avail);
573  copybuf->cursor += avail;
574  maxread -= avail;
575  bytesread += avail;
576  }
577 
578  while (maxread > 0 && bytesread < minread)
579  {
581  int len;
582  char *buf = NULL;
583 
584  for (;;)
585  {
586  /* Try read the data. */
587  len = walrcv_receive(wrconn, &buf, &fd);
588 
590 
591  if (len == 0)
592  break;
593  else if (len < 0)
594  return bytesread;
595  else
596  {
597  /* Process the data */
598  copybuf->data = buf;
599  copybuf->len = len;
600  copybuf->cursor = 0;
601 
602  avail = copybuf->len - copybuf->cursor;
603  if (avail > maxread)
604  avail = maxread;
605  memcpy(outbuf, &copybuf->data[copybuf->cursor], avail);
606  outbuf = (void *) ((char *) outbuf + avail);
607  copybuf->cursor += avail;
608  maxread -= avail;
609  bytesread += avail;
610  }
611 
612  if (maxread <= 0 || bytesread >= minread)
613  return bytesread;
614  }
615 
616  /*
617  * Wait for more data or latch.
618  */
619  (void) WaitLatchOrSocket(MyLatch,
622  fd, 1000L, WAIT_EVENT_LOGICAL_SYNC_DATA);
623 
625  }
626 
627  return bytesread;
628 }
WalReceiverConn * wrconn
Definition: worker.c:105
#define WL_TIMEOUT
Definition: latch.h:127
#define walrcv_receive(conn, buffer, wait_fd)
Definition: walreceiver.h:415
#define WL_SOCKET_READABLE
Definition: latch.h:125
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void ResetLatch(Latch *latch)
Definition: latch.c:588
static char * buf
Definition: pg_test_fsync.c:67
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:438
int pgsocket
Definition: port.h:31
#define PGINVALID_SOCKET
Definition: port.h:33
StringInfo copybuf
Definition: tablesync.c:108
struct Latch * MyLatch
Definition: globals.c:54
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define WL_LATCH_SET
Definition: latch.h:124
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129

◆ copy_table()

static void copy_table ( Relation  rel)
static

Definition at line 745 of file tablesync.c.

References AccessShareLock, addRangeTableEntryForRelation(), appendStringInfo(), appendStringInfoString(), Assert, LogicalRepRelation::attnames, BeginCopyFrom(), copy_read_data(), CopyFrom(), StringInfoData::data, ereport, WalRcvExecResult::err, errmsg(), ERROR, fetch_remote_table_info(), get_namespace_name(), i, initStringInfo(), LogicalRepRelMapEntry::localrel, logicalrep_rel_close(), logicalrep_rel_open(), logicalrep_relmap_update(), make_copy_attnamelist(), make_parsestate(), makeStringInfo(), LogicalRepRelation::natts, NIL, NoLock, LogicalRepRelation::nspname, pfree(), quote_identifier(), quote_qualified_identifier(), RelationGetNamespace, RelationGetRelationName, LogicalRepRelation::relkind, LogicalRepRelation::relname, LogicalRepRelation::remoteid, WalRcvExecResult::status, walrcv_clear_result(), walrcv_exec, WALRCV_OK_COPY_OUT, and wrconn.

Referenced by LogicalRepSyncTableStart().

746 {
747  LogicalRepRelMapEntry *relmapentry;
748  LogicalRepRelation lrel;
749  WalRcvExecResult *res;
750  StringInfoData cmd;
751  CopyState cstate;
752  List *attnamelist;
753  ParseState *pstate;
754 
755  /* Get the publisher relation info. */
757  RelationGetRelationName(rel), &lrel);
758 
759  /* Put the relation into relmap. */
761 
762  /* Map the publisher relation to local one. */
763  relmapentry = logicalrep_rel_open(lrel.remoteid, NoLock);
764  Assert(rel == relmapentry->localrel);
765 
766  /* Start copy on the publisher. */
767  initStringInfo(&cmd);
768  if (lrel.relkind == RELKIND_RELATION)
769  appendStringInfo(&cmd, "COPY %s TO STDOUT",
771  else
772  {
773  /*
774  * For non-tables, we need to do COPY (SELECT ...), but we can't just
775  * do SELECT * because we need to not copy generated columns.
776  */
777  appendStringInfo(&cmd, "COPY (SELECT ");
778  for (int i = 0; i < lrel.natts; i++)
779  {
781  if (i < lrel.natts - 1)
782  appendStringInfoString(&cmd, ", ");
783  }
784  appendStringInfo(&cmd, " FROM %s) TO STDOUT",
786  }
787  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
788  pfree(cmd.data);
789  if (res->status != WALRCV_OK_COPY_OUT)
790  ereport(ERROR,
791  (errmsg("could not start initial contents copy for table \"%s.%s\": %s",
792  lrel.nspname, lrel.relname, res->err)));
793  walrcv_clear_result(res);
794 
796 
797  pstate = make_parsestate(NULL);
799  NULL, false, false);
800 
801  attnamelist = make_copy_attnamelist(relmapentry);
802  cstate = BeginCopyFrom(pstate, rel, NULL, false, copy_read_data, attnamelist, NIL);
803 
804  /* Do the copy */
805  (void) CopyFrom(cstate);
806 
807  logicalrep_rel_close(relmapentry, NoLock);
808 }
#define NIL
Definition: pg_list.h:65
WalReceiverConn * wrconn
Definition: worker.c:105
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10727
CopyState BeginCopyFrom(ParseState *pstate, Relation rel, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
Definition: copy.c:3383
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
#define AccessShareLock
Definition: lockdefs.h:36
uint64 CopyFrom(CopyState cstate)
Definition: copy.c:2702
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:399
LogicalRepRelId remoteid
Definition: logicalproto.h:57
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:43
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:172
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:429
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
#define NoLock
Definition: lockdefs.h:34
#define RelationGetRelationName(relation)
Definition: rel.h:490
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:237
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10811
StringInfo copybuf
Definition: tablesync.c:108
static void fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel)
Definition: tablesync.c:636
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
WalRcvExecStatus status
Definition: walreceiver.h:211
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
static int copy_read_data(void *outbuf, int minread, int maxread)
Definition: tablesync.c:561
static List * make_copy_attnamelist(LogicalRepRelMapEntry *rel)
Definition: tablesync.c:541
Definition: pg_list.h:50
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:423
#define RelationGetNamespace(relation)
Definition: rel.h:497

◆ fetch_remote_table_info()

static void fetch_remote_table_info ( char *  nspname,
char *  relname,
LogicalRepRelation lrel 
)
static

Definition at line 636 of file tablesync.c.

References appendStringInfo(), Assert, LogicalRepRelation::attkeys, LogicalRepRelation::attnames, LogicalRepRelation::atttyps, bms_add_member(), StringInfoData::data, DatumGetBool, DatumGetChar, DatumGetObjectId, elog, ereport, WalRcvExecResult::err, errmsg(), ERROR, ExecClearTuple(), ExecDropSingleTupleTableSlot(), initStringInfo(), lengthof, MakeSingleTupleTableSlot(), MaxTupleAttributeNumber, LogicalRepRelation::natts, LogicalRepRelation::nspname, palloc0(), pfree(), quote_literal_cstr(), LogicalRepRelation::relkind, relname, LogicalRepRelation::relname, LogicalRepRelation::remoteid, LogicalRepRelation::replident, resetStringInfo(), slot_getattr(), WalRcvExecResult::status, TextDatumGetCString, TTSOpsMinimalTuple, WalRcvExecResult::tupledesc, WalRcvExecResult::tuplestore, tuplestore_gettupleslot(), walrcv_clear_result(), walrcv_exec, WALRCV_OK_TUPLES, walrcv_server_version, and wrconn.

Referenced by copy_table().

638 {
639  WalRcvExecResult *res;
640  StringInfoData cmd;
641  TupleTableSlot *slot;
642  Oid tableRow[] = {OIDOID, CHAROID, CHAROID};
643  Oid attrRow[] = {TEXTOID, OIDOID, INT4OID, BOOLOID};
644  bool isnull;
645  int natt;
646 
647  lrel->nspname = nspname;
648  lrel->relname = relname;
649 
650  /* First fetch Oid and replica identity. */
651  initStringInfo(&cmd);
652  appendStringInfo(&cmd, "SELECT c.oid, c.relreplident, c.relkind"
653  " FROM pg_catalog.pg_class c"
654  " INNER JOIN pg_catalog.pg_namespace n"
655  " ON (c.relnamespace = n.oid)"
656  " WHERE n.nspname = %s"
657  " AND c.relname = %s",
658  quote_literal_cstr(nspname),
660  res = walrcv_exec(wrconn, cmd.data, lengthof(tableRow), tableRow);
661 
662  if (res->status != WALRCV_OK_TUPLES)
663  ereport(ERROR,
664  (errmsg("could not fetch table info for table \"%s.%s\" from publisher: %s",
665  nspname, relname, res->err)));
666 
668  if (!tuplestore_gettupleslot(res->tuplestore, true, false, slot))
669  ereport(ERROR,
670  (errmsg("table \"%s.%s\" not found on publisher",
671  nspname, relname)));
672 
673  lrel->remoteid = DatumGetObjectId(slot_getattr(slot, 1, &isnull));
674  Assert(!isnull);
675  lrel->replident = DatumGetChar(slot_getattr(slot, 2, &isnull));
676  Assert(!isnull);
677  lrel->relkind = DatumGetChar(slot_getattr(slot, 3, &isnull));
678  Assert(!isnull);
679 
681  walrcv_clear_result(res);
682 
683  /* Now fetch columns. */
684  resetStringInfo(&cmd);
685  appendStringInfo(&cmd,
686  "SELECT a.attname,"
687  " a.atttypid,"
688  " a.atttypmod,"
689  " a.attnum = ANY(i.indkey)"
690  " FROM pg_catalog.pg_attribute a"
691  " LEFT JOIN pg_catalog.pg_index i"
692  " ON (i.indexrelid = pg_get_replica_identity_index(%u))"
693  " WHERE a.attnum > 0::pg_catalog.int2"
694  " AND NOT a.attisdropped %s"
695  " AND a.attrelid = %u"
696  " ORDER BY a.attnum",
697  lrel->remoteid,
698  (walrcv_server_version(wrconn) >= 120000 ? "AND a.attgenerated = ''" : ""),
699  lrel->remoteid);
700  res = walrcv_exec(wrconn, cmd.data, lengthof(attrRow), attrRow);
701 
702  if (res->status != WALRCV_OK_TUPLES)
703  ereport(ERROR,
704  (errmsg("could not fetch table info for table \"%s.%s\": %s",
705  nspname, relname, res->err)));
706 
707  /* We don't know the number of rows coming, so allocate enough space. */
708  lrel->attnames = palloc0(MaxTupleAttributeNumber * sizeof(char *));
709  lrel->atttyps = palloc0(MaxTupleAttributeNumber * sizeof(Oid));
710  lrel->attkeys = NULL;
711 
712  natt = 0;
714  while (tuplestore_gettupleslot(res->tuplestore, true, false, slot))
715  {
716  lrel->attnames[natt] =
717  TextDatumGetCString(slot_getattr(slot, 1, &isnull));
718  Assert(!isnull);
719  lrel->atttyps[natt] = DatumGetObjectId(slot_getattr(slot, 2, &isnull));
720  Assert(!isnull);
721  if (DatumGetBool(slot_getattr(slot, 4, &isnull)))
722  lrel->attkeys = bms_add_member(lrel->attkeys, natt);
723 
724  /* Should never happen. */
725  if (++natt >= MaxTupleAttributeNumber)
726  elog(ERROR, "too many columns in remote table \"%s.%s\"",
727  nspname, relname);
728 
729  ExecClearTuple(slot);
730  }
732 
733  lrel->natts = natt;
734 
735  walrcv_clear_result(res);
736  pfree(cmd.data);
737 }
WalReceiverConn * wrconn
Definition: worker.c:105
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
#define DatumGetObjectId(X)
Definition: postgres.h:500
#define walrcv_server_version(conn)
Definition: walreceiver.h:407
#define lengthof(array)
Definition: c.h:675
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:65
LogicalRepRelId remoteid
Definition: logicalproto.h:57
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:429
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
TupleDesc tupledesc
Definition: walreceiver.h:214
#define ERROR
Definition: elog.h:43
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
#define DatumGetBool(X)
Definition: postgres.h:393
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define TextDatumGetCString(d)
Definition: builtins.h:88
void * palloc0(Size size)
Definition: mcxt.c:980
#define DatumGetChar(X)
Definition: postgres.h:409
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define ereport(elevel,...)
Definition: elog.h:144
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
Tuplestorestate * tuplestore
Definition: walreceiver.h:213
#define Assert(condition)
Definition: c.h:745
WalRcvExecStatus status
Definition: walreceiver.h:211
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:423

◆ invalidate_syncing_table_states()

void invalidate_syncing_table_states ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)

Definition at line 256 of file tablesync.c.

References table_states_valid.

Referenced by ApplyWorkerMain().

257 {
258  table_states_valid = false;
259 }
static bool table_states_valid
Definition: tablesync.c:106

◆ LogicalRepSyncTableStart()

char* LogicalRepSyncTableStart ( XLogRecPtr origin_startpos)

Definition at line 816 of file tablesync.c.

References CommandCounterIncrement(), CommitTransactionCommand(), Subscription::conninfo, copy_table(), CRS_USE_SNAPSHOT, elog, ereport, WalRcvExecResult::err, errdetail(), errmsg(), ERROR, GetSubscriptionRelState(), GetTransactionSnapshot(), InvalidXLogRecPtr, MyLogicalRepWorker, MySubscription, NAMEDATALEN, NoLock, Subscription::oid, pgstat_report_stat(), PopActiveSnapshot(), psprintf(), PushActiveSnapshot(), LogicalRepWorker::relid, LogicalRepWorker::relmutex, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, RowExclusiveLock, Subscription::slotname, SpinLockAcquire, SpinLockRelease, StartTransactionCommand(), StaticAssertStmt, WalRcvExecResult::status, LogicalRepWorker::subid, table_close(), table_open(), UpdateSubscriptionRelState(), wait_for_worker_state_change(), walrcv_clear_result(), walrcv_connect, walrcv_create_slot, walrcv_exec, WALRCV_OK_COMMAND, and wrconn.

Referenced by ApplyWorkerMain().

817 {
818  char *slotname;
819  char *err;
820  char relstate;
821  XLogRecPtr relstate_lsn;
822 
823  /* Check the state of the table synchronization. */
827  &relstate_lsn, true);
829 
831  MyLogicalRepWorker->relstate = relstate;
832  MyLogicalRepWorker->relstate_lsn = relstate_lsn;
834 
835  /*
836  * To build a slot name for the sync work, we are limited to NAMEDATALEN -
837  * 1 characters. We cut the original slot name to NAMEDATALEN - 28 chars
838  * and append _%u_sync_%u (1 + 10 + 6 + 10 + '\0'). (It's actually the
839  * NAMEDATALEN on the remote that matters, but this scheme will also work
840  * reasonably if that is different.)
841  */
842  StaticAssertStmt(NAMEDATALEN >= 32, "NAMEDATALEN too small"); /* for sanity */
843  slotname = psprintf("%.*s_%u_sync_%u",
844  NAMEDATALEN - 28,
848 
849  /*
850  * Here we use the slot name instead of the subscription name as the
851  * application_name, so that it is different from the main apply worker,
852  * so that synchronous replication can distinguish them.
853  */
854  wrconn = walrcv_connect(MySubscription->conninfo, true, slotname, &err);
855  if (wrconn == NULL)
856  ereport(ERROR,
857  (errmsg("could not connect to the publisher: %s", err)));
858 
859  switch (MyLogicalRepWorker->relstate)
860  {
861  case SUBREL_STATE_INIT:
862  case SUBREL_STATE_DATASYNC:
863  {
864  Relation rel;
865  WalRcvExecResult *res;
866 
868  MyLogicalRepWorker->relstate = SUBREL_STATE_DATASYNC;
871 
872  /* Update the state and make it visible to others. */
879  pgstat_report_stat(false);
880 
881  /*
882  * We want to do the table data sync in a single transaction.
883  */
885 
886  /*
887  * Use a standard write lock here. It might be better to
888  * disallow access to the table while it's being synchronized.
889  * But we don't want to block the main apply process from
890  * working and it has to open the relation in RowExclusiveLock
891  * when remapping remote relation id to local one.
892  */
894 
895  /*
896  * Create a temporary slot for the sync process. We do this
897  * inside the transaction so that we can use the snapshot made
898  * by the slot to get existing data.
899  */
900  res = walrcv_exec(wrconn,
901  "BEGIN READ ONLY ISOLATION LEVEL "
902  "REPEATABLE READ", 0, NULL);
903  if (res->status != WALRCV_OK_COMMAND)
904  ereport(ERROR,
905  (errmsg("table copy could not start transaction on publisher"),
906  errdetail("The error was: %s", res->err)));
907  walrcv_clear_result(res);
908 
909  /*
910  * Create new temporary logical decoding slot.
911  *
912  * We'll use slot for data copy so make sure the snapshot is
913  * used for the transaction; that way the COPY will get data
914  * that is consistent with the lsn used by the slot to start
915  * decoding.
916  */
917  walrcv_create_slot(wrconn, slotname, true,
918  CRS_USE_SNAPSHOT, origin_startpos);
919 
921  copy_table(rel);
923 
924  res = walrcv_exec(wrconn, "COMMIT", 0, NULL);
925  if (res->status != WALRCV_OK_COMMAND)
926  ereport(ERROR,
927  (errmsg("table copy could not finish transaction on publisher"),
928  errdetail("The error was: %s", res->err)));
929  walrcv_clear_result(res);
930 
931  table_close(rel, NoLock);
932 
933  /* Make the copy visible. */
935 
936  /*
937  * We are done with the initial data synchronization, update
938  * the state.
939  */
941  MyLogicalRepWorker->relstate = SUBREL_STATE_SYNCWAIT;
942  MyLogicalRepWorker->relstate_lsn = *origin_startpos;
944 
945  /* Wait for main apply worker to tell us to catchup. */
946  wait_for_worker_state_change(SUBREL_STATE_CATCHUP);
947 
948  /*----------
949  * There are now two possible states here:
950  * a) Sync is behind the apply. If that's the case we need to
951  * catch up with it by consuming the logical replication
952  * stream up to the relstate_lsn. For that, we exit this
953  * function and continue in ApplyWorkerMain().
954  * b) Sync is caught up with the apply. So it can just set
955  * the state to SYNCDONE and finish.
956  *----------
957  */
958  if (*origin_startpos >= MyLogicalRepWorker->relstate_lsn)
959  {
960  /*
961  * Update the new state in catalog. No need to bother
962  * with the shmem state as we are exiting for good.
963  */
966  SUBREL_STATE_SYNCDONE,
967  *origin_startpos);
968  finish_sync_worker();
969  }
970  break;
971  }
972  case SUBREL_STATE_SYNCDONE:
973  case SUBREL_STATE_READY:
974  case SUBREL_STATE_UNKNOWN:
975 
976  /*
977  * Nothing to do here but finish. (UNKNOWN means the relation was
978  * removed from pg_subscription_rel before the sync worker could
979  * start.)
980  */
981  finish_sync_worker();
982  break;
983  default:
984  elog(ERROR, "unknown relation state \"%c\"",
986  }
987 
988  return slotname;
989 }
Subscription * MySubscription
Definition: worker.c:107
WalReceiverConn * wrconn
Definition: worker.c:105
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void CommitTransactionCommand(void)
Definition: xact.c:2947
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void copy_table(Relation rel)
Definition: tablesync.c:745
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
#define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
Definition: walreceiver.h:419
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:306
XLogRecPtr relstate_lsn
#define NAMEDATALEN
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:859
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:429
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:735
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define SpinLockRelease(lock)
Definition: spin.h:64
void CommandCounterIncrement(void)
Definition: xact.c:1021
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
#define ereport(elevel,...)
Definition: elog.h:144
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalRcvExecStatus status
Definition: walreceiver.h:211
void StartTransactionCommand(void)
Definition: xact.c:2846
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
static bool wait_for_worker_state_change(char expected_state)
Definition: tablesync.c:207
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:423
void pgstat_report_stat(bool force)
Definition: pgstat.c:839
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:397

◆ make_copy_attnamelist()

static List* make_copy_attnamelist ( LogicalRepRelMapEntry rel)
static

Definition at line 541 of file tablesync.c.

References LogicalRepRelation::attnames, i, lappend(), makeString(), LogicalRepRelation::natts, NIL, and LogicalRepRelMapEntry::remoterel.

Referenced by copy_table().

542 {
543  List *attnamelist = NIL;
544  int i;
545 
546  for (i = 0; i < rel->remoterel.natts; i++)
547  {
548  attnamelist = lappend(attnamelist,
549  makeString(rel->remoterel.attnames[i]));
550  }
551 
552 
553  return attnamelist;
554 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
LogicalRepRelation remoterel
List * lappend(List *list, void *datum)
Definition: list.c:321
int i
Definition: pg_list.h:50

◆ pg_attribute_noreturn()

static void pg_attribute_noreturn ( )
static

Definition at line 114 of file tablesync.c.

References CommitTransactionCommand(), ereport, errmsg(), get_rel_name(), GetXLogWriteRecPtr(), InvalidOid, IsTransactionState(), LOG, logicalrep_worker_wakeup(), MyLogicalRepWorker, MySubscription, Subscription::name, pgstat_report_stat(), proc_exit(), LogicalRepWorker::relid, StartTransactionCommand(), LogicalRepWorker::subid, and XLogFlush().

116 {
117  /*
118  * Commit any outstanding transaction. This is the usual case, unless
119  * there was nothing to do for the table.
120  */
121  if (IsTransactionState())
122  {
124  pgstat_report_stat(false);
125  }
126 
127  /* And flush all writes. */
129 
131  ereport(LOG,
132  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has finished",
136 
137  /* Find the main apply worker and signal it. */
139 
140  /* Stop gracefully */
141  proc_exit(0);
142 }
Subscription * MySubscription
Definition: worker.c:107
void logicalrep_worker_wakeup(Oid subid, Oid relid)
Definition: launcher.c:594
void CommitTransactionCommand(void)
Definition: xact.c:2947
void proc_exit(int code)
Definition: ipc.c:104
#define LOG
Definition: elog.h:26
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2844
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11510
void StartTransactionCommand(void)
Definition: xact.c:2846
bool IsTransactionState(void)
Definition: xact.c:371
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1840
void pgstat_report_stat(bool force)
Definition: pgstat.c:839

◆ process_syncing_tables()

void process_syncing_tables ( XLogRecPtr  current_lsn)

Definition at line 529 of file tablesync.c.

References am_tablesync_worker(), process_syncing_tables_for_apply(), and process_syncing_tables_for_sync().

Referenced by apply_handle_commit(), and LogicalRepApplyLoop().

530 {
531  if (am_tablesync_worker())
532  process_syncing_tables_for_sync(current_lsn);
533  else
535 }
static void process_syncing_tables_for_apply(XLogRecPtr current_lsn)
Definition: tablesync.c:320
static bool am_tablesync_worker(void)
static void process_syncing_tables_for_sync(XLogRecPtr current_lsn)
Definition: tablesync.c:270

◆ process_syncing_tables_for_apply()

static void process_syncing_tables_for_apply ( XLogRecPtr  current_lsn)
static

Definition at line 320 of file tablesync.c.

References Assert, CacheMemoryContext, CommitTransactionCommand(), LogicalRepWorker::dbid, HASHCTL::entrysize, GetCurrentTimestamp(), GetSubscriptionNotReadyRelations(), HASH_BLOBS, hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, hash_search(), IsTransactionState(), HASHCTL::keysize, lappend(), lfirst, list_free_deep(), logicalrep_sync_worker_count(), logicalrep_worker_find(), logicalrep_worker_launch(), logicalrep_worker_wakeup_ptr(), SubscriptionRelState::lsn, LW_SHARED, LWLockAcquire(), LWLockRelease(), Max, max_sync_workers_per_subscription, MemoryContextSwitchTo(), MyLogicalRepWorker, MySubscription, Subscription::name, NIL, now(), Subscription::oid, palloc(), pgstat_report_stat(), LogicalRepWorker::proc, SubscriptionRelState::relid, LogicalRepWorker::relmutex, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, SpinLockAcquire, SpinLockRelease, StartTransactionCommand(), SubscriptionRelState::state, LogicalRepWorker::subid, table_states_valid, TimestampDifferenceExceeds(), UpdateSubscriptionRelState(), LogicalRepWorker::userid, wait_for_relation_state_change(), and wal_retrieve_retry_interval.

Referenced by process_syncing_tables().

321 {
322  struct tablesync_start_time_mapping
323  {
324  Oid relid;
325  TimestampTz last_start_time;
326  };
327  static List *table_states = NIL;
328  static HTAB *last_start_times = NULL;
329  ListCell *lc;
330  bool started_tx = false;
331 
333 
334  /* We need up-to-date sync state info for subscription tables here. */
335  if (!table_states_valid)
336  {
337  MemoryContext oldctx;
338  List *rstates;
339  ListCell *lc;
340  SubscriptionRelState *rstate;
341 
342  /* Clean the old list. */
343  list_free_deep(table_states);
344  table_states = NIL;
345 
347  started_tx = true;
348 
349  /* Fetch all non-ready tables. */
351 
352  /* Allocate the tracking info in a permanent memory context. */
354  foreach(lc, rstates)
355  {
356  rstate = palloc(sizeof(SubscriptionRelState));
357  memcpy(rstate, lfirst(lc), sizeof(SubscriptionRelState));
358  table_states = lappend(table_states, rstate);
359  }
360  MemoryContextSwitchTo(oldctx);
361 
362  table_states_valid = true;
363  }
364 
365  /*
366  * Prepare a hash table for tracking last start times of workers, to avoid
367  * immediate restarts. We don't need it if there are no tables that need
368  * syncing.
369  */
370  if (table_states && !last_start_times)
371  {
372  HASHCTL ctl;
373 
374  memset(&ctl, 0, sizeof(ctl));
375  ctl.keysize = sizeof(Oid);
376  ctl.entrysize = sizeof(struct tablesync_start_time_mapping);
377  last_start_times = hash_create("Logical replication table sync worker start times",
378  256, &ctl, HASH_ELEM | HASH_BLOBS);
379  }
380 
381  /*
382  * Clean up the hash table when we're done with all tables (just to
383  * release the bit of memory).
384  */
385  else if (!table_states && last_start_times)
386  {
387  hash_destroy(last_start_times);
388  last_start_times = NULL;
389  }
390 
391  /*
392  * Process all tables that are being synchronized.
393  */
394  foreach(lc, table_states)
395  {
397 
398  if (rstate->state == SUBREL_STATE_SYNCDONE)
399  {
400  /*
401  * Apply has caught up to the position where the table sync has
402  * finished. Mark the table as ready so that the apply will just
403  * continue to replicate it normally.
404  */
405  if (current_lsn >= rstate->lsn)
406  {
407  rstate->state = SUBREL_STATE_READY;
408  rstate->lsn = current_lsn;
409  if (!started_tx)
410  {
412  started_tx = true;
413  }
414 
416  rstate->relid, rstate->state,
417  rstate->lsn);
418  }
419  }
420  else
421  {
422  LogicalRepWorker *syncworker;
423 
424  /*
425  * Look for a sync worker for this relation.
426  */
427  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
428 
430  rstate->relid, false);
431 
432  if (syncworker)
433  {
434  /* Found one, update our copy of its state */
435  SpinLockAcquire(&syncworker->relmutex);
436  rstate->state = syncworker->relstate;
437  rstate->lsn = syncworker->relstate_lsn;
438  if (rstate->state == SUBREL_STATE_SYNCWAIT)
439  {
440  /*
441  * Sync worker is waiting for apply. Tell sync worker it
442  * can catchup now.
443  */
444  syncworker->relstate = SUBREL_STATE_CATCHUP;
445  syncworker->relstate_lsn =
446  Max(syncworker->relstate_lsn, current_lsn);
447  }
448  SpinLockRelease(&syncworker->relmutex);
449 
450  /* If we told worker to catch up, wait for it. */
451  if (rstate->state == SUBREL_STATE_SYNCWAIT)
452  {
453  /* Signal the sync worker, as it may be waiting for us. */
454  if (syncworker->proc)
455  logicalrep_worker_wakeup_ptr(syncworker);
456 
457  /* Now safe to release the LWLock */
458  LWLockRelease(LogicalRepWorkerLock);
459 
460  /*
461  * Enter busy loop and wait for synchronization worker to
462  * reach expected state (or die trying).
463  */
464  if (!started_tx)
465  {
467  started_tx = true;
468  }
469 
471  SUBREL_STATE_SYNCDONE);
472  }
473  else
474  LWLockRelease(LogicalRepWorkerLock);
475  }
476  else
477  {
478  /*
479  * If there is no sync worker for this table yet, count
480  * running sync workers for this subscription, while we have
481  * the lock.
482  */
483  int nsyncworkers =
485 
486  /* Now safe to release the LWLock */
487  LWLockRelease(LogicalRepWorkerLock);
488 
489  /*
490  * If there are free sync worker slot(s), start a new sync
491  * worker for the table.
492  */
493  if (nsyncworkers < max_sync_workers_per_subscription)
494  {
496  struct tablesync_start_time_mapping *hentry;
497  bool found;
498 
499  hentry = hash_search(last_start_times, &rstate->relid,
500  HASH_ENTER, &found);
501 
502  if (!found ||
503  TimestampDifferenceExceeds(hentry->last_start_time, now,
505  {
510  rstate->relid);
511  hentry->last_start_time = now;
512  }
513  }
514  }
515  }
516  }
517 
518  if (started_tx)
519  {
521  pgstat_report_stat(false);
522  }
523 }
Subscription * MySubscription
Definition: worker.c:107
#define NIL
Definition: pg_list.h:65
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:835
#define HASH_ELEM
Definition: hsearch.h:87
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
int64 TimestampTz
Definition: timestamp.h:39
void CommitTransactionCommand(void)
Definition: xact.c:2947
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
static bool wait_for_relation_state_change(Oid relid, char expected_state)
Definition: tablesync.c:154
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:927
unsigned int Oid
Definition: postgres_ext.h:31
static bool table_states_valid
Definition: tablesync.c:106
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1673
void list_free_deep(List *list)
Definition: list.c:1390
XLogRecPtr relstate_lsn
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:614
#define SpinLockAcquire(lock)
Definition: spin.h:62
Definition: dynahash.c:220
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
int max_sync_workers_per_subscription
Definition: launcher.c:55
List * lappend(List *list, void *datum)
Definition: list.c:321
int wal_retrieve_retry_interval
Definition: xlog.c:109
#define SpinLockRelease(lock)
Definition: spin.h:64
#define HASH_BLOBS
Definition: hsearch.h:88
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:328
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
Size keysize
Definition: hsearch.h:72
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:720
#define Max(x, y)
Definition: c.h:921
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:282
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:231
void StartTransactionCommand(void)
Definition: xact.c:2846
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
bool IsTransactionState(void)
Definition: xact.c:371
void * palloc(Size size)
Definition: mcxt.c:949
Definition: pg_list.h:50
List * GetSubscriptionNotReadyRelations(Oid subid)
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
MemoryContext CacheMemoryContext
Definition: mcxt.c:47
void pgstat_report_stat(bool force)
Definition: pgstat.c:839

◆ process_syncing_tables_for_sync()

static void process_syncing_tables_for_sync ( XLogRecPtr  current_lsn)
static

Definition at line 270 of file tablesync.c.

References Assert, IsTransactionState(), MyLogicalRepWorker, LogicalRepWorker::relid, LogicalRepWorker::relmutex, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, SpinLockAcquire, SpinLockRelease, LogicalRepWorker::subid, UpdateSubscriptionRelState(), walrcv_endstreaming, and wrconn.

Referenced by process_syncing_tables().

271 {
273 
275 
276  if (MyLogicalRepWorker->relstate == SUBREL_STATE_CATCHUP &&
277  current_lsn >= MyLogicalRepWorker->relstate_lsn)
278  {
279  TimeLineID tli;
280 
281  MyLogicalRepWorker->relstate = SUBREL_STATE_SYNCDONE;
282  MyLogicalRepWorker->relstate_lsn = current_lsn;
283 
285 
290 
292  finish_sync_worker();
293  }
294  else
296 }
WalReceiverConn * wrconn
Definition: worker.c:105
#define walrcv_endstreaming(conn, next_tli)
Definition: walreceiver.h:413
uint32 TimeLineID
Definition: xlogdefs.h:52
XLogRecPtr relstate_lsn
#define SpinLockAcquire(lock)
Definition: spin.h:62
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define SpinLockRelease(lock)
Definition: spin.h:64
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
#define Assert(condition)
Definition: c.h:745
bool IsTransactionState(void)
Definition: xact.c:371

◆ wait_for_relation_state_change()

static bool wait_for_relation_state_change ( Oid  relid,
char  expected_state 
)
static

Definition at line 154 of file tablesync.c.

References am_tablesync_worker(), CHECK_FOR_INTERRUPTS, GetLatestSnapshot(), GetSubscriptionRelState(), InvalidOid, logicalrep_worker_find(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyLatch, MyLogicalRepWorker, PopActiveSnapshot(), PushActiveSnapshot(), ResetLatch(), LogicalRepWorker::subid, WAIT_EVENT_LOGICAL_SYNC_STATE_CHANGE, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by process_syncing_tables_for_apply().

155 {
156  char state;
157 
158  for (;;)
159  {
160  LogicalRepWorker *worker;
161  XLogRecPtr statelsn;
162 
164 
165  /* XXX use cache invalidation here to improve performance? */
168  relid, &statelsn, true);
170 
171  if (state == SUBREL_STATE_UNKNOWN)
172  return false;
173 
174  if (state == expected_state)
175  return true;
176 
177  /* Check if the sync worker is still running and bail if not. */
178  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
179 
180  /* Check if the opposite worker is still running and bail if not. */
182  am_tablesync_worker() ? InvalidOid : relid,
183  false);
184  LWLockRelease(LogicalRepWorkerLock);
185  if (!worker)
186  return false;
187 
188  (void) WaitLatch(MyLatch,
191 
193  }
194 
195  return false;
196 }
#define WL_TIMEOUT
Definition: latch.h:127
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
void ResetLatch(Latch *latch)
Definition: latch.c:588
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
static bool am_tablesync_worker(void)
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:735
#define InvalidOid
Definition: postgres_ext.h:36
uint64 XLogRecPtr
Definition: xlogdefs.h:21
Definition: regguts.h:298
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:231
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:381
struct Latch * MyLatch
Definition: globals.c:54
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define WL_LATCH_SET
Definition: latch.h:124
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129

◆ wait_for_worker_state_change()

static bool wait_for_worker_state_change ( char  expected_state)
static

Definition at line 207 of file tablesync.c.

References CHECK_FOR_INTERRUPTS, InvalidOid, logicalrep_worker_find(), logicalrep_worker_wakeup_ptr(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyLatch, MyLogicalRepWorker, LogicalRepWorker::proc, LogicalRepWorker::relstate, ResetLatch(), LogicalRepWorker::subid, WAIT_EVENT_LOGICAL_SYNC_STATE_CHANGE, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by LogicalRepSyncTableStart().

208 {
209  int rc;
210 
211  for (;;)
212  {
213  LogicalRepWorker *worker;
214 
216 
217  /*
218  * Done if already in correct state. (We assume this fetch is atomic
219  * enough to not give a misleading answer if we do it with no lock.)
220  */
221  if (MyLogicalRepWorker->relstate == expected_state)
222  return true;
223 
224  /*
225  * Bail out if the apply worker has died, else signal it we're
226  * waiting.
227  */
228  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
230  InvalidOid, false);
231  if (worker && worker->proc)
233  LWLockRelease(LogicalRepWorkerLock);
234  if (!worker)
235  break;
236 
237  /*
238  * Wait. We expect to get a latch signal back from the apply worker,
239  * but use a timeout in case it dies without sending one.
240  */
241  rc = WaitLatch(MyLatch,
244 
245  if (rc & WL_LATCH_SET)
247  }
248 
249  return false;
250 }
#define WL_TIMEOUT
Definition: latch.h:127
void ResetLatch(Latch *latch)
Definition: latch.c:588
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:614
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define InvalidOid
Definition: postgres_ext.h:36
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:231
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
struct Latch * MyLatch
Definition: globals.c:54
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define WL_LATCH_SET
Definition: latch.h:124
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129

Variable Documentation

◆ copybuf

◆ table_states_valid

bool table_states_valid = false
static