pgsql: Fix performance problems with autovacuum truncation in busy work

From: Kevin Grittner <kgrittn(at)postgresql(dot)org>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix performance problems with autovacuum truncation in busy work
Date: 2012-12-11 20:35:09
Message-ID: E1TiWXl-0001Zv-Vh@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix performance problems with autovacuum truncation in busy workloads.

In situations where there are over 8MB of empty pages at the end of
a table, the truncation work for trailing empty pages takes longer
than deadlock_timeout, and there is frequent access to the table by
processes other than autovacuum, there was a problem with the
autovacuum worker process being canceled by the deadlock checking
code. The truncation work done by autovacuum up that point was
lost, and the attempt tried again by a later autovacuum worker. The
attempts could continue indefinitely without making progress,
consuming resources and blocking other processes for up to
deadlock_timeout each time.

This patch has the autovacuum worker checking whether it is
blocking any other thread at 20ms intervals. If such a condition
develops, the autovacuum worker will persist the work it has done
so far, release its lock on the table, and sleep in 50ms intervals
for up to 5 seconds, hoping to be able to re-acquire the lock and
try again. If it is unable to get the lock in that time, it moves
on and a worker will try to continue later from the point this one
left off.

While this patch doesn't change the rules about when and what to
truncate, it does cause the truncation to occur sooner, with less
blocking, and with the consumption of fewer resources when there is
contention for the table's lock.

The only user-visible change other than improved performance is
that the table size during truncation may change incrementally
instead of just once.

This problem exists in all supported versions but is infrequently
reported, although some reports of performance problems when
autovacuum runs might be caused by this. Initial commit is just the
master branch, but this should probably be backpatched once the
build farm and general developer usage confirm that there are no
surprising effects.

Jan Wieck

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/b19e4250b45e91c9cbdd18d35ea6391ab5961c8d

Modified Files
--------------
src/backend/commands/vacuumlazy.c | 230 ++++++++++++++++++++++++++----------
src/backend/storage/lmgr/lmgr.c | 18 +++
src/backend/storage/lmgr/lock.c | 92 +++++++++++++++
src/include/storage/lmgr.h | 1 +
src/include/storage/lock.h | 2 +
5 files changed, 279 insertions(+), 64 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2012-12-12 00:28:40 pgsql: Disable event triggers in standalone mode.
Previous Message Bruce Momjian 2012-12-11 20:09:31 pgsql: Fix pg_upgrade for invalid indexes