Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c6e1f62

Browse filesBrowse files
committed
Wake up a subscription's replication worker processes after DDL.
Waken related worker processes immediately at commit of a transaction that has performed ALTER SUBSCRIPTION (including the RENAME and OWNER variants). This reduces the response time for such operations. In the real world that might not be worth much, but it shaves several seconds off the runtime for the subscription test suite. In the case of PREPARE, we just throw away this notification state; it doesn't seem worth the work to preserve it. The workers will still react after the eventual COMMIT PREPARED, but not as quickly. Nathan Bossart Discussion: https://postgr.es/m/20221122004119.GA132961@nathanxps13
1 parent 4c032dd commit c6e1f62
Copy full SHA for c6e1f62

File tree

5 files changed

+72
-0
lines changed
Filter options

5 files changed

+72
-0
lines changed

‎src/backend/access/transam/xact.c

Copy file name to clipboardExpand all lines: src/backend/access/transam/xact.c
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "pgstat.h"
4848
#include "replication/logical.h"
4949
#include "replication/logicallauncher.h"
50+
#include "replication/logicalworker.h"
5051
#include "replication/origin.h"
5152
#include "replication/snapbuild.h"
5253
#include "replication/syncrep.h"
@@ -2360,6 +2361,7 @@ CommitTransaction(void)
23602361
AtEOXact_PgStat(true, is_parallel_worker);
23612362
AtEOXact_Snapshot(true, false);
23622363
AtEOXact_ApplyLauncher(true);
2364+
AtEOXact_LogicalRepWorkers(true);
23632365
pgstat_report_xact_timestamp(0);
23642366

23652367
CurrentResourceOwner = NULL;
@@ -2647,6 +2649,9 @@ PrepareTransaction(void)
26472649
AtEOXact_HashTables(true);
26482650
/* don't call AtEOXact_PgStat here; we fixed pgstat state above */
26492651
AtEOXact_Snapshot(true, true);
2652+
/* we treat PREPARE as ROLLBACK so far as waking workers goes */
2653+
AtEOXact_ApplyLauncher(false);
2654+
AtEOXact_LogicalRepWorkers(false);
26502655
pgstat_report_xact_timestamp(0);
26512656

26522657
CurrentResourceOwner = NULL;
@@ -2860,6 +2865,7 @@ AbortTransaction(void)
28602865
AtEOXact_HashTables(false);
28612866
AtEOXact_PgStat(false, is_parallel_worker);
28622867
AtEOXact_ApplyLauncher(false);
2868+
AtEOXact_LogicalRepWorkers(false);
28632869
pgstat_report_xact_timestamp(0);
28642870
}
28652871

‎src/backend/commands/alter.c

Copy file name to clipboardExpand all lines: src/backend/commands/alter.c
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "commands/user.h"
6060
#include "miscadmin.h"
6161
#include "parser/parse_func.h"
62+
#include "replication/logicalworker.h"
6263
#include "rewrite/rewriteDefine.h"
6364
#include "tcop/utility.h"
6465
#include "utils/builtins.h"
@@ -279,6 +280,9 @@ AlterObjectRename_internal(Relation rel, Oid objectId, const char *new_name)
279280
if (strncmp(new_name, "regress_", 8) != 0)
280281
elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\"");
281282
#endif
283+
284+
/* Wake up related replication workers to handle this change quickly */
285+
LogicalRepWorkersWakeupAtCommit(objectId);
282286
}
283287
else if (nameCacheId >= 0)
284288
{

‎src/backend/commands/subscriptioncmds.c

Copy file name to clipboardExpand all lines: src/backend/commands/subscriptioncmds.c
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "nodes/makefuncs.h"
3535
#include "pgstat.h"
3636
#include "replication/logicallauncher.h"
37+
#include "replication/logicalworker.h"
3738
#include "replication/origin.h"
3839
#include "replication/slot.h"
3940
#include "replication/walreceiver.h"
@@ -1362,6 +1363,9 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt,
13621363

13631364
InvokeObjectPostAlterHook(SubscriptionRelationId, subid, 0);
13641365

1366+
/* Wake up related replication workers to handle this change quickly. */
1367+
LogicalRepWorkersWakeupAtCommit(subid);
1368+
13651369
return myself;
13661370
}
13671371

@@ -1732,7 +1736,9 @@ AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
17321736
InvokeObjectPostAlterHook(SubscriptionRelationId,
17331737
form->oid, 0);
17341738

1739+
/* Wake up related background processes to handle this change quickly. */
17351740
ApplyLauncherWakeupAtCommit();
1741+
LogicalRepWorkersWakeupAtCommit(form->oid);
17361742
}
17371743

17381744
/*

‎src/backend/replication/logical/worker.c

Copy file name to clipboardExpand all lines: src/backend/replication/logical/worker.c
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ WalReceiverConn *LogRepWorkerWalRcvConn = NULL;
254254
Subscription *MySubscription = NULL;
255255
static bool MySubscriptionValid = false;
256256

257+
static List *on_commit_wakeup_workers_subids = NIL;
258+
257259
bool in_remote_transaction = false;
258260
static XLogRecPtr remote_final_lsn = InvalidXLogRecPtr;
259261

@@ -4092,3 +4094,53 @@ reset_apply_error_context_info(void)
40924094
apply_error_callback_arg.remote_attnum = -1;
40934095
set_apply_error_context_xact(InvalidTransactionId, InvalidXLogRecPtr);
40944096
}
4097+
4098+
/*
4099+
* Request wakeup of the workers for the given subscription OID
4100+
* at commit of the current transaction.
4101+
*
4102+
* This is used to ensure that the workers process assorted changes
4103+
* as soon as possible.
4104+
*/
4105+
void
4106+
LogicalRepWorkersWakeupAtCommit(Oid subid)
4107+
{
4108+
MemoryContext oldcxt;
4109+
4110+
oldcxt = MemoryContextSwitchTo(TopTransactionContext);
4111+
on_commit_wakeup_workers_subids =
4112+
list_append_unique_oid(on_commit_wakeup_workers_subids, subid);
4113+
MemoryContextSwitchTo(oldcxt);
4114+
}
4115+
4116+
/*
4117+
* Wake up the workers of any subscriptions that were changed in this xact.
4118+
*/
4119+
void
4120+
AtEOXact_LogicalRepWorkers(bool isCommit)
4121+
{
4122+
if (isCommit && on_commit_wakeup_workers_subids != NIL)
4123+
{
4124+
ListCell *lc;
4125+
4126+
LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
4127+
foreach(lc, on_commit_wakeup_workers_subids)
4128+
{
4129+
Oid subid = lfirst_oid(lc);
4130+
List *workers;
4131+
ListCell *lc2;
4132+
4133+
workers = logicalrep_workers_find(subid, true);
4134+
foreach(lc2, workers)
4135+
{
4136+
LogicalRepWorker *worker = (LogicalRepWorker *) lfirst(lc2);
4137+
4138+
logicalrep_worker_wakeup_ptr(worker);
4139+
}
4140+
}
4141+
LWLockRelease(LogicalRepWorkerLock);
4142+
}
4143+
4144+
/* The List storage will be reclaimed automatically in xact cleanup. */
4145+
on_commit_wakeup_workers_subids = NIL;
4146+
}

‎src/include/replication/logicalworker.h

Copy file name to clipboardExpand all lines: src/include/replication/logicalworker.h
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ extern void ApplyWorkerMain(Datum main_arg);
1616

1717
extern bool IsLogicalWorker(void);
1818

19+
extern void LogicalRepWorkersWakeupAtCommit(Oid subid);
20+
21+
extern void AtEOXact_LogicalRepWorkers(bool isCommit);
22+
1923
#endif /* LOGICALWORKER_H */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.