Subject: Does cancelling autovacuum make you lose all the work it did?

From: "Greg Rychlewski (LCL)" <[email-masked]>
Date: Fri, 12 Jun 2020 10:18:38 +0000

I have a long running autovacuum (to prevent wraparound) that has been doing on for 6 days. I’m considering changing the autovacuum settings to be more aggressive, cancelling the current one, and then let it restart. Would the tuples its cleaned be lost if I did this?

This email message is confidential, may be legally privileged and is intended for the exclusive use of the addressee. If you received this message in error or are not the intended recipient, you should destroy the email message and any attachments or copies, and you are prohibited from retaining, distributing, disclosing or using any information contained. Please inform us of the delivery error by return email. Thank you for your cooperation.

Le présent message électronique est confidentiel et peut être couvert par le secret professionnel. Il est à l’usage exclusif du destinataire. Si vous recevez ce message par erreur ou si vous n’en êtes pas le destinataire prévu, vous devez détruire le message et toute pièce jointe ou copie et vous êtes tenu de ne pas conserver, distribuer, divulguer ni utiliser tout renseignement qu’il contient. Veuillez nous informer de toute erreur d’envoi en répondant à ce message. Merci de votre collaboration.

---

From: Stephen Froehlich <[email-masked]>
Date: Fri, 12 Jun 2020 17:40:45 +0000

Hi Greg,

No autovacuum or vacuum causes no user-visible or breaking changes of any kind … it does change the performance of the database backend, but that’s all.

--Stephen

From: Greg Rychlewski (LCL) <[email-masked]>
Sent: Friday, June 12, 2020 04:19
To: [email-masked]
Subject: Does cancelling autovacuum make you lose all the work it did?

---

From: "David G. Johnston" <[email-masked]>
Date: Fri, 12 Jun 2020 10:56:08 -0700

The custom here is to inline or bottom-post.

On Fri, Jun 12, 2020 at 10:41 AM Stephen Froehlich <
[email-masked]> wrote:

> No autovacuum or vacuum causes no user-visible or breaking changes of any
> kind … it does change the performance of the database backend, but that’s
> all.
>

Um...doesn't really answer the question and besides hitting the wrap-around
limit causes the server to not start in multi-user mode which is surely a
user-visible effect.  Performance is a user-visible effect too - the only
thing it better not impact is the correctness of the results.

To answer the original question: vacuum's effects are basically immediate
so work is not lost if it gets cancelled.

David J.

---

From: Tom Lane <[email-masked]>
Date: Fri, 12 Jun 2020 16:33:46 -0400

> so work is not lost if it gets cancelled.

Mmm ... not entirely true.  vacuum does a cycle like this:

1. scan table looking for removable rows; remember their TIDs

2. when memory for said TIDs is full, make a pass over the table's
indexes to find and delete index entries pointing at those TIDs

3. then, go back to the heap pages and actually remove the rows

4. if we didn't reach the end of the table yet, return to step 1.

If we get cancelled at any point, the data structure is still
consistent; there may be heap rows that don't have a full set
of index entries, but that doesn't matter because nobody cares
about finding those entries from the indexes.  However, when
you redo the vacuum, it'll have to redo some of the work from
the current cycle, depending on exactly how far along it was
when you cancelled it.  At the very least it's going to be
repeating some heap-scanning work, though that's the cheapest
part of this because it's basically read-only.  The most
expensive parts are the actual index and heap tuple removals,
and any one of those won't need to be done over.

The size of the cycles depends on maintenance_work_mem; so if
you have that set really large, you can lose more time than
if it's not so large.

TBH, though, are you sure the vacuum is doing work and not
just blocked waiting for somebody else?  An autovac can be
blocked indefinitely by some other query holding a table-level
or page-level lock.  Check its state in pg_stat_activity.

			regards, tom lane

---

From: "Greg Rychlewski (LCL)" <[email-masked]>
Date: Sat, 13 Jun 2020 00:32:14 +0000

Hi Tom,

Thanks for your reply. Regarding it being blocked, I don't think it is but I could be wrong. Both wait_event and wait_event_type are null. One thing I noticed though is that n_dead_tup in pg_stat_all_tables either stays the same or goes up. Should this be going down during the autovacuum?

On 2020-06-12, 4:33 PM, "Tom Lane" <[email-masked]> wrote:

    CAUTION: External email. Do not click links or open attachments unless you recognize the sender and know the content is safe.

    regards, tom lane

---

From: Tom Lane <[email-masked]>
Date: Fri, 12 Jun 2020 23:18:28 -0400

> Thanks for your reply. Regarding it being blocked, I don't think it is but I could be wrong. Both wait_event and wait_event_type are null. One thing I noticed though is that n_dead_tup in pg_stat_a…

No ... ongoing transactions could be creating new dead rows.  IIRC,
a vacuum won't report its removal of dead rows to the stats collector
till the very end, so what I'd expect is a big drop when it finishes.

(I am not sure offhand what happens to those stats if you cancel the
vacuum partway through --- it might report nothing, causing the
stats to be way off until the next successful vacuum.)

			regards, tom lane