Skip to contentManaged Kafka is live: Apache Kafka on AWS for half the cost of MSK

Database administration

Prevent WAL growth on RDS Postgres

Learn how to prevent Write-Ahead Log (WAL) growth issues in your AWS RDS Postgres database, especially for low-traffic or test environments.

Why idle RDS databases accumulate WAL

PostgreSQL writes every change to the write-ahead log (WAL). Artie reads the WAL through a replication slot for change data capture (CDC), and PostgreSQL keeps WAL until the slot advances past it.

Amazon RDS generates WAL even when your tables are idle. Gunnar Morling explains the cause in The Insatiable Postgres Replication Slot:

  • RDS writes a heartbeat to its internal rdsadmin database every 5 minutes.
  • RDS sets the WAL segment size to 64 MB, up from the PostgreSQL default of 16 MB.
  • With archive_timeout at 5 minutes, each heartbeat lands in a new 64 MB segment.
  • An idle database gains 64 MB of WAL every 5 minutes, about 18.4 GB per day.

While your replicated tables are idle, the slot can’t advance, so PostgreSQL retains every segment. Unchecked WAL growth overflows the replication slot, exhausts database storage, and can take the database down.

Test databases, low-traffic environments and idle databases are most at risk. Active databases are unaffected, because regular changes to replicated tables keep the slot advancing.

Artie also recommends heartbeats for low-traffic databases on Google Cloud SQL and PlanetScale for Postgres. For PlanetScale, follow Create the heartbeat table.

Prevent WAL growth with heartbeats

Heartbeats suit low-traffic or idle databases. Active databases don’t need them. Heartbeats are off by default.

With heartbeats on, Artie runs this query against your database every 5 minutes:

SQL
UPDATE test_heartbeat_table set ts = now() where id = '1';

Each update is a change the replication slot can advance past. The query rewrites the same row, so the table never grows.

From the blog post Prevent WAL Growth on AWS Postgres RDS.

Set up

  1. Create the heartbeat table

    Run this SQL once in the database you’re replicating:

    SQL
    -- Create the heartbeat table
    CREATE TABLE test_heartbeat_table (id text PRIMARY KEY, ts timestamp);
    
    -- Grant necessary permissions
    GRANT SELECT, UPDATE ON TABLE test_heartbeat_table TO artie;
    
    -- Insert initial record
    INSERT INTO test_heartbeat_table (id, ts) VALUES (1, NOW());

    Replace artie with the database user Artie connects as.

  2. Add the table to a filtered publication

    Skip this step if your publication is FOR ALL TABLES, which is what Artie creates when Publication mode is All tables. If the publication lists specific tables, run:

    SQL
    ALTER PUBLICATION dbz_publication ADD TABLE test_heartbeat_table;

    dbz_publication is the default publication name. Use your own name if you set a publication override.

  3. Turn on heartbeats in Artie

    In the pipeline editor, open the Source tab and expand Advanced settings. Under Postgres settings, select Enable heartbeats.

    A dialog titled Finish setting up heartbeats shows the same SQL. Select Close.

  4. Save and deploy

    Save your changes, then deploy the pipeline. Saved changes go live after a deploy.

In a new pipeline, turn on Heartbeats under Advanced options on the Connect and verify step instead. The Prepare source step also shows an Enable heartbeats task with the same SQL. The task appears when Hosting provider is Amazon Aurora / RDS, Google Cloud SQL or PlanetScale.

To manage the setting as code, set enable_heartbeats = true on the Terraform artie_source_reader resource, or enableHeartbeats in the source reader’s API settings.

Verify heartbeats

Wait at least 5 minutes after the deploy. Then run the slot size query in Diagnostic queries and check that retained_wal stops growing. This confirms the slot is advancing.

To check the heartbeat row itself, read its timestamp:

SQL
SELECT ts FROM test_heartbeat_table WHERE id = '1';

ts changes every 5 minutes. Artie’s own heartbeat check also updates it, so a changing ts alone doesn’t prove the pipeline is sending heartbeats.

Troubleshooting

Artie checks the heartbeat table of every running pipeline that has heartbeats on, including pipelines with only Transfer paused. It alerts you when the check fails.

Artie cannot write to the heartbeat table

Artie shows a warning on the pipeline’s overview page and sends an email titled [ALERT] PostgreSQL issues detected. The email starts with this line and quotes the error after it:

Text
We are having issues writing to the heartbeats table for this pipeline

Cause: Artie’s check of test_heartbeat_table failed, so heartbeats may not be advancing the replication slot. The check fails when Artie can’t update the row (error while updating heartbeat table), or when the table isn’t in the publication (heartbeat table "test_heartbeat_table" is not part of publication(s)). The quoted error names the reason.

Fix: Run these three checks in the database you’re replicating. First, confirm that test_heartbeat_table has the row with id = '1':

SQL
SELECT * FROM test_heartbeat_table WHERE id = '1';

The query returns one row. If it returns no rows, Artie’s update matches nothing and no heartbeat is written. Add the row:

SQL
INSERT INTO test_heartbeat_table (id, ts) VALUES (1, NOW());

Second, confirm the table is in your publication:

SQL
-- Check if the table is included in your publication
-- For Artie pipelines, the publication name should default
-- to `dbz_publication` (unless you set a publication override)
SELECT pubname, tablename
FROM pg_publication_tables
WHERE tablename = 'test_heartbeat_table';

The query returns one row with your publication’s name. If it returns no rows, add the table with ALTER PUBLICATION, as in Set up.

Third, confirm the service account has write permission by granting it again:

SQL
GRANT SELECT, UPDATE ON TABLE test_heartbeat_table TO artie;

Replace artie with the database user Artie connects as.

Verify: Artie sends [RESOLVED] PostgreSQL issues resolved, with the line “We are now able to write to the heartbeats table for this pipeline”, once it can write to the heartbeat table.

Replication slot keeps growing with heartbeats on

Cause: A long-running query can block replication, which stops the slot from advancing even while heartbeats run.

Fix: First run the three checks under Artie cannot write to the heartbeat table. Then list queries that have run for more than 1 minute:

SQL
-- Check for queries that might block replication
SELECT
  pid,
  now() - pg_stat_activity.query_start AS duration,
  query,
  state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '1 minute';

For other causes, such as a paused pipeline, see Replication slot keeps growing.

Verify: Run the slot size query in Diagnostic queries and confirm retained_wal has stopped growing.

More safeguards

These measures limit the damage if WAL grows despite heartbeats.

Monitoring

Artie and RDS both report the numbers to watch:

  • Set an alert on the RDS FreeStorageSpace metric.
  • Artie alerts you when a slot passes 5% of your database size, with a 10 GB floor and a 100 GB ceiling.
  • If max_slot_wal_keep_size is set, Artie alerts at 65% of it when that is lower.
  • To alert at your own threshold, create a Large replication slot monitor in custom monitors.

Database configuration

Review these two settings on the database:

  • Configure max_slot_wal_keep_size. The default is -1, which means no limit.
  • Enable storage autoscaling by following the AWS storage autoscaling guide.

When a slot exceeds max_slot_wal_keep_size, PostgreSQL invalidates it and the tables need a backfill. Choose a value with Set max_slot_wal_keep_size.

Diagnostic queries

Run these queries in the source database. List every replication slot:

SQL
-- View all replication slots
SELECT * FROM pg_replication_slots;

Check how much WAL each slot retains:

SQL
-- Check replication slot size
SELECT
  slot_name,
  wal_status,
  pg_size_pretty(
    pg_wal_lsn_diff(
      pg_current_wal_lsn(), restart_lsn)) AS retained_wal,
  active,
  restart_lsn 
FROM pg_replication_slots;

retained_wal is the WAL PostgreSQL holds for the slot. A wal_status of lost means PostgreSQL has invalidated the slot.

Remove a replication slot by name:

SQL
-- Remove a replication slot
SELECT pg_drop_replication_slot('REPLICATION_SLOT_NAME');

Replace REPLICATION_SLOT_NAME with the slot’s name. Artie’s slot is named artie unless you set a replication slot override.

Was this page helpful?