← Blog2026-08-28Engineering

Measuring Postgres queue table growth: linear fit, heap extensions and peak-to-trough compared

By Michael Cooper, Founder

We ran a pg-boss job table at 8,000 jobs/s for eleven and a half hours to answer one question: does a one-hour retention window keep it bounded? The heap is bounded. Two common ways to test for growth gave wrong answers on the same data, and the indexes were still growing when the run ended.

Summary

A Postgres heap under a retention window does not creep and it does not sit still. It reaches a high-water mark and then oscillates, including downward, because VACUUM truncates trailing empty pages and hands that space back to the operating system.

That property defeats the usual ways to test whether a table is growing. Fit a line to the whole post-fill window and you get +3.2% a day; fit it to the worst 20 minutes and you get +72% a day. Count file extensions and wait for quiet and you get settled, three hours before the next extension. Take the peak-to-trough range over a window holding many cycles and you get a 0.84% band ending below its own peak.

On the same run the indexes reached 2,387 MB and were still growing at about +26 MB/h when the run ended.

What was measured, and on what

One m7i.xlarge (4 vCPU, non-burstable) per rate point, one rate point per instance, so no two points share I/O. PostgreSQL 18.6 in Docker on the same instance as the load driver, 200 GB gp3 at 6,000 IOPS and 250 MB/s. Server settings shared_buffers=4GB, work_mem=32MB, maintenance_work_mem=512MB, max_wal_size=8GB, checkpoint_timeout=15min. Both fsync and synchronous_commit are on.

Queue configuration: deleteAfterSeconds: 3600, maintenanceIntervalSeconds: 30, expireInSeconds: 300, retryLimit: 5 with backoff. The handler is a no-op, which maximises churn per second.

Measure the leaf partition

pg-boss creates its job table with PARTITION BY LIST (name). A partitioned parent has no storage of its own, so pg_relation_size('pgboss.job') returns 0 no matter how much data is in the queue. Both rows, from the 8,000 jobs/s run reported below, sampled at t=28,206s:

  relname   |  heap_bytes  | indexes_bytes
------------+--------------+---------------
 job        |            0 |             0
 job_common |   9235193856 |    2345754624

Every size in this post is the leaf. One queue means one leaf partition, job_common, sampled every 30 seconds with:

SELECT relname,
       n_live_tup, n_dead_tup,
       vacuum_count, autovacuum_count, analyze_count, autoanalyze_count,
       pg_relation_size(relid) AS heap_bytes,
       pg_indexes_size(relid)  AS indexes_bytes
FROM pg_stat_user_tables
WHERE schemaname = 'pgboss';

pg_stat_user_tables lists only relations with storage, which is why this returns the leaves and skips the parent. If you prefer to work from the parent, sum across pg_partition_tree(). Heap and index sizes are exact. Dead-tuple figures are the statistics collector’s estimate, reported throughout as n_dead_tup / (n_live_tup + n_dead_tup) and read as a median over a window, not off a single sample. Sizes are quoted in decimal MB (bytes divided by one million), so they read about 5% larger than the MiB pg_size_pretty prints. A "heap extension" below means an increase of more than 1 MB between consecutive samples.

1. Why does fitting a line to table size invent a trend?

Because a heap has no slope to fit. It changes in discrete page extensions and truncations, and a line averages them into a rate that describes neither.

Least squares across the entire post-fill window, with no window selection, 10.3 hours and 1,236 samples:

window          t=4200s to t=41255s  (10.3 hours, all post-fill samples)
least-squares   +12.3 MB/h  on a 9089 MB table
extrapolated    +0.14%/h, about +3.2% per day

VERDICT: growing.

That is the wrong answer, on a table whose last sample is 0.45% below its own all-time peak. A series that steps up and then truncates back down has a positive least-squares slope whenever the sampling window happens to contain more of the rising edges, and there is no window length that reliably fixes it.

Window selection makes it worse. The steepest 20-minute window in the post-fill series reports this:

window          t=17254s to t=18454s  (worst 20 minutes, selected adversarially)
least-squares   +277 MB/h  on a 9233 MB table
extrapolated    +3.0%/h, about +72% per day

VERDICT: growing, and fast enough to page someone.

That window spans 1,200 seconds and contains four distinct heap values, rising from 9,233.1 MB to 9,296.2 MB and then stopping. It is the steepest 20 minutes in the run, so it is an upper bound on how wrong a fit can be. The full-window fit above is the typical result.

2. Why doesn’t counting heap file extensions work?

Because it watches one edge of the cycle. Extensions are visible between samples. Truncations are not events, and nothing fires when the file gets smaller.

Counting the steps and waiting for them to stop:

heap file extensions after the fill: 17
longest quiet stretch   171 minutes, t=17947s to t=28206s

VERDICT: settled. Any reasonable stopping rule fires here.
...and then it extended again at t=28206s.

Two things are wrong with that verdict. First, the heap extended again 171 minutes later and kept extending until t=38,767s, near the end of the run. An hour of no change and two hours of no change both fire inside that gap.

Second, the table was not idle during the quiet stretch. It took six distinct values, falling from a peak of 9,296.3 MB to a trough of 9,218.8 MB before ending the stretch at 9,235.2 MB: 77.5 MB peak to trough, 61.0 MB start to end. It also moved by 65,536 bytes at t=19,835s, which is eight pages and far under the 1 MB threshold, so "byte-identical" would be wrong even where the megabyte figure holds still. The tool called the stretch quiet because it detects an extension as an increase between samples and cannot see a decrease at all.

The structural problem is worse than the false stop. A method built on extensions has an output space containing only "grew" and "silent". On a series that rises and falls it cannot return the right answer for any window you give it.

3. What does a bounded queue table actually look like?

A narrow band, entered from below, ending at or under its own peak. It is not a flat line.

window          t=20628s to t=41255s  (5.7 hours, many retention cycles)
trough / peak   9218.8 MB / 9296.3 MB
band            0.84%
ended           9254.8 MB, -0.45% against its all-time peak

VERDICT: bounded. It breathes inside a 1% band and ends below its peak.

The mechanism is documented behaviour. The PostgreSQL manual states that standard VACUUM "will not return the space to the operating system, except in the special case where one or more pages at the end of a table become entirely free and an exclusive table lock can be easily obtained". Under a retention window that deletes in age order, trailing pages emptying is the normal case, so truncation fires repeatedly and the file oscillates.

Same series, three methods, with the fit shown at two window choices:

MethodReportsAction it implies
Least-squares, whole post-fill window+3.2% / dayPlan a migration
Least-squares, worst 20-minute window+72% / dayPage someone
Count extensions, wait for quietsettledShip it
Peak-to-trough, multi-cycle window0.84% bandBounded, size it and move on

4. How long do you have to run before a plateau is real?

At three of the four rates measured, the heap was still taking extensions nine to eleven retention cycles in.

A two-hour run against a one-hour retention window contains one deletion cycle: enough to see the fill and the first delete, not enough to distinguish a plateau from a slow slope. The last heap extension on each arm of the completed run landed here:

RateLast extension at= retention cyclesRun length
1,000 jobs/s3,690s1.0211.4 h
2,000 jobs/s34,261s9.5211.4 h
4,000 jobs/s34,352s9.5411.4 h
8,000 jobs/s38,767s10.7711.5 h

Only the 1,000 jobs/s arm reached a final extension early. The other three were still occasionally extending in the last hour of an eleven-hour run, so they are bounded in band terms and never stop moving. "It stopped extending" is not a state a queue table under continuous load reliably reaches, so it is not a test you can wait for.

A shorter benchmark cannot show this. At 8,000 jobs/s the last extension arrives at 10.77 retention cycles. A two-hour run sees one, a four-hour run sees four, and none of them can tell you what the twelve-hour run does.

5. What does one hour of retention actually cost?

Between 1.11 and 1.16 GB of heap per 1,000 jobs/s, rising slightly with rate.

Second half of each run, entirely post-fill. The first four rows are the configuration AGLedger ships; the last is a stock-autovacuum control at the same rate, included for reference and discussed in the next section.

RateTroughPeakBandEnded vs all-time peakGB per 1k/s, at peak
1,000 jobs/s1,109.1 MB1,109.9 MB0.07%-0.03%1.110
2,000 jobs/s2,222.3 MB2,226.2 MB0.18%-0.05%1.113
4,000 jobs/s4,483.0 MB4,581.8 MB2.20%-2.02%1.145
8,000 jobs/s9,218.8 MB9,296.3 MB0.84%-0.45%1.162
8,000, stock autovacuum11,764.0 MB12,151.8 MB3.30%-3.19%1.519

The last column is the second-half peak divided by the rate. The cost per 1,000 jobs/s is not quite linear: 1.110, 1.113, 1.145 and 1.162 GB as the rate goes up eightfold. Every arm ended below its own peak.

6. Does autovacuum tuning change the shape or the size?

Both. On this run it holds the heap 23.6% smaller and the band four times tighter.

The tuned arm sets autovacuum_vacuum_scale_factor and autovacuum_analyze_scale_factor to 0.01 on the job partition, against PostgreSQL defaults of 0.2 and 0.1. Both arms below are the same rig at 8,000 jobs/s with a one-hour retention window, compared over the window both runs cover (t=20,646s to t=37,715s), medians, not single samples.

MeasureStockTunedDelta
Heap, median12,108.7 MB9,250.4 MB23.6% smaller
Indexes, median2,793.5 MB2,349.4 MB15.9% smaller
Dead tuples, median22.0%4.4%5x lower
Peak-to-trough band3.30%0.84%4x tighter
Autovacuum cadence0.21 / min1.00 / minat the naptime floor
Autoanalyze cadence0.25 / min0.98 / min4x more often

autovacuum_naptime defaults to one minute, so one run per minute is the ceiling any scale factor can buy. The tuned arm runs at that ceiling for both vacuum and analyze. Any scale factor low enough to keep the table over threshold every naptime produces the same cadence, so 0.01 is clear of the floor at these row counts.

The stock control ran 10.5 hours against the tuned arm’s 11.5, and the window above is the overlap, a slice inside the span both runs cover. A longer stock run might close some of the heap gap, though its band is wider throughout.

A compressed retention window hides all of this

At a 360-second retention window this same A/B returns no difference at any rate. Both arms record exactly 20 autovacuums in 1,200 seconds, one per 60 seconds, because that is autovacuum_naptime and neither arm can beat it. The tuned arm asks for far more and gets one. Compressing the clock to finish a bloat experiment sooner removes this variable from it.

7. Is a bounded heap a bounded table?

Not on this run. The heap settled and the indexes did not.

Every "bounded" verdict above is about heap bytes. The indexes on the same table, over the same runs, measured with pg_indexes_size:

RateIndex size at endSlope, post-fillHeap at end
1,000 jobs/s293.0 MB+3.2 MB/h1,109.8 MB
2,000 jobs/s585.2 MB+6.1 MB/h2,225.2 MB
4,000 jobs/s1,174.5 MB+13.1 MB/h4,489.2 MB
8,000 jobs/s2,387.2 MB+26.4 MB/h9,254.8 MB

At 8,000 jobs/s the indexes are 2.4 GB against a 9.25 GB heap, and they were still climbing when the run ended. A slope does not fit the heap because the heap truncates. The indexes do not truncate, so a slope fits them, and it is positive at every rate after eleven hours.

Where index size settles, or whether it does within a practical duration, is not known. It is the open number from this work. The size of this table at 8,000 jobs/s is at least 11.6 GB (heap plus indexes) and still moving.

The sampling consequence: pg_indexes_size on its own line, on the same interval as pg_relation_size. A single "table size" number that sums them hides which half is moving, and a heap-only number omits the indexes.

8. So what should you measure instead?

  1. Sample pg_relation_size and pg_indexes_size separately, on the leaf partitions, on a fixed interval. Thirty seconds sufficed for a one-hour window.
  2. Run for many retention cycles. At 8,000 jobs/s the last heap extension here landed at 10.77 cycles, so budget accordingly.
  3. Discard the fill phase explicitly. Retention has not fired yet, so nothing before the first deletion counts toward the measured window.
  4. For the heap, report the band (trough to peak) over the remaining window and where the run ended relative to its all-time peak. Do not report a slope.
  5. For the indexes, report the slope. They do not truncate, so a trend line means what it says.
  6. Read n_dead_tup as a median over the measurement window, never off the last sample. A sample landing just before an autovacuum catches the top of the sawtooth: on a separate arm of this rig (a per-queue partition comparison running a compressed 360-second retention window), one final sample read 27.0% dead against that arm’s own 13.2% median, a 2.0x overstatement from one badly timed read.

Decision rule: call the heap bounded when the run ends at or below its all-time peak and the band over the last several cycles is stable. A series still setting new peaks at the end of the window is growing, whatever its band width. Band width alone does not decide it; 3.30% and 0.84% are both bounded here.

What this does not show

The table is not shown to be bounded, only the heap. See section 7. Index growth was positive at every rate at the end of the run and there is no asymptote for it.

8,000 jobs/s is the top of the rig. Producer, consumer and Postgres share 4 vCPU, so above that rate this becomes a driver measurement and no longer a retention one. Where the retention window itself stops holding is not known, and the per-rate heap cost is already mildly superlinear across the range we could measure.

The handler is a no-op. Real work holds a transaction while it runs; ours completes instantly. That maximises churn per second, so this run contains no long-running transactions. That is a separate mechanism and the subject of the companion post on the xmin horizon.

Dead-tuple counts are estimates from the statistics collector and not from pgstattuple. Heap and index sizes are exact.

This is not a comparison against other queues. There is a public multi-system Postgres queue benchmark with a different contract, different hardware, different job shape and chaos injection that this run does not use. The question here is where the AGLedger retention setting stops holding on this hardware. There is no pg-boss-defaults arm, because at a drained rate the default seven-day window is arithmetic and needs no measurement. The companion post covers what happens when the rate is not drained, which is where that arithmetic stops applying.

One queue, one workload, no replicates. No OLAP alongside, no other tenants, no chaos injection, and one instance per rate point.

Sources & further reading

Related