Skip to content

Commit 6473543

Browse files
author
Antonin Houska
committed
Refined comments.
1 parent add66f0 commit 6473543

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

worker.c

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,12 @@
3737

3838
/*
3939
* There are 2 kinds of worker: 1) scheduler, which creates new tasks, 2) the
40-
* actual "squeeze worker" which calls the squeeze_table() function. With
41-
* scheduling independent from the actual table processing we check the table
42-
* status exactly (with the granularity of one minute) at the scheduled
43-
* time. This way we avoid the tricky question how long should particular
44-
* schedule stay valid and thus we can use equality operator to check if the
45-
* scheduled time is there.
46-
*
47-
* There are 2 alternatives to the equality test: 1) schedule is valid for
48-
* some time interval which is hard to define, 2) the schedule is valid
49-
* forever - this is bad because it allows table processing even hours after
50-
* the schedule if the table happens to get bloated some time after the
51-
* schedule.
52-
*
53-
* If the squeeze_table() function makes the following tasks delayed, it's
54-
* another problem that we can address by increasing the number of "squeeze
55-
* workers". (In that case we'd have to adjust the replication slot naming
56-
* scheme so that there are no conflicts.)
40+
* actual "squeeze worker" which calls the squeeze_table() function. It's
41+
* simpler to have a separate worker that checks the schedules every
42+
* minute. If there was a single worker that checks the schedules among the
43+
* calls of squeeze_table(), it'd be harder to handle the cases where the call
44+
* of squeeze_table() took too much time to complete (i.e. the worker could
45+
* miss some schedule(s)).
5746
*/
5847
static bool am_i_scheduler = false;
5948

@@ -64,14 +53,17 @@ static bool am_i_scheduler = false;
6453
static bool am_i_standalone = false;
6554

6655
/*
67-
* The shmem_request_hook_type hook was introduced in PG 15. Since the number
68-
* of slots depends on the max_worker_processes GUC, the maximum number of
69-
* squeeze workers must be a compile time constant for PG < 15.
56+
* As long as the number of slots depends on the max_worker_processes GUC (it
57+
* just makes sense not to allocate more slots for our workers than this
58+
* value), we should not use this GUC before the other libraries have been
59+
* loaded: those libraries might, at least in theory, adjust
60+
* max_worker_processes.
7061
*
71-
* XXX Regarding PG < 15: maybe we don't need to worry about dependency on an
72-
* in-core GUC - the value should be known at load time and no other loadable
73-
* module should be able to change it before we start the shared memory
74-
* allocation.
62+
* In PG >= 15, this function is called from squeeze_worker_shmem_request(),
63+
* after all the related GUCs have been set. In earlier versions (which do not
64+
* have the hook), the function is called while our library is being loaded,
65+
* and some other libraries might follow. Therefore we prefer a compile time
66+
* constant to a (possibly) not-yet-finalized GUC.
7567
*/
7668
static int
7769
max_squeeze_workers(void)
@@ -209,6 +201,10 @@ squeeze_save_prev_shmem_request_hook(void)
209201
}
210202
#endif
211203

204+
/*
205+
* The shmem_request_hook hook was introduced in PG 15. In earlier versions we
206+
* call it directly from _PG_init().
207+
*/
212208
void
213209
squeeze_worker_shmem_request(void)
214210
{
@@ -914,7 +910,8 @@ squeeze_worker_main(Datum main_arg)
914910

915911
/*
916912
* Initialize MyWorkerTask as soon as possible so that
917-
* worker_shmem_shutdown() can release it in case of ERROR.
913+
* worker_shmem_shutdown() can clean it up in the shared memory in case of
914+
* ERROR.
918915
*/
919916
if (task_idx >= 0)
920917
{
@@ -942,10 +939,9 @@ squeeze_worker_main(Datum main_arg)
942939
/*
943940
* The first worker after restart is responsible for cleaning up
944941
* replication slots and/or origins that other workers could not remove
945-
* due to server crash. Do that while holding the exclusive lock, as
946-
* concurrency makes no sense here. That also ensures that the other
947-
* workers wait for the cleanup to finish instead of complaining about the
948-
* existing slots / origins.
942+
* due to server crash. Do that while holding the exclusive lock - that
943+
* also ensures that the other workers wait for the cleanup to finish
944+
* instead of complaining about the existing slots / origins.
949945
*/
950946
if (!am_i_scheduler && !workerData->cleanup_done)
951947
{
@@ -1352,7 +1348,8 @@ scheduler_worker_loop(void)
13521348
* the current transaction.
13531349
*/
13541350
old_cxt = MemoryContextSwitchTo(sched_cxt);
1355-
registered = start_worker_internal(false, task_idx, &worker->handle);
1351+
registered = start_worker_internal(false, task_idx,
1352+
&worker->handle);
13561353
MemoryContextSwitchTo(old_cxt);
13571354

13581355
if (!registered)
@@ -2322,7 +2319,7 @@ release_task(WorkerTask *task)
23222319
*/
23232320

23242321
MyWorkerTask = NULL;
2325-
/* Let others to see the WTS_UNUSED state. */
2322+
/* Let others see the WTS_UNUSED state. */
23262323
SpinLockRelease(&task->mutex);
23272324
}
23282325

0 commit comments

Comments
 (0)