agora inbox for [email protected]
help / color / mirror / Atom feedRe: Is indexing broken for bigint columns?
498+ messages / 4 participants
[nested] [flat]
* Re: Is indexing broken for bigint columns?
@ 2004-02-25 00:20 Dann Corbit <[email protected]>
0 siblings, 2 replies; 498+ messages in thread
From: Dann Corbit @ 2004-02-25 00:20 UTC (permalink / raw)
To: Peter Eisentraut <[email protected]>; pgsql-hackers
> -----Original Message-----
> From: Peter Eisentraut [mailto:[email protected]]
> Sent: Tuesday, February 24, 2004 3:38 PM
> To: Dann Corbit; PostgreSQL-development
> Subject: Re: [HACKERS] Is indexing broken for bigint columns?
>
>
> Dann Corbit wrote:
> > http://www.phpbuilder.com/columns/smith20010821.php3?page=3
>
http://www.postgresql.org/docs/7.4/static/datatype.html#DATATYPE-INT
PostgreSQL is the only database that requires casts to do an index
lookup.
This is SQL*Server syntax:
==============================================================
drop table foo
go
create table foo (bar bigint)
go
insert into foo (bar) values (1)
go
insert into foo (bar) values (-9223372036854775808)
go
insert into foo (bar) values (9223372036854775807)
go
create unique clustered index foobar on foo(bar)
go
select * from foo where bar = 1
Go
-- Correctly returns a value of 1.
==============================================================
This is Oracle syntax:
==============================================================
SQL> drop table foo;
Table dropped.
SQL>
SQL> create table foo (bar number(19));
Table created.
SQL>
SQL> insert into foo (bar) values (1);
1 row created.
SQL>
SQL> insert into foo (bar) values (-9223372036854775808);
1 row created.
SQL>
SQL> insert into foo (bar) values (9223372036854775807);
1 row created.
SQL>
SQL> create unique index foobar on foo(bar);
Index created.
SQL>
SQL> select * from foo where bar = 1;
BAR
---------
1
SQL>
SQL>
==============================================================
DB/2 uses bigint like SQL*Server and PostgreSQL and necessary
conversions are implicit.
Sybase and Rdb also use bigint types.
And now, here is the unkindest cut of all:
mysql> create table foo (bar bigint);
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> insert into foo (bar) values (1);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> insert into foo (bar) values (-9223372036854775808);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> insert into foo (bar) values (9223372036854775807);
Query OK, 1 row affected (0.02 sec)
mysql>
mysql> create unique index foobar on foo(bar);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql>
mysql> select * from foo where bar = 1;
+------+
| bar |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
And (prattling on) if this is necessary for PostgreSQL:
select * from foo where bar = 1::bigint;
Why wouldn't this be necessary:
select * from foo where bar = 1::integer;
For an integer column?
^ permalink raw reply [nested|flat] 498+ messages in thread
* Re: Is indexing broken for bigint columns?
@ 2004-02-25 00:37 Mike Mascari <[email protected]>
parent: Dann Corbit <[email protected]>
1 sibling, 0 replies; 498+ messages in thread
From: Mike Mascari @ 2004-02-25 00:37 UTC (permalink / raw)
To: Dann Corbit <[email protected]>; +Cc: Peter Eisentraut <[email protected]>; pgsql-hackers
Dann Corbit wrote:
> PostgreSQL is the only database that requires casts to do an index
> lookup.
Possibly (quite probably) true, but you don't show any evidence that
SQL*Server, Oracle, or MySQL uses indexes either. Like I said
before, Tom (of course) already has a fix is already in the
development branch:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=29832.1068682253...
>
> This is SQL*Server syntax:
> ==============================================================
...
> select * from foo where bar = 1
...
> This is Oracle syntax:
> ==============================================================
> SQL> select * from foo where bar = 1;
...
> mysql> select * from foo where bar = 1;
Mike Mascari
^ permalink raw reply [nested|flat] 498+ messages in thread
* Re: Is indexing broken for bigint columns?
@ 2004-02-25 00:38 Peter Eisentraut <[email protected]>
parent: Dann Corbit <[email protected]>
1 sibling, 0 replies; 498+ messages in thread
From: Peter Eisentraut @ 2004-02-25 00:38 UTC (permalink / raw)
To: Dann Corbit <[email protected]>; pgsql-hackers
Dann Corbit wrote:
> > Dann Corbit wrote:
> > > http://www.phpbuilder.com/columns/smith20010821.php3?page=3
>
> http://www.postgresql.org/docs/7.4/static/datatype.html#DATATYPE-INT
>
> PostgreSQL is the only database that requires casts to do an index
> lookup.
This issue has been discussed on these mailing lists literally dozens of
times. If you're interested in the details, please see the archives.
Further discussion will hopefully not be necessary, because 7.5 will
fix it.
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
* [PATCH] Restructure repack worker teardown
@ 2026-05-18 17:13 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 498+ messages in thread
From: Álvaro Herrera @ 2026-05-18 17:13 UTC (permalink / raw)
The original code would leave a shared memory segment unreleased if we
fail partway through initialization. Change the shutdown order so that
we already free it.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 67 ++++++++++++++++-------------------
1 file changed, 31 insertions(+), 36 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index bfc62c8f752..c9064d8fd13 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3411,10 +3411,14 @@ start_repack_decoding_worker(Oid relid)
shm_mq_handle *mqh;
BackgroundWorker bgw;
+ decoding_worker = palloc0_object(DecodingWorker);
+
/* Setup shared memory. */
size = BUFFERALIGN(offsetof(DecodingWorkerShared, error_queue)) +
BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);
seg = dsm_create(size, 0);
+ decoding_worker->seg = seg;
+
shared = (DecodingWorkerShared *) dsm_segment_address(seg);
shared->initialized = false;
shared->lsn_upto = InvalidXLogRecPtr;
@@ -3454,14 +3458,12 @@ start_repack_decoding_worker(Oid relid)
bgw.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
bgw.bgw_notify_pid = MyProcPid;
- decoding_worker = palloc0_object(DecodingWorker);
if (!RegisterDynamicBackgroundWorker(&bgw, &decoding_worker->handle))
ereport(ERROR,
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("out of background worker slots"),
errhint("You might need to increase \"%s\".", "max_worker_processes"));
- decoding_worker->seg = seg;
decoding_worker->error_mqh = mqh;
/*
@@ -3487,17 +3489,6 @@ start_repack_decoding_worker(Oid relid)
ConditionVariableCancelSleep();
}
-/*
- * PG_ENSURE_ERROR_CLEANUP callback to stop the decoding worker.
- * This ensures the worker is terminated on both ERROR and FATAL exits,
- * unlike PG_FINALLY which only handles ERROR.
- */
-static void
-repack_decoding_worker_cleanup_cb(int code, Datum arg)
-{
- stop_repack_decoding_worker();
-}
-
/*
* Stop the decoding worker and cleanup the related resources.
*
@@ -3508,39 +3499,43 @@ static void
stop_repack_decoding_worker(void)
{
BgwHandleStatus status;
+ dsm_segment *dsmseg;
- /* Haven't reached the worker startup? */
+ /* Nothing to do if no worker was set up. */
if (decoding_worker == NULL)
return;
- /* Could not register the worker? */
- if (decoding_worker->handle == NULL)
- return;
+ /* Terminate the worker process, if one is running. */
+ if (decoding_worker->handle != NULL)
+ {
+ TerminateBackgroundWorker(decoding_worker->handle);
+ /* The worker should really exit before the REPACK command does. */
+ HOLD_INTERRUPTS();
+ status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
+ RESUME_INTERRUPTS();
- TerminateBackgroundWorker(decoding_worker->handle);
- /* The worker should really exit before the REPACK command does. */
- HOLD_INTERRUPTS();
- status = WaitForBackgroundWorkerShutdown(decoding_worker->handle);
- RESUME_INTERRUPTS();
-
- if (status == BGWH_POSTMASTER_DIED)
- ereport(FATAL,
- errcode(ERRCODE_ADMIN_SHUTDOWN),
- errmsg("postmaster exited during REPACK command"));
-
- shm_mq_detach(decoding_worker->error_mqh);
+ if (status == BGWH_POSTMASTER_DIED)
+ ereport(FATAL,
+ errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("postmaster exited during REPACK command"));
+ }
/*
- * If we could not cancel the current sleep due to ERROR, do that before
- * we detach from the shared memory the condition variable is located in.
- * If we did not, the bgworker ERROR handling code would try and fail
- * badly.
+ * Now detach from our shared memory segment. In error cases there might
+ * still be messages from the worker in the queue, which ProcessInterrupts
+ * would try to read; this is pointless (and causes an assertion failure),
+ * so set the global pointer to NULL to have ProcessRepackMessages ignore
+ * them.
*/
- ConditionVariableCancelSleep();
-
- dsm_detach(decoding_worker->seg);
+ dsmseg = decoding_worker->seg;
pfree(decoding_worker);
decoding_worker = NULL;
+
+ /* We must also cancel the current sleep, if one is still set up */
+ ConditionVariableCancelSleep();
+
+ if (dsmseg != NULL)
+ dsm_detach(dsmseg);
}
/* stop_repack_decoding_worker, wrapped as a before_shmem_exit callback */
--
2.47.3
--b42ct6adeyjj4cbm--
^ permalink raw reply [nested|flat] 498+ messages in thread
end of thread, other threads:[~2026-05-18 17:13 UTC | newest]
Thread overview: 498+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-02-25 00:20 Re: Is indexing broken for bigint columns? Dann Corbit <[email protected]>
2004-02-25 00:37 ` Mike Mascari <[email protected]>
2004-02-25 00:38 ` Peter Eisentraut <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
2026-05-18 17:13 [PATCH] Restructure repack worker teardown Álvaro Herrera <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox