Skip to content

Commit 95909f3

Browse files
committed
Ensure ANALYZE phase is not skipped because of canceled truncate.
Patch b19e425 attempted to preserve existing behavior regarding statistics generation in the case that a truncation attempt was canceled due to lock conflicts. It failed to do this accurately in two regards: (1) autovacuum had previously generated statistics if the truncate attempt failed to initially get the lock rather than having started the attempt, and (2) the VACUUM ANALYZE command had always generated statistics. Both of these changes were unintended, and are reverted by this patch. On review, there seems to be consensus that the previous failure to generate statistics when the truncate was terminated was more an unfortunate consequence of how that effort was previously terminated than a feature we want to keep; so this patch generates statistics even when an autovacuum truncation attempt terminates early. Another unintended change which is kept on the basis that it is an improvement is that when a VACUUM command is truncating, it will the new heuristic for avoiding blocking other processes, rather than keeping an AccessExclusiveLock on the table for however long the truncation takes. Per multiple reports, with some renaming per patch by Jeff Janes. Backpatch to 9.0, where problem was created.
1 parent 4dbe52d commit 95909f3

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

src/backend/commands/vacuumlazy.c

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@
7575
* that the potential for improvement was great enough to merit the cost of
7676
* supporting them.
7777
*/
78-
#define AUTOVACUUM_TRUNCATE_LOCK_CHECK_INTERVAL 20 /* ms */
79-
#define AUTOVACUUM_TRUNCATE_LOCK_WAIT_INTERVAL 50 /* ms */
80-
#define AUTOVACUUM_TRUNCATE_LOCK_TIMEOUT 5000 /* ms */
78+
#define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL 20 /* ms */
79+
#define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL 50 /* ms */
80+
#define VACUUM_TRUNCATE_LOCK_TIMEOUT 5000 /* ms */
8181

8282
/*
8383
* Guesstimation of number of dead tuples per page. This is used to
@@ -272,17 +272,10 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
272272
vacrelstats->hasindex,
273273
new_frozen_xid);
274274

275-
/*
276-
* Report results to the stats collector, too. An early terminated
277-
* lazy_truncate_heap attempt suppresses the message and also cancels the
278-
* execution of ANALYZE, if that was ordered.
279-
*/
280-
if (!vacrelstats->lock_waiter_detected)
281-
pgstat_report_vacuum(RelationGetRelid(onerel),
282-
onerel->rd_rel->relisshared,
283-
new_rel_tuples);
284-
else
285-
vacstmt->options &= ~VACOPT_ANALYZE;
275+
/* report results to the stats collector, too */
276+
pgstat_report_vacuum(RelationGetRelid(onerel),
277+
onerel->rd_rel->relisshared,
278+
new_rel_tuples);
286279

287280
/* and log the action if appropriate */
288281
if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration >= 0)
@@ -1306,28 +1299,21 @@ lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats)
13061299
*/
13071300
CHECK_FOR_INTERRUPTS();
13081301

1309-
if (++lock_retry > (AUTOVACUUM_TRUNCATE_LOCK_TIMEOUT /
1310-
AUTOVACUUM_TRUNCATE_LOCK_WAIT_INTERVAL))
1302+
if (++lock_retry > (VACUUM_TRUNCATE_LOCK_TIMEOUT /
1303+
VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL))
13111304
{
13121305
/*
13131306
* We failed to establish the lock in the specified number of
1314-
* retries. This means we give up truncating. Suppress the
1315-
* ANALYZE step. Doing an ANALYZE at this point will reset the
1316-
* dead_tuple_count in the stats collector, so we will not get
1317-
* called by the autovacuum launcher again to do the truncate.
1307+
* retries. This means we give up truncating.
13181308
*/
13191309
vacrelstats->lock_waiter_detected = true;
1320-
ereport(LOG,
1321-
(errmsg("automatic vacuum of table \"%s.%s.%s\": "
1322-
"could not (re)acquire exclusive "
1323-
"lock for truncate scan",
1324-
get_database_name(MyDatabaseId),
1325-
get_namespace_name(RelationGetNamespace(onerel)),
1310+
ereport(elevel,
1311+
(errmsg("\"%s\": stopping truncate due to conflicting lock request",
13261312
RelationGetRelationName(onerel))));
13271313
return;
13281314
}
13291315

1330-
pg_usleep(AUTOVACUUM_TRUNCATE_LOCK_WAIT_INTERVAL);
1316+
pg_usleep(VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL);
13311317
}
13321318

13331319
/*
@@ -1407,8 +1393,6 @@ count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
14071393
{
14081394
BlockNumber blkno;
14091395
instr_time starttime;
1410-
instr_time currenttime;
1411-
instr_time elapsed;
14121396

14131397
/* Initialize the starttime if we check for conflicting lock requests */
14141398
INSTR_TIME_SET_CURRENT(starttime);
@@ -1426,24 +1410,26 @@ count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
14261410
/*
14271411
* Check if another process requests a lock on our relation. We are
14281412
* holding an AccessExclusiveLock here, so they will be waiting. We
1429-
* only do this in autovacuum_truncate_lock_check millisecond
1430-
* intervals, and we only check if that interval has elapsed once
1431-
* every 32 blocks to keep the number of system calls and actual
1432-
* shared lock table lookups to a minimum.
1413+
* only do this once per VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL, and we
1414+
* only check if that interval has elapsed once every 32 blocks to
1415+
* keep the number of system calls and actual shared lock table
1416+
* lookups to a minimum.
14331417
*/
14341418
if ((blkno % 32) == 0)
14351419
{
1420+
instr_time currenttime;
1421+
instr_time elapsed;
1422+
14361423
INSTR_TIME_SET_CURRENT(currenttime);
14371424
elapsed = currenttime;
14381425
INSTR_TIME_SUBTRACT(elapsed, starttime);
14391426
if ((INSTR_TIME_GET_MICROSEC(elapsed) / 1000)
1440-
>= AUTOVACUUM_TRUNCATE_LOCK_CHECK_INTERVAL)
1427+
>= VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL)
14411428
{
14421429
if (LockHasWaitersRelation(onerel, AccessExclusiveLock))
14431430
{
14441431
ereport(elevel,
1445-
(errmsg("\"%s\": suspending truncate "
1446-
"due to conflicting lock request",
1432+
(errmsg("\"%s\": suspending truncate due to conflicting lock request",
14471433
RelationGetRelationName(onerel))));
14481434

14491435
vacrelstats->lock_waiter_detected = true;

0 commit comments

Comments
 (0)