Use WaitLatch() instead of pg_usleep() at end-of-vacuum truncation
authorMichael Paquier <[email protected]>
Fri, 2 Jul 2021 03:58:34 +0000 (12:58 +0900)
committerMichael Paquier <[email protected]>
Fri, 2 Jul 2021 03:58:34 +0000 (12:58 +0900)
This has the advantage to make a process more responsive when the
postmaster dies, even if the wait time was rather limited as there was
only a 50ms timeout here.  Another advantage of this change is for
monitoring, as we gain a new wait event for the end-of-vacuum
truncation.

Author: Bharath Rupireddy
Reviewed-by: Aleksander Alekseev, Thomas Munro, Michael Paquier
Discussion: https://postgr.es/m/CALj2ACU4AdPCq6NLfcA-ZGwX7pPCK5FgEj-CAU0xCKzkASSy_A@mail.gmail.com

doc/src/sgml/monitoring.sgml
src/backend/access/heap/vacuumlazy.c
src/backend/utils/activity/wait_event.c
src/include/utils/wait_event.h

index 07a042254f986a60a69baecee88bfb62dcd3e7bd..643e1ad49fd887e540dae59d9b51ed209aafefaf 100644 (file)
@@ -2242,6 +2242,11 @@ postgres   27093  0.0  0.0  30096  2752 ?        Ss   11:34   0:00 postgres: ser
       <entry><literal>VacuumDelay</literal></entry>
       <entry>Waiting in a cost-based vacuum delay point.</entry>
      </row>
+     <row>
+      <entry><literal>VacuumTruncate</literal></entry>
+      <entry>Waiting to acquire an exclusive lock to truncate off any
+       empty pages at the end of a table vacuumed.</entry>
+     </row>
     </tbody>
    </tgroup>
   </table>
index 44f198398c19d57258a3c3134eba7da31e24acbb..2c04b69221f449bcf106a2ba89e0631dcee43469 100644 (file)
@@ -3236,7 +3236,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);
+           ResetLatch(MyLatch);
        }
 
        /*
index 6baf67740c7dd4823554abc27697ee00cd210340..ef7e6bfb779ffa05980304ad5f3a7a976a3c7d85 100644 (file)
@@ -485,6 +485,9 @@ pgstat_get_wait_timeout(WaitEventTimeout w)
        case WAIT_EVENT_VACUUM_DELAY:
            event_name = "VacuumDelay";
            break;
+       case WAIT_EVENT_VACUUM_TRUNCATE:
+           event_name = "VacuumTruncate";
+           break;
            /* no default case, so that compiler will warn */
    }
 
index 6c6ec2e7118fa35c846bfd580a837e00b31f1ab7..6007827b44588a176c0e66f18df5affe366e007a 100644 (file)
@@ -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
 } WaitEventTimeout;
 
 /* ----------