Skip to content

Commit 4c032dd

Browse files
committed
Check for two_phase change at end of process_syncing_tables_for_apply.
Previously this function checked to see if we were ready to switch to two_phase mode at its start, but that's silly: we should check at the end, after we've done the work that might make us ready. This simple change removes one sleep cycle from the time needed to switch to two_phase mode. In the real world that might not be worth much, but it shaves a few seconds off the runtime for the subscription test suite. Nathan Bossart Discussion: https://postgr.es/m/20221122004119.GA132961@nathanxps13
1 parent b1665bf commit 4c032dd

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/backend/replication/logical/tablesync.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
415415
static HTAB *last_start_times = NULL;
416416
ListCell *lc;
417417
bool started_tx = false;
418+
bool should_exit = false;
418419

419420
Assert(!IsTransactionState());
420421

@@ -446,28 +447,6 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
446447
last_start_times = NULL;
447448
}
448449

449-
/*
450-
* Even when the two_phase mode is requested by the user, it remains as
451-
* 'pending' until all tablesyncs have reached READY state.
452-
*
453-
* When this happens, we restart the apply worker and (if the conditions
454-
* are still ok) then the two_phase tri-state will become 'enabled' at
455-
* that time.
456-
*
457-
* Note: If the subscription has no tables then leave the state as
458-
* PENDING, which allows ALTER SUBSCRIPTION ... REFRESH PUBLICATION to
459-
* work.
460-
*/
461-
if (MySubscription->twophasestate == LOGICALREP_TWOPHASE_STATE_PENDING &&
462-
AllTablesyncsReady())
463-
{
464-
ereport(LOG,
465-
(errmsg("logical replication apply worker for subscription \"%s\" will restart so that two_phase can be enabled",
466-
MySubscription->name)));
467-
468-
proc_exit(0);
469-
}
470-
471450
/*
472451
* Process all tables that are being synchronized.
473452
*/
@@ -619,9 +598,36 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
619598

620599
if (started_tx)
621600
{
601+
/*
602+
* Even when the two_phase mode is requested by the user, it remains
603+
* as 'pending' until all tablesyncs have reached READY state.
604+
*
605+
* When this happens, we restart the apply worker and (if the
606+
* conditions are still ok) then the two_phase tri-state will become
607+
* 'enabled' at that time.
608+
*
609+
* Note: If the subscription has no tables then leave the state as
610+
* PENDING, which allows ALTER SUBSCRIPTION ... REFRESH PUBLICATION to
611+
* work.
612+
*/
613+
if (MySubscription->twophasestate == LOGICALREP_TWOPHASE_STATE_PENDING)
614+
{
615+
CommandCounterIncrement(); /* make updates visible */
616+
if (AllTablesyncsReady())
617+
{
618+
ereport(LOG,
619+
(errmsg("logical replication apply worker for subscription \"%s\" will restart so that two_phase can be enabled",
620+
MySubscription->name)));
621+
should_exit = true;
622+
}
623+
}
624+
622625
CommitTransactionCommand();
623626
pgstat_report_stat(true);
624627
}
628+
629+
if (should_exit)
630+
proc_exit(0);
625631
}
626632

627633
/*
@@ -802,6 +808,7 @@ fetch_remote_table_info(char *nspname, char *relname,
802808
TupleTableSlot *tslot;
803809
Oid attrsRow[] = {INT2VECTOROID};
804810
StringInfoData pub_names;
811+
805812
initStringInfo(&pub_names);
806813
foreach(lc, MySubscription->publications)
807814
{

0 commit comments

Comments
 (0)