From 4cd54df7e27efb5dd82751fd8c7d15c475ce4a40 Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Mon, 19 Apr 2021 19:25:02 +0530 Subject: [PATCH v1] Use a WaitLatch for lock waiting in lazy_truncate_heap Instead of using pg_usleep() in lazy_truncate_heap(), use a WaitLatch. This has the advantage that we will realize if the postmaster has been killed since the last time we decided to sleep. --- doc/src/sgml/monitoring.sgml | 5 +++++ src/backend/access/heap/vacuumlazy.c | 6 +++++- src/backend/utils/activity/wait_event.c | 3 +++ src/include/utils/wait_event.h | 3 ++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 5cf083bb77..65d51ce445 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -2249,6 +2249,11 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser VacuumDelay Waiting in a cost-based vacuum delay point. + + VacuumTruncateLock + Waiting to acquire exclusive lock on relation for truncation while + vacuuming. + diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c index e90fc18aa9..dcd598fca8 100644 --- a/src/backend/access/heap/vacuumlazy.c +++ b/src/backend/access/heap/vacuumlazy.c @@ -3188,7 +3188,11 @@ lazy_truncate_heap(LVRelState *vacrel) return; } - pg_usleep(VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL * 1000L); + (void) WaitLatch(MyLatch, + WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL, + WAIT_EVENT_VACUUM_TRUNCATE_LOCK); + ResetLatch(MyLatch); } /* diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index 89b5b8b7b9..694cd48315 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -485,6 +485,9 @@ pgstat_get_wait_timeout(WaitEventTimeout w) case WAIT_EVENT_VACUUM_DELAY: event_name = "VacuumDelay"; break; + case WAIT_EVENT_VACUUM_TRUNCATE_LOCK: + event_name = "VacuumTruncateLock"; + break; /* no default case, so that compiler will warn */ } diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index 47accc5ffe..60b8ca76f7 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -140,7 +140,8 @@ typedef enum WAIT_EVENT_PG_SLEEP, WAIT_EVENT_RECOVERY_APPLY_DELAY, WAIT_EVENT_RECOVERY_RETRIEVE_RETRY_INTERVAL, - WAIT_EVENT_VACUUM_DELAY + WAIT_EVENT_VACUUM_DELAY, + WAIT_EVENT_VACUUM_TRUNCATE_LOCK } WaitEventTimeout; /* ---------- -- 2.25.1