From a56869c1da23f56d747d2d05695879d6659cb8ba Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 14 Aug 2017 13:57:50 -0300 Subject: [PATCH 2/2] Don't repeatedly try to attach to shared memory If the first attempt fails, give up on it. Reported-by: Robert Haas Discussion: https://postgr.es/m/CA+TgmobQVbz4K_+RSmiM9HeRKpy3vS5xnbkL95gSEnWijzprKQ@mail.gmail.com --- src/backend/postmaster/autovacuum.c | 45 ++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index ed1a288475..ac47b1ce1b 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -255,6 +255,7 @@ typedef enum * av_startingWorker pointer to WorkerInfo currently being started (cleared by * the worker itself as soon as it's up and running) * av_dsa_handle handle for allocatable shared memory + * av_dsa_failed already tried to allocate shared memory and failed * * This struct is protected by AutovacuumLock, except for av_signal and parts * of the worker list (see above). av_dsa_handle is readable unlocked. @@ -268,6 +269,7 @@ typedef struct dlist_head av_runningWorkers; WorkerInfo av_startingWorker; dsa_handle av_dsa_handle; + bool av_dsa_failed; dsa_pointer av_workitems; } AutoVacuumShmemStruct; @@ -621,22 +623,35 @@ AutoVacLauncherMain(int argc, char *argv[]) * already exist as created by a previous launcher; and we may even be * already attached to it, if we're here after longjmp'ing above. */ - if (!AutoVacuumShmem->av_dsa_handle) + if (!AutoVacuumShmem->av_dsa_failed) { - LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE); - AutoVacuumDSA = dsa_create(AutovacuumLock->tranche); - /* make sure it doesn't go away even if we do */ - dsa_pin(AutoVacuumDSA); - dsa_pin_mapping(AutoVacuumDSA); - AutoVacuumShmem->av_dsa_handle = dsa_get_handle(AutoVacuumDSA); - /* delay array allocation until first request */ - AutoVacuumShmem->av_workitems = InvalidDsaPointer; - LWLockRelease(AutovacuumLock); - } - else if (AutoVacuumDSA == NULL) - { - AutoVacuumDSA = dsa_attach(AutoVacuumShmem->av_dsa_handle); - dsa_pin_mapping(AutoVacuumDSA); + PG_TRY(); + { + if (!AutoVacuumShmem->av_dsa_handle) + { + LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE); + AutoVacuumDSA = dsa_create(AutovacuumLock->tranche); + /* make sure it doesn't go away even if we do */ + dsa_pin(AutoVacuumDSA); + dsa_pin_mapping(AutoVacuumDSA); + AutoVacuumShmem->av_dsa_handle = dsa_get_handle(AutoVacuumDSA); + + /* delay array allocation until first request */ + AutoVacuumShmem->av_workitems = InvalidDsaPointer; + LWLockRelease(AutovacuumLock); + } + else if (AutoVacuumDSA == NULL) + { + AutoVacuumDSA = dsa_attach(AutoVacuumShmem->av_dsa_handle); + dsa_pin_mapping(AutoVacuumDSA); + } + } + PG_CATCH(); + { + AutoVacuumShmem->av_dsa_failed = true; + PG_RE_THROW(); + } + PG_END_TRY(); } /* loop until shutdown request */ -- 2.11.0