ETLMCPData Conveyance Works
Log in Start free
Guides

Why the etlmcp PostgreSQL table keeps growing: rewritten days, dead rows and vacuum

Why an etlmcp PostgreSQL table takes more disk than its row count suggests, how autovacuum handles the dead rows, and when VACUUM FULL is worth its lock.

Your etlmcp table in PostgreSQL grows faster than its row count because every scheduled run deletes and re-inserts a week or more of rows, and PostgreSQL keeps each deleted row version on disk until vacuum clears it. Autovacuum, which is on by default, makes that space reusable, so the file size levels off after a while instead of growing without end. It does not usually shrink the file. Only VACUUM FULL does that, and it locks the table while it works.

What each run writes

When a run loads into PostgreSQL, etlmcp deletes the rows that belong to that pipeline within the run's date range (widened to cover any date in the fresh rows) and then inserts the fresh rows for the same days. It never touches rows from other pipelines. A scheduled run does not cover one day. It covers a trailing window: the server's setting, 7 days by default, or longer for platforms that keep changing past figures. A Meta pipeline re-pulls 29 days and a Shopify pipeline 58 days. So a table that gains one new day of data per run also turns a week or more of old rows into dead rows on that same run.

Two cases rewrite even more. If the rows have no date column, each run replaces all of the pipeline's rows. If a run returns no rows and the table already has a date column, etlmcp still deletes the pipeline's rows in that date range. How the replacement works in detail is on row replacement.

Why deleted rows still take space

PostgreSQL keeps old row versions after a DELETE, because other open transactions may still need to see them. They stay as dead rows until a VACUUM removes them. A standard VACUUM removes dead rows from the table and its indexes and marks the space free for new rows. It runs alongside normal reads and writes, so your dashboards and etlmcp's loads carry on. It gives space back to the operating system only in a narrow case, when whole pages at the end of the table are empty. That is why the table's size settles at a plateau: the next run's inserts reuse the space the last run's deletes freed.

When autovacuum steps in

By default autovacuum vacuums a table once its dead rows pass 50 plus 20% of the table's rows (autovacuum_vacuum_threshold and autovacuum_vacuum_scale_factor). Recent versions also cap that figure with autovacuum_vacuum_max_threshold, 100 million rows by default. On a large table, 20% can be a lot of dead rows, so you may want vacuum to run sooner on the etlmcp table only. You can set this per table:

ALTER TABLE marketing.ads_daily SET (
  autovacuum_vacuum_scale_factor = 0.05,
  autovacuum_vacuum_threshold = 1000
);

Lower values mean more frequent, smaller vacuums.

Checking the table

To see dead rows and when vacuum last ran:

SELECT relname, n_live_tup, n_dead_tup, last_vacuum, last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'ads_daily';

Both row counts are estimates. last_vacuum records manual runs and last_autovacuum the daemon's. A high n_dead_tup with an old or empty last_autovacuum means autovacuum is not keeping up.

To see the size on disk, use SELECT pg_size_pretty(pg_total_relation_size('marketing.ads_daily'));. That total includes the table's indexes and TOAST data. pg_table_size leaves the indexes out.

VACUUM FULL, only if you must shrink

VACUUM FULL writes a new copy of the table without the dead space and returns the freed space to the operating system. It has three costs. It takes an ACCESS EXCLUSIVE lock, so dashboards and etlmcp loads on that table wait until it finishes. It needs extra free disk, roughly the size of the table, until the new copy is done, which you may not have if the disk is already close to full. And it is much slower than a plain vacuum. Autovacuum never runs it. If you do run it, pick a time between your pipelines' scheduled runs; load locks covers how a waiting load behaves.

Related

Sources

  1. Routine Vacuuming — www.postgresql.org, checked
  2. Vacuuming (server configuration) — www.postgresql.org, checked
  3. The Cumulative Statistics System — www.postgresql.org, checked
  4. System Administration Functions — www.postgresql.org, checked