# Benchmark results: parallel heap vacuum (v22)

Benchmark of the v22 patch set from
[CALj2ACX9BCAsCYcdCozfG=FxW3-xWw5p6y000m1LxLMrbdbNAQ](https://postgr.es/m/CALj2ACX9BCAsCYcdCozfG=FxW3-xWw5p6y000m1LxLMrbdbNAQ@mail.gmail.com).

## Environment

- **Instance:** AWS EC2 `i4i.4xlarge`: 16 vCPU (Intel Xeon Platinum 8375C @
  2.90GHz), 123GiB RAM, local NVMe instance-store (XFS), Amazon Linux 2023.
- **Build:** autoconf + make, `--disable-cassert`, `-O2`. `head` = unpatched
  master; `v22` = master + v22-0001/0002/0003.
- **GUCs:** `shared_buffers = 16GB`, `maintenance_work_mem = 1GB` (except
  the `maintenance_work_mem` sweep), `max_wal_size = 32GB`,
  `max_worker_processes = 32`, `max_parallel_workers = 32`,
  `max_parallel_maintenance_workers = 16`, `autovacuum = off`,
  `track_io_timing = on`.
- **Table:** `unlogged table test (a bigint)`, 8GB unless noted, btree
  index(es) on `a` where indicated, ~20% dead tuples per pass. Median of 5
  passes per cell.

Note on workers: the planner caps heap-scan workers by table size. At 8GB,
`workers = 8` and `workers = 16` both launch 6 (VACUUM VERBOSE: "launched 6
parallel vacuum workers for heap scanning (planned: 6)"); at 10GB, 8
launches 5.

## Index count x worker count

Median total VACUUM time, seconds.

| indexes | workers | head | v22 | speedup |
|---|---|---|---|---|
| 0 | 0  | 3.53  | 3.39  | 1.04x |
| 0 | 1  | 3.51  | 1.88  | 1.87x |
| 0 | 4  | 3.52  | 1.47  | 2.40x |
| 0 | 16 | 3.54  | 1.45  | 2.44x |
| 1 | 0  | 8.27  | 8.16  | 1.01x |
| 1 | 4  | 8.20  | 6.47  | 1.27x |
| 1 | 16 | 8.28  | 6.48  | 1.28x |
| 2 | 0  | 11.51 | 11.57 | 0.99x |
| 2 | 4  | 9.12  | 6.58  | 1.39x |
| 2 | 16 | 9.13  | 6.63  | 1.38x |
| 4 | 0  | 18.11 | 17.90 | 1.01x |
| 4 | 4  | 9.28  | 6.76  | 1.37x |
| 4 | 16 | 9.73  | 6.92  | 1.41x |

No regression at `workers = 0`. The zero-dead-tuple and 100k-row cases run
in single-digit milliseconds on both binaries.

## `vacuum_buffer_usage_limit`

`INDEXES=0, WORKERS=16`.

| ring buffer | head | v22 |
|---|---|---|
| 2MB   | 3.52 | 1.63 |
| 64MB  | 3.71 | 1.48 |
| 256MB | 3.68 | 0.96 |
| 1GB   | 3.47 | 0.90 |

head is flat; v22 improves up to about 256MB-1GB.

## Concurrent write load

`INDEXES={0,2}, WORKERS=16`, 4 clients doing INSERT+UPDATE during VACUUM.

| indexes | head median (min-max) | v22 median (min-max) |
|---|---|---|
| 0 | 0.016 (0.012-1.411) | 5.015 (0.030-5.021) |
| 2 | 5.020 (3.599-6.226) | 5.020 (3.701-5.952) |

Zero write errors in all 20 runs. At `nidx = 0`, 3 of 5 v22 passes took
~5.02s with 0.01s CPU, and VERBOSE reports "stopping truncate due to
conflicting lock request", so the time is the truncate lock backoff, not
scan work. Not yet checked whether this is specific to v22 or just this
script's timing.

## `maintenance_work_mem`

`INDEXES=2, WORKERS=16`, contiguous dead-tuple range.

| maintenance_work_mem | head | v22 | dead_item_resets |
|---|---|---|---|
| 64MB  | 9.13 | 6.89 | 1 |
| 256MB | 9.10 | 6.92 | 1 |
| 1GB   | 9.16 | 7.09 | 1 |
| 4GB   | 9.28 | 6.78 | 1 |

A contiguous dead range never forces a second reset at 8GB, so this test
doesn't exercise the knob.

## Chunk size (`PARALLEL_LV_CHUNK_SIZE`)

10GB table, 0 indexes, seconds (speedup vs head at the same worker count):

| chunk (blocks) | w=0 | w=2 | w=4 | w=8 |
|---|---|---|---|---|
| head            | 1.114 | 1.111 | 1.114 | 1.120 |
| 1024 (default)  | 1.107 (1.01x) | 0.515 (2.16x) | 0.523 (2.13x) | 0.502 (2.23x) |
| 4096            | 1.116 (1.00x) | 0.543 (2.04x) | 0.512 (2.18x) | 0.486 (2.31x) |
| 8192            | 1.115 (1.00x) | 0.560 (1.99x) | 0.498 (2.24x) | 0.475 (2.36x) |
| 32768           | 1.121 (0.99x) | 0.594 (1.87x) | 0.356 (3.13x) | 0.368 (3.04x) |

32768 gives ~3.1x at 4 workers vs ~2.1x at 1024; at 2 workers it is
slightly slower. `w=8` launches 5 workers here, so `w=4` and `w=8` are
about the same.

## Testing

No crash, assertion failure or wrong result found, on an assert-enabled
build and on a test build with 2-block chunks and no size threshold (so
almost every VACUUM goes parallel):

- make check, isolation, amcheck, pg_visibility, autovacuum TAP, and
  027_stream_regress (on the test build).
- Parallel vs serial on identical tables: same page contents,
  all-visible/frozen counts, amcheck and pg_visibility clean. Covered up
  to 258 index passes, sizes around the threshold, FREEZE,
  DISABLE_PAGE_SKIPPING, INDEX_CLEANUP off, truncation, toast.
- Forced failsafe: index vacuuming stops and relfrozenxid advances.
- Cancel/terminate of leader and workers: clean errors, no leftover
  workers, next VACUUM fine.
- ~600 parallel VACUUMs during 11-19k tps of updates/deletes/upserts:
  zero errors.

Two small behaviors, not correctness bugs: each worker is a fresh process,
so the first page it prunes skips opportunistic freezing (about one extra
all-visible-but-not-frozen page per worker per pass); and with
`maintenance_work_mem` below 256kB nearly every chunk ends in an index
pass, as parallel index vacuum already does on master.

## Summary

1. No regression at `workers = 0`, any index count.
2. Up to 2.4x with no indexes, 1.3-1.4x with indexes.
3. No errors under concurrent writes.
4. `PARALLEL_LV_CHUNK_SIZE` matters: 32768 gives ~3.1x at 4 workers vs
   ~2.1x at 1024.
5. No bugs found in testing.

Happy to share raw CSVs or scripts.
