agora inbox for [email protected]
help / color / mirror / Atom feed[PATCH v54 2/5] Rename cluster.c/h -> repack.c/h
57+ messages / 1 participants
[nested] [flat]
* [PATCH v54 2/5] Rename cluster.c/h -> repack.c/h
@ 2026-03-31 16:55 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-03-31 16:55 UTC (permalink / raw)
---
src/backend/commands/Makefile | 2 +-
src/backend/commands/matview.c | 2 +-
src/backend/commands/meson.build | 2 +-
src/backend/commands/{cluster.c => repack.c} | 6 +++---
src/backend/commands/tablecmds.c | 2 +-
src/backend/commands/vacuum.c | 6 +++---
src/backend/storage/ipc/procsignal.c | 1 +
src/backend/tcop/postgres.c | 1 +
src/backend/tcop/utility.c | 2 +-
src/include/commands/{cluster.h => repack.h} | 12 ++++++------
10 files changed, 19 insertions(+), 17 deletions(-)
rename src/backend/commands/{cluster.c => repack.c} (99%)
rename src/include/commands/{cluster.h => repack.h} (90%)
diff --git a/src/backend/commands/Makefile b/src/backend/commands/Makefile
index c10fdba2bbb..fe1bba3a9b9 100644
--- a/src/backend/commands/Makefile
+++ b/src/backend/commands/Makefile
@@ -18,7 +18,6 @@ OBJS = \
amcmds.o \
analyze.o \
async.o \
- cluster.o \
collationcmds.o \
comment.o \
constraint.o \
@@ -51,6 +50,7 @@ OBJS = \
proclang.o \
propgraphcmds.o \
publicationcmds.o \
+ repack.o \
schemacmds.o \
seclabel.o \
sequence.o \
diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c
index d3be8939011..5db4fe75dce 100644
--- a/src/backend/commands/matview.c
+++ b/src/backend/commands/matview.c
@@ -24,8 +24,8 @@
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "catalog/pg_opclass.h"
-#include "commands/cluster.h"
#include "commands/matview.h"
+#include "commands/repack.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
#include "executor/executor.h"
diff --git a/src/backend/commands/meson.build b/src/backend/commands/meson.build
index 90c7e37a429..f624aae74af 100644
--- a/src/backend/commands/meson.build
+++ b/src/backend/commands/meson.build
@@ -6,7 +6,6 @@ backend_sources += files(
'amcmds.c',
'analyze.c',
'async.c',
- 'cluster.c',
'collationcmds.c',
'comment.c',
'constraint.c',
@@ -39,6 +38,7 @@ backend_sources += files(
'proclang.c',
'propgraphcmds.c',
'publicationcmds.c',
+ 'repack.c',
'schemacmds.c',
'seclabel.c',
'sequence.c',
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/repack.c
similarity index 99%
rename from src/backend/commands/cluster.c
rename to src/backend/commands/repack.c
index f241e18b153..20f0a572236 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/repack.c
@@ -1,6 +1,6 @@
/*-------------------------------------------------------------------------
*
- * cluster.c
+ * repack.c
* REPACK a table; formerly known as CLUSTER. VACUUM FULL also uses
* parts of this code.
*
@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
- * src/backend/commands/cluster.c
+ * src/backend/commands/repack.c
*
*-------------------------------------------------------------------------
*/
@@ -33,9 +33,9 @@
#include "catalog/pg_am.h"
#include "catalog/pg_inherits.h"
#include "catalog/toasting.h"
-#include "commands/cluster.h"
#include "commands/defrem.h"
#include "commands/progress.h"
+#include "commands/repack.h"
#include "commands/tablecmds.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 0ce2e81f9c2..e2882a50b3b 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -57,10 +57,10 @@
#include "catalog/storage.h"
#include "catalog/storage_xlog.h"
#include "catalog/toasting.h"
-#include "commands/cluster.h"
#include "commands/comment.h"
#include "commands/defrem.h"
#include "commands/event_trigger.h"
+#include "commands/repack.h"
#include "commands/sequence.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 0ed363d1c85..b179b62b5c8 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -9,7 +9,7 @@
*
* VACUUM for heap AM is implemented in vacuumlazy.c, parallel vacuum in
* vacuumparallel.c, ANALYZE in analyze.c, and VACUUM FULL is a variant of
- * CLUSTER, handled in cluster.c.
+ * REPACK, handled in repack.c.
*
*
* Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
@@ -38,9 +38,9 @@
#include "catalog/pg_database.h"
#include "catalog/pg_inherits.h"
#include "commands/async.h"
-#include "commands/cluster.h"
#include "commands/defrem.h"
#include "commands/progress.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
@@ -2293,7 +2293,7 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams params,
if ((params.options & VACOPT_VERBOSE) != 0)
cluster_params.options |= CLUOPT_VERBOSE;
- /* VACUUM FULL is a variant of REPACK; see cluster.c */
+ /* VACUUM FULL is a variant of REPACK; see repack.c */
cluster_rel(REPACK_COMMAND_VACUUMFULL, rel, InvalidOid,
&cluster_params);
/* cluster_rel closes the relation, but keeps lock */
diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c
index f1ab3aa3fe0..02d28df1c6a 100644
--- a/src/backend/storage/ipc/procsignal.c
+++ b/src/backend/storage/ipc/procsignal.c
@@ -19,6 +19,7 @@
#include "access/parallel.h"
#include "commands/async.h"
+#include "commands/repack.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 10be60011ad..9fbaa5c00f0 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -39,6 +39,7 @@
#include "commands/event_trigger.h"
#include "commands/explain_state.h"
#include "commands/prepare.h"
+#include "commands/repack.h"
#include "common/pg_prng.h"
#include "jit/jit.h"
#include "libpq/libpq.h"
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 2b609bfc824..5f8c766c4be 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -26,7 +26,6 @@
#include "catalog/toasting.h"
#include "commands/alter.h"
#include "commands/async.h"
-#include "commands/cluster.h"
#include "commands/collationcmds.h"
#include "commands/comment.h"
#include "commands/conversioncmds.h"
@@ -46,6 +45,7 @@
#include "commands/proclang.h"
#include "commands/propgraphcmds.h"
#include "commands/publicationcmds.h"
+#include "commands/repack.h"
#include "commands/schemacmds.h"
#include "commands/seclabel.h"
#include "commands/sequence.h"
diff --git a/src/include/commands/cluster.h b/src/include/commands/repack.h
similarity index 90%
rename from src/include/commands/cluster.h
rename to src/include/commands/repack.h
index d6b62c747e8..85061158b0c 100644
--- a/src/include/commands/cluster.h
+++ b/src/include/commands/repack.h
@@ -1,17 +1,17 @@
/*-------------------------------------------------------------------------
*
- * cluster.h
- * header file for postgres cluster command stuff
+ * repack.h
+ * header file for the REPACK command
*
* Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
* Portions Copyright (c) 1994-5, Regents of the University of California
*
- * src/include/commands/cluster.h
+ * src/include/commands/repack.h
*
*-------------------------------------------------------------------------
*/
-#ifndef CLUSTER_H
-#define CLUSTER_H
+#ifndef REPACK_H
+#define REPACK_H
#include "nodes/parsenodes.h"
#include "parser/parse_node.h"
@@ -52,4 +52,4 @@ extern void finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
MultiXactId cutoffMulti,
char newrelpersistence);
-#endif /* CLUSTER_H */
+#endif /* REPACK_H */
--
2.47.3
--cdhh4t7ukb6tnjoq
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v54-0003-Add-CONCURRENTLY-option-to-REPACK-command.patch"
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
* [PATCH 2/2] Publish list of tables being repacked in shared memory
@ 2026-04-07 20:29 Álvaro Herrera <[email protected]>
0 siblings, 0 replies; 57+ messages in thread
From: Álvaro Herrera @ 2026-04-07 20:29 UTC (permalink / raw)
Use it in autovacuum to skip processing tables that are being repacked.
This is mostly to avoid repeated attempts to process such tables, which
would fail due to the special deadlock checker behavior for repack.
Author: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/commands/repack.c | 195 ++++++++++++++++--
src/backend/postmaster/autovacuum.c | 20 ++
.../utils/activity/wait_event_names.txt | 1 +
src/include/commands/repack.h | 2 +
src/include/storage/lwlocklist.h | 2 +-
src/include/storage/subsystemlist.h | 1 +
src/tools/pgindent/typedefs.list | 3 +
7 files changed, 210 insertions(+), 14 deletions(-)
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index a5f5df77291..ee7072dce6a 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -63,9 +63,11 @@
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
+#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "storage/subsystems.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
@@ -79,6 +81,32 @@
#include "utils/syscache.h"
#include "utils/wait_event_types.h"
+
+/* Shared memory layout for REPACK */
+typedef struct RepackWorkerInfo
+{
+ bool ri_in_use;
+ pid_t ri_backendpid;
+ Oid ri_dbid;
+ Oid ri_relid;
+ Oid ri_toastrelid;
+} RepackWorkerInfo;
+
+typedef struct
+{
+ bool re_useless;
+ RepackWorkerInfo re_workerinfo[FLEXIBLE_ARRAY_MEMBER];
+} RepackShmemStruct;
+
+static RepackShmemStruct *RepackShmem;
+
+typedef struct RepackCleanupContext
+{
+ bool concurrent;
+ int workerindex;
+} RepackCleanupContext;
+
+
/*
* This struct is used to pass around the information on tables to be
* clustered. We need this so we can make a list of them when invoked without
@@ -90,6 +118,7 @@ typedef struct
Oid indexOid;
} RelToCluster;
+
/*
* The first file exported by the decoding worker must contain a snapshot, the
* following ones contain the data changes.
@@ -166,6 +195,10 @@ static List *get_tables_to_repack_partitioned(RepackCommand cmd,
MemoryContext permcxt);
static bool repack_is_permitted_for_relation(RepackCommand cmd,
Oid relid, Oid userid);
+static void RepackCleanup(RepackCleanupContext *context);
+static void RepackCleanupCb(int code, Datum arg);
+static void RepackShmemRequest(void *arg);
+static void RepackShmemInit(void *arg);
static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
@@ -210,6 +243,11 @@ static void ProcessRepackMessage(StringInfo msg);
static const char *RepackCommandAsString(RepackCommand cmd);
+const ShmemCallbacks RepackShmemCallbacks = {
+ .request_fn = RepackShmemRequest,
+ .init_fn = RepackShmemInit,
+};
+
/*
* The repack code allows for processing multiple tables at once. Because
* of this, we cannot just run everything on a single transaction, or we
@@ -514,6 +552,7 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
Oid tableOid = RelationGetRelid(OldHeap);
Relation index;
LOCKMODE lmode;
+ RepackCleanupContext context;
Oid save_userid;
int save_sec_context;
int save_nestlevel;
@@ -660,24 +699,43 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid,
TransferPredicateLocksToHeapRelation(OldHeap);
/* rebuild_relation does all the dirty work */
- PG_TRY();
- {
- rebuild_relation(OldHeap, index, verbose, ident_idx);
- }
- PG_FINALLY();
+ context.concurrent = concurrent;
+
+ PG_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
{
if (concurrent)
{
- /*
- * Since during normal operation the worker was already asked to
- * exit, stopping it explicitly is especially important on ERROR.
- * However it still seems a good practice to make sure that the
- * worker never survives the REPACK command.
- */
- stop_repack_decoding_worker();
+ bool freefound = false;
+
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *worker;
+
+ if (RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ freefound = true;
+ worker = &RepackShmem->re_workerinfo[i];
+ context.workerindex = i;
+
+ worker->ri_in_use = true;
+ worker->ri_backendpid = MyProcPid;
+ worker->ri_dbid = MyDatabaseId;
+ worker->ri_relid = RelationGetRelid(OldHeap);
+ worker->ri_toastrelid = OldHeap->rd_rel->reltoastrelid;
+ break;
+ }
+ if (!freefound)
+ elog(ERROR, "could not find free repack entry");
+ LWLockRelease(RepackLock);
}
+
+ rebuild_relation(OldHeap, index, verbose, ident_idx);
}
- PG_END_TRY();
+ PG_END_ENSURE_ERROR_CLEANUP(RepackCleanupCb, PointerGetDatum(&context));
+
+ RepackCleanup(&context);
/* rebuild_relation closes OldHeap, and index if valid */
@@ -691,6 +749,117 @@ out:
pgstat_progress_end_command();
}
+/*
+ * Return whether any backend is running concurrent REPACK on the given table
+ * (which could be a toast table).
+ */
+bool
+is_table_under_repack(Oid databaseId, Oid relid)
+{
+ bool retval = false;
+
+ LWLockAcquire(RepackLock, LW_SHARED);
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ RepackWorkerInfo *rworker;
+
+ if (!RepackShmem->re_workerinfo[i].ri_in_use)
+ continue;
+
+ rworker = &RepackShmem->re_workerinfo[i];
+ if (rworker->ri_dbid == MyDatabaseId &&
+ (rworker->ri_relid == relid ||
+ rworker->ri_toastrelid == relid))
+ retval = true;
+ }
+ LWLockRelease(RepackLock);
+
+ return retval;
+}
+
+/*
+ * Remove ourselves from the workerinfo array.
+ */
+static void
+RepackCleanup(RepackCleanupContext *context)
+{
+ if (context->concurrent)
+ {
+ RepackWorkerInfo *worker;
+
+ /*
+ * The worker would normally terminate on its own when the work is
+ * done, but make sure we signal it just in case.
+ */
+ stop_repack_decoding_worker();
+
+ /*
+ * also, make sure we stop advertising the relation we were repacking,
+ * so that autovacuum reverts to handling it normally.
+ */
+ LWLockAcquire(RepackLock, LW_EXCLUSIVE);
+
+ worker = &RepackShmem->re_workerinfo[context->workerindex];
+ Assert(worker->ri_backendpid == MyProcPid);
+ worker->ri_in_use = false;
+ worker->ri_backendpid = 0;
+ worker->ri_dbid = InvalidOid;
+ worker->ri_relid = InvalidOid;
+ worker->ri_toastrelid = InvalidOid;
+ LWLockRelease(RepackLock);
+ }
+}
+
+/*
+ * RepackCleanup wrapped as an on_shmem_exit callback function
+ */
+static void
+RepackCleanupCb(int code, Datum arg)
+{
+ RepackCleanup((RepackCleanupContext *) DatumGetPointer(arg));
+}
+
+/*
+ * RepackShmemRequest
+ * Register shared memory space needed for repack
+ */
+static void
+RepackShmemRequest(void *arg)
+{
+ Size size;
+
+ /*
+ * Need the fixed struct and the array of RepackWorkerInfo.
+ */
+ size = sizeof(RepackShmemStruct);
+ size = MAXALIGN(size);
+ size = add_size(size, mul_size(max_repack_replication_slots,
+ sizeof(RepackWorkerInfo)));
+
+ ShmemRequestStruct(.name = "Repack Data",
+ .size = size,
+ .ptr = (void **) &RepackShmem,
+ );
+}
+
+static void
+RepackShmemInit(void *arg)
+{
+ RepackWorkerInfo *reinfo;
+
+ reinfo = (RepackWorkerInfo *) ((char *) RepackShmem +
+ MAXALIGN(sizeof(RepackShmemStruct)));
+
+ for (int i = 0; i < max_repack_replication_slots; i++)
+ {
+ reinfo[i].ri_in_use = false;
+ reinfo[i].ri_backendpid = 0;
+ reinfo[i].ri_dbid = InvalidOid;
+ reinfo[i].ri_relid = InvalidOid;
+ reinfo[i].ri_toastrelid = InvalidOid;
+ }
+}
+
/*
* Check if the table (and its index) still meets the requirements of
* cluster_rel().
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index bd626a16363..080c64ea3c8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
+#include "commands/repack.h"
#include "commands/vacuum.h"
#include "common/int.h"
#include "funcapi.h"
@@ -2422,6 +2423,25 @@ do_autovacuum(void)
}
}
LWLockRelease(AutovacuumLock);
+
+ /*
+ * Similarly, if the table is being processed by concurrent repack,
+ * skip it (but make a note of that). We wouldn't be able to acquire
+ * its lock anyway.
+ */
+ if (!skipit)
+ {
+ MemoryContextSwitchTo(PortalContext);
+
+ skipit = is_table_under_repack(MyDatabaseId, relid);
+ if (skipit)
+ ereport(LOG,
+ errmsg("skipping table \"%s.%s.%s\" because it's being repacked in concurrent mode",
+ get_database_name(MyDatabaseId),
+ get_namespace_name(get_rel_namespace(relid)),
+ get_rel_name(relid)));
+ }
+
if (skipit)
{
LWLockRelease(AutovacuumScheduleLock);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 7bda5298558..e206304f204 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -332,6 +332,7 @@ SInvalWrite "Waiting to add a message to the shared catalog invalidation queue."
WALBufMapping "Waiting to replace a page in WAL buffers."
WALWrite "Waiting for WAL buffers to be written to disk."
ControlFile "Waiting to read or update the <filename>pg_control</filename> file or create a new WAL file."
+Repack "Waiting to read or update tables in process by concurrent repack."
MultiXactGen "Waiting to read or update shared multixact state."
RelCacheInit "Waiting to read or update a <filename>pg_internal.init</filename> relation cache initialization file."
CheckpointerComm "Waiting to manage fsync requests."
diff --git a/src/include/commands/repack.h b/src/include/commands/repack.h
index fd16e74b179..be7d38b5fae 100644
--- a/src/include/commands/repack.h
+++ b/src/include/commands/repack.h
@@ -42,6 +42,8 @@ extern void ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel);
extern void cluster_rel(RepackCommand command, Relation OldHeap, Oid indexOid,
ClusterParams *params, bool isTopLevel);
+extern bool is_table_under_repack(Oid databaseId, Oid relid);
+
extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
LOCKMODE lockmode);
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index af8553bcb6c..3f08f4a15d4 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -41,7 +41,7 @@ PG_LWLOCK(6, SInvalWrite)
PG_LWLOCK(7, WALBufMapping)
PG_LWLOCK(8, WALWrite)
PG_LWLOCK(9, ControlFile)
-/* 10 was CheckpointLock */
+PG_LWLOCK(10, Repack)
/* 11 was XactSLRULock */
/* 12 was SubtransSLRULock */
PG_LWLOCK(13, MultiXactGen)
diff --git a/src/include/storage/subsystemlist.h b/src/include/storage/subsystemlist.h
index 9ad619080be..4e683b8b0a8 100644
--- a/src/include/storage/subsystemlist.h
+++ b/src/include/storage/subsystemlist.h
@@ -72,6 +72,7 @@ PG_SHMEM_SUBSYSTEM(WalSummarizerShmemCallbacks)
PG_SHMEM_SUBSYSTEM(PgArchShmemCallbacks)
PG_SHMEM_SUBSYSTEM(ApplyLauncherShmemCallbacks)
PG_SHMEM_SUBSYSTEM(SlotSyncShmemCallbacks)
+PG_SHMEM_SUBSYSTEM(RepackShmemCallbacks)
/* other modules that need some shared memory space */
PG_SHMEM_SUBSYSTEM(BTreeShmemCallbacks)
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 637c669a146..d019e03aaf1 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2639,9 +2639,12 @@ ReorderBufferTupleCidEnt
ReorderBufferTupleCidKey
ReorderBufferUpdateProgressTxnCB
ReorderTuple
+RepackCleanupContext
RepackCommand
RepackDecodingState
+RepackShmemStruct
RepackStmt
+RepackWorkerInfo
ReparameterizeForeignPathByChild_function
ReplOriginId
ReplOriginXactState
--
2.47.3
--kdrcpfmkbkc4lqhu--
^ permalink raw reply [nested|flat] 57+ messages in thread
end of thread, other threads:[~2026-04-07 20:29 UTC | newest]
Thread overview: 57+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-31 16:55 [PATCH v54 2/5] Rename cluster.c/h -> repack.c/h Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Álvaro Herrera <[email protected]>
2026-04-07 20:29 [PATCH 2/2] Publish list of tables being repacked in shared memory Á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