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 bool FetchTableStates (bool *started_tx)
 
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)
 
void ReplicationSlotNameForTablesync (Oid suboid, Oid relid, char *syncslotname, int szslot)
 
void ReplicationOriginNameForTablesync (Oid suboid, Oid relid, char *originname, int szorgname)
 
char * LogicalRepSyncTableStart (XLogRecPtr *origin_startpos)
 
bool AllTablesyncsReady (void)
 
void UpdateTwoPhaseState (Oid suboid, char new_state)
 

Variables

static bool table_states_valid = false
 
static Listtable_states_not_ready = NIL
 
StringInfo copybuf = NULL
 

Function Documentation

◆ AllTablesyncsReady()

bool AllTablesyncsReady ( void  )

Definition at line 1230 of file tablesync.c.

References CommitTransactionCommand(), FetchTableStates(), list_length(), and pgstat_report_stat().

Referenced by ApplyWorkerMain(), and process_syncing_tables_for_apply().

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 }
static bool FetchTableStates(bool *started_tx)
Definition: tablesync.c:1169
void CommitTransactionCommand(void)
Definition: xact.c:2939
void pgstat_report_stat(bool disconnect)
Definition: pgstat.c:843
static List * table_states_not_ready
Definition: tablesync.c:121
static int list_length(const List *l)
Definition: pg_list.h:149

◆ copy_read_data()

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

Definition at line 619 of file tablesync.c.

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

Referenced by copy_table().

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 }
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:244
#define WL_TIMEOUT
Definition: latch.h:128
#define walrcv_receive(conn, buffer, wait_fd)
Definition: walreceiver.h:422
#define WL_SOCKET_READABLE
Definition: latch.h:126
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void ResetLatch(Latch *latch)
Definition: latch.c:660
static char * buf
Definition: pg_test_fsync.c:68
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:500
int pgsocket
Definition: port.h:31
#define PGINVALID_SOCKET
Definition: port.h:33
StringInfo copybuf
Definition: tablesync.c:124
struct Latch * MyLatch
Definition: globals.c:57
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130

◆ copy_table()

static void copy_table ( Relation  rel)
static

Definition at line 808 of file tablesync.c.

References AccessShareLock, addRangeTableEntryForRelation(), appendStringInfo(), appendStringInfoString(), Assert, LogicalRepRelation::attnames, BeginCopyFrom(), copy_read_data(), CopyFrom(), StringInfoData::data, ereport, WalRcvExecResult::err, errcode(), errmsg(), ERROR, fetch_remote_table_info(), get_namespace_name(), i, initStringInfo(), LogicalRepRelMapEntry::localrel, logicalrep_rel_close(), logicalrep_rel_open(), logicalrep_relmap_update(), LogRepWorkerWalRcvConn, 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, and WALRCV_OK_COPY_OUT.

Referenced by LogicalRepSyncTableStart().

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 
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 }
#define NIL
Definition: pg_list.h:65
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:244
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11332
uint64 CopyFrom(CopyFromState cstate)
Definition: copyfrom.c:525
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:698
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:433
LogicalRepRelId remoteid
Definition: logicalproto.h:101
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:157
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
#define ERROR
Definition: elog.h:46
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
#define RelationGetRelationName(relation)
Definition: rel.h:511
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:258
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:11416
StringInfo copybuf
Definition: tablesync.c:124
static void fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel)
Definition: tablesync.c:694
#define ereport(elevel,...)
Definition: elog.h:157
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
#define Assert(condition)
Definition: c.h:804
WalRcvExecStatus status
Definition: walreceiver.h:216
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
static int copy_read_data(void *outbuf, int minread, int maxread)
Definition: tablesync.c:619
static List * make_copy_attnamelist(LogicalRepRelMapEntry *rel)
Definition: tablesync.c:599
Definition: pg_list.h:50
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:430
#define RelationGetNamespace(relation)
Definition: rel.h:518

◆ fetch_remote_table_info()

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

Definition at line 694 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, errcode(), errmsg(), ERROR, ExecClearTuple(), ExecDropSingleTupleTableSlot(), initStringInfo(), lengthof, LogRepWorkerWalRcvConn, 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, and walrcv_server_version.

Referenced by copy_table().

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),
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 }
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:244
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:1238
#define DatumGetObjectId(X)
Definition: postgres.h:544
#define walrcv_server_version(conn)
Definition: walreceiver.h:414
int errcode(int sqlerrcode)
Definition: elog.c:698
#define lengthof(array)
Definition: c.h:734
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:109
LogicalRepRelId remoteid
Definition: logicalproto.h:101
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
#define ERROR
Definition: elog.h:46
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
#define DatumGetBool(X)
Definition: postgres.h:437
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define TextDatumGetCString(d)
Definition: builtins.h:83
void * palloc0(Size size)
Definition: mcxt.c:1093
#define DatumGetChar(X)
Definition: postgres.h:453
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define ereport(elevel,...)
Definition: elog.h:157
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
Tuplestorestate * tuplestore
Definition: walreceiver.h:219
#define Assert(condition)
Definition: c.h:804
WalRcvExecStatus status
Definition: walreceiver.h:216
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:430

◆ FetchTableStates()

static bool FetchTableStates ( bool started_tx)
static

Definition at line 1169 of file tablesync.c.

References CacheMemoryContext, GetSubscriptionNotReadyRelations(), HasSubscriptionRelations(), IsTransactionState(), lappend(), lfirst, list_free_deep(), list_length(), MemoryContextSwitchTo(), MySubscription, NIL, Subscription::oid, palloc(), StartTransactionCommand(), and table_states_valid.

Referenced by AllTablesyncsReady(), and process_syncing_tables_for_apply().

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. */
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));
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 }
Subscription * MySubscription
Definition: worker.c:246
#define NIL
Definition: pg_list.h:65
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static bool table_states_valid
Definition: tablesync.c:120
void list_free_deep(List *list)
Definition: list.c:1405
bool HasSubscriptionRelations(Oid subid)
List * lappend(List *list, void *datum)
Definition: list.c:336
static List * table_states_not_ready
Definition: tablesync.c:121
#define lfirst(lc)
Definition: pg_list.h:169
void StartTransactionCommand(void)
Definition: xact.c:2838
static int list_length(const List *l)
Definition: pg_list.h:149
bool IsTransactionState(void)
Definition: xact.c:371
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: pg_list.h:50
List * GetSubscriptionNotReadyRelations(Oid subid)
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ invalidate_syncing_table_states()

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

Definition at line 268 of file tablesync.c.

References table_states_valid.

Referenced by ApplyWorkerMain().

269 {
270  table_states_valid = false;
271 }
static bool table_states_valid
Definition: tablesync.c:120

◆ LogicalRepSyncTableStart()

char* LogicalRepSyncTableStart ( XLogRecPtr origin_startpos)

Definition at line 920 of file tablesync.c.

References Assert, CommandCounterIncrement(), CommitTransactionCommand(), Subscription::conninfo, copy_table(), CRS_USE_SNAPSHOT, DEBUG1, elog, ereport, WalRcvExecResult::err, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, GetSubscriptionRelState(), GetTransactionSnapshot(), HOLD_INTERRUPTS, InvalidXLogRecPtr, LockRelationOid(), LogRepWorkerWalRcvConn, LSN_FORMAT_ARGS, MyLogicalRepWorker, MySubscription, NAMEDATALEN, NoLock, Subscription::oid, OidIsValid, palloc(), pgstat_report_stat(), PopActiveSnapshot(), PushActiveSnapshot(), LogicalRepWorker::relid, LogicalRepWorker::relmutex, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, ReplicationOriginNameForTablesync(), ReplicationSlotDropAtPubNode(), ReplicationSlotNameForTablesync(), replorigin_advance(), replorigin_by_name(), replorigin_create(), replorigin_session_get_progress(), replorigin_session_origin, replorigin_session_setup(), RESUME_INTERRUPTS, RowExclusiveLock, SpinLockAcquire, SpinLockRelease, StartTransactionCommand(), WalRcvExecResult::status, LogicalRepWorker::subid, table_close(), table_open(), UnlockRelationOid(), UpdateSubscriptionRelState(), wait_for_worker_state_change(), walrcv_clear_result(), walrcv_connect, walrcv_create_slot, walrcv_exec, and WALRCV_OK_COMMAND.

Referenced by ApplyWorkerMain().

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 }
Subscription * MySubscription
Definition: worker.c:246
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:244
void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:200
void CommitTransactionCommand(void)
Definition: xact.c:2939
RepOriginId replorigin_by_name(const char *roname, bool missing_ok)
Definition: origin.c:209
uint16 RepOriginId
Definition: xlogdefs.h:65
static void copy_table(Relation rel)
Definition: tablesync.c:808
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1206
int errcode(int sqlerrcode)
Definition: elog.c:698
RepOriginId replorigin_create(const char *roname)
Definition: origin.c:240
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
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:843
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1071
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
#define OidIsValid(objectId)
Definition: c.h:710
XLogRecPtr relstate_lsn
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:133
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:436
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define ERROR
Definition: elog.h:46
#define NoLock
Definition: lockdefs.h:34
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
#define RowExclusiveLock
Definition: lockdefs.h:38
#define SpinLockRelease(lock)
Definition: spin.h:64
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
void CommandCounterIncrement(void)
Definition: xact.c:1021
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
#define ereport(elevel,...)
Definition: elog.h:157
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
WalRcvExecStatus status
Definition: walreceiver.h:216
RepOriginId replorigin_session_origin
Definition: origin.c:154
void StartTransactionCommand(void)
Definition: xact.c:2838
void ReplicationOriginNameForTablesync(Oid suboid, Oid relid, char *originname, int szorgname)
Definition: tablesync.c:905
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
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:109
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
static bool wait_for_worker_state_change(char expected_state)
Definition: tablesync.c:219
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:430
void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, int szslot)
Definition: tablesync.c:892
#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

◆ make_copy_attnamelist()

static List* make_copy_attnamelist ( LogicalRepRelMapEntry rel)
static

Definition at line 599 of file tablesync.c.

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

Referenced by copy_table().

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 }
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:336
int i
Definition: pg_list.h:50

◆ pg_attribute_noreturn()

static void pg_attribute_noreturn ( )
static

Definition at line 130 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().

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 }
Subscription * MySubscription
Definition: worker.c:246
void logicalrep_worker_wakeup(Oid subid, Oid relid)
Definition: launcher.c:533
void CommitTransactionCommand(void)
Definition: xact.c:2939
void proc_exit(int code)
Definition: ipc.c:104
void pgstat_report_stat(bool disconnect)
Definition: pgstat.c:843
#define LOG
Definition: elog.h:26
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2860
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11771
void StartTransactionCommand(void)
Definition: xact.c:2838
bool IsTransactionState(void)
Definition: xact.c:371
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899

◆ process_syncing_tables()

void process_syncing_tables ( XLogRecPtr  current_lsn)

Definition at line 587 of file tablesync.c.

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

Referenced by apply_handle_commit(), apply_handle_commit_prepared(), apply_handle_prepare(), apply_handle_rollback_prepared(), apply_handle_stream_commit(), apply_handle_stream_prepare(), and LogicalRepApplyLoop().

588 {
589  if (am_tablesync_worker())
590  process_syncing_tables_for_sync(current_lsn);
591  else
593 }
static void process_syncing_tables_for_apply(XLogRecPtr current_lsn)
Definition: tablesync.c:362
static bool am_tablesync_worker(void)
static void process_syncing_tables_for_sync(XLogRecPtr current_lsn)
Definition: tablesync.c:282

◆ process_syncing_tables_for_apply()

static void process_syncing_tables_for_apply ( XLogRecPtr  current_lsn)
static

Definition at line 362 of file tablesync.c.

References AllTablesyncsReady(), Assert, CommitTransactionCommand(), LogicalRepWorker::dbid, HASHCTL::entrysize, ereport, errmsg(), FetchTableStates(), GetCurrentTimestamp(), HASH_BLOBS, hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, hash_search(), IsTransactionState(), HASHCTL::keysize, lfirst, LOG, logicalrep_sync_worker_count(), LOGICALREP_TWOPHASE_STATE_PENDING, logicalrep_worker_find(), logicalrep_worker_launch(), logicalrep_worker_wakeup_ptr(), SubscriptionRelState::lsn, LW_SHARED, LWLockAcquire(), LWLockRelease(), Max, max_sync_workers_per_subscription, MyLogicalRepWorker, MySubscription, Subscription::name, NAMEDATALEN, now(), Subscription::oid, pgstat_report_stat(), LogicalRepWorker::proc, proc_exit(), SubscriptionRelState::relid, LogicalRepWorker::relmutex, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, ReplicationOriginNameForTablesync(), replorigin_drop_by_name(), SpinLockAcquire, SpinLockRelease, StartTransactionCommand(), SubscriptionRelState::state, LogicalRepWorker::subid, TimestampDifferenceExceeds(), Subscription::twophasestate, UpdateSubscriptionRelState(), LogicalRepWorker::userid, wait_for_relation_state_change(), and wal_retrieve_retry_interval.

Referenced by process_syncing_tables().

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 }
Subscription * MySubscription
Definition: worker.c:246
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
#define HASH_ELEM
Definition: hsearch.h:95
static bool FetchTableStates(bool *started_tx)
Definition: tablesync.c:1169
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
void CommitTransactionCommand(void)
Definition: xact.c:2939
void replorigin_drop_by_name(const char *name, bool missing_ok, bool nowait)
Definition: origin.c:414
Size entrysize
Definition: hsearch.h:76
void proc_exit(int code)
Definition: ipc.c:104
static bool wait_for_relation_state_change(Oid relid, char expected_state)
Definition: tablesync.c:171
void pgstat_report_stat(bool disconnect)
Definition: pgstat.c:843
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1711
XLogRecPtr relstate_lsn
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define NAMEDATALEN
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:553
#define SpinLockAcquire(lock)
Definition: spin.h:62
Definition: dynahash.c:219
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
int max_sync_workers_per_subscription
Definition: launcher.c:55
#define LOGICALREP_TWOPHASE_STATE_PENDING
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
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
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
Size keysize
Definition: hsearch.h:75
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:659
#define ereport(elevel,...)
Definition: elog.h:157
#define Max(x, y)
Definition: c.h:980
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
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:215
void StartTransactionCommand(void)
Definition: xact.c:2838
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
int errmsg(const char *fmt,...)
Definition: elog.c:909
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ process_syncing_tables_for_sync()

static void process_syncing_tables_for_sync ( XLogRecPtr  current_lsn)
static

Definition at line 282 of file tablesync.c.

References IsTransactionState(), LogRepWorkerWalRcvConn, MyLogicalRepWorker, NAMEDATALEN, LogicalRepWorker::relid, LogicalRepWorker::relmutex, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, ReplicationSlotDropAtPubNode(), ReplicationSlotNameForTablesync(), SpinLockAcquire, SpinLockRelease, StartTransactionCommand(), LogicalRepWorker::subid, UpdateSubscriptionRelState(), and walrcv_endstreaming.

Referenced by process_syncing_tables().

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 }
#define walrcv_endstreaming(conn, next_tli)
Definition: walreceiver.h:420
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:244
uint32 TimeLineID
Definition: xlogdefs.h:59
void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
XLogRecPtr relstate_lsn
#define NAMEDATALEN
#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)
void StartTransactionCommand(void)
Definition: xact.c:2838
bool IsTransactionState(void)
Definition: xact.c:371
void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, int szslot)
Definition: tablesync.c:892

◆ ReplicationOriginNameForTablesync()

void ReplicationOriginNameForTablesync ( Oid  suboid,
Oid  relid,
char *  originname,
int  szorgname 
)

Definition at line 905 of file tablesync.c.

References snprintf.

Referenced by AlterSubscription_refresh(), DropSubscription(), LogicalRepSyncTableStart(), and process_syncing_tables_for_apply().

907 {
908  snprintf(originname, szorgname, "pg_%u_%u", suboid, relid);
909 }
#define snprintf
Definition: port.h:216

◆ ReplicationSlotNameForTablesync()

void ReplicationSlotNameForTablesync ( Oid  suboid,
Oid  relid,
char *  syncslotname,
int  szslot 
)

Definition at line 892 of file tablesync.c.

References GetSystemIdentifier(), snprintf, and UINT64_FORMAT.

Referenced by AlterSubscription_refresh(), DropSubscription(), LogicalRepSyncTableStart(), process_syncing_tables_for_sync(), and ReportSlotConnectionError().

894 {
895  snprintf(syncslotname, szslot, "pg_%u_sync_%u_" UINT64_FORMAT, suboid,
896  relid, GetSystemIdentifier());
897 }
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4958
#define snprintf
Definition: port.h:216
#define UINT64_FORMAT
Definition: c.h:484

◆ UpdateTwoPhaseState()

void UpdateTwoPhaseState ( Oid  suboid,
char  new_state 
)

Definition at line 1255 of file tablesync.c.

References Assert, CatalogTupleUpdate(), CharGetDatum, elog, ERROR, heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, LOGICALREP_TWOPHASE_STATE_DISABLED, LOGICALREP_TWOPHASE_STATE_ENABLED, LOGICALREP_TWOPHASE_STATE_PENDING, ObjectIdGetDatum, RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy1, SUBSCRIPTIONOID, HeapTupleData::t_self, table_close(), table_open(), and values.

Referenced by ApplyWorkerMain(), and CreateSubscription().

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 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RelationGetDescr(relation)
Definition: rel.h:503
#define LOGICALREP_TWOPHASE_STATE_DISABLED
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define LOGICALREP_TWOPHASE_STATE_ENABLED
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define LOGICALREP_TWOPHASE_STATE_PENDING
uintptr_t Datum
Definition: postgres.h:411
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
#define CharGetDatum(X)
Definition: postgres.h:460
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
#define elog(elevel,...)
Definition: elog.h:232
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

◆ wait_for_relation_state_change()

static bool wait_for_relation_state_change ( Oid  relid,
char  expected_state 
)
static

Definition at line 171 of file tablesync.c.

References CHECK_FOR_INTERRUPTS, GetSubscriptionRelState(), InvalidateCatalogSnapshot(), logicalrep_worker_find(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyLatch, MyLogicalRepWorker, 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().

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 }
#define WL_TIMEOUT
Definition: latch.h:128
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:456
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
Definition: regguts.h:317
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:215
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
struct Latch * MyLatch
Definition: globals.c:57
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130

◆ wait_for_worker_state_change()

static bool wait_for_worker_state_change ( char  expected_state)
static

Definition at line 219 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().

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 }
#define WL_TIMEOUT
Definition: latch.h:128
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:553
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:215
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
struct Latch * MyLatch
Definition: globals.c:57
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130

Variable Documentation

◆ copybuf

◆ table_states_not_ready

List* table_states_not_ready = NIL
static

Definition at line 121 of file tablesync.c.

◆ table_states_valid

bool table_states_valid = false
static

Definition at line 120 of file tablesync.c.

Referenced by FetchTableStates(), and invalidate_syncing_table_states().