Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

Plan a near-zero-downtime upgrade of a 3 TB PostgreSQL 14 database behind a Spring Boot fleet to PostgreSQL 17 using logical replication.

I would check that every table has a primary key or replica identity, set wal_level = logical on the 14 primary, and confirm the extensions exist on 17; then copy the schema with pg_dump --schema-only, create a publication and subscription, and let the initial copy run and catch up. At switchover I stop writes for a minute, wait for zero lag, copy sequence values with a safety margin because they are not replicated, and repoint the Spring Boot fleet, keeping the 14 cluster with reverse replication for rollback. Before any of that, the application is tested on PostgreSQL 17 with the JDBC driver version it will use.

Why this question is asked

For a 3 TB database, pg_upgrade would mean minutes of downtime and no easy way back, and a dump and restore would take most of a day. Logical replication lets the new version run next to the old one for days, so the actual cutover is a short, rehearsed step with a rollback path. Interviewers ask for this plan because it has many small traps, each of which can stop the cutover or lose data. In this answer, we will go through the plan in order: prerequisites, building the new cluster, the initial copy, handling sequences and DDL, the switchover itself, rollback, and testing the application on 17.

1A Quick Recap: Why Logical Replication Works for Upgrades

Logical replication decodes the WAL of the old cluster into row changes, such as 'insert this row into orders', and applies them on a subscriber with ordinary SQL. Because row changes do not depend on the on-disk format, the subscriber can be a different major version. The new 17 cluster is a normal writable database that we can test, index and tune while it keeps receiving changes.

The other side of the same fact is that only row changes travel. Schema changes and sequence values are not sent, so the plan has to cover them by hand.

2Checking the Prerequisites

Updates and deletes are replicated by identifying the old row, normally through its primary key. A table without a primary key or a REPLICA IDENTITY setting cannot publish updates and deletes, and the publisher reports an error for them as soon as it is in a publication. So we list such tables first and fix them.

SQL
-- Tables without a primary key
SELECT c.oid::regclass FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r', 'p') AND n.nspname NOT IN ('pg_catalog', 'information_schema')
AND NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conrelid = c.oid AND contype = 'p');
-- audit_log
ALTER TABLE audit_log REPLICA IDENTITY FULL; -- or better, add a primary key
SHOW wal_level; -- must be logical; changing it needs a restart

REPLICA IDENTITY FULL works, but each update then sends the whole old row, and the subscriber may scan the table to find it, so a real primary key is better for large tables. We also check that every extension exists on 17 at a compatible version, and look for things logical replication does not carry at all, such as large objects.

3Building the New Cluster and the Initial Copy

We create the 17 cluster, copy the schema with pg_dump --schema-only from the old database, and restore it. Then we publish all tables on 14 and subscribe from 17. The subscription first copies the existing rows of each table, then streams the changes made since.

SQL
-- On PostgreSQL 14
CREATE PUBLICATION upgrade_pub FOR ALL TABLES;
-- On PostgreSQL 17, after restoring the schema
CREATE SUBSCRIPTION upgrade_sub
CONNECTION 'host=pg14 dbname=shop user=replicator'
PUBLICATION upgrade_pub;
SELECT srrelid::regclass, srsubstate FROM pg_subscription_rel;
-- orders | d (copying data)
-- users | r (ready: copied and streaming)
Step throughUpgrade plan from 14 to 17
  1. Fix tables without a primary keyor set REPLICA IDENTITY
  2. Schema to 17 with pg_dump --schema-onlyextensions checked
  3. Publication on 14, subscription on 17initial copy, then streaming
  4. Freeze DDL; test the app on 17JDBC driver, query plans
  5. Switchoverstop writes, lag zero, sequences, repoint
  6. Reverse replication 17 -> 14rollback path for a few days

6 steps in order. Play them, or step through them one at a time.

For 3 TB, the initial copy can take many hours, and the replication slot on 14 keeps WAL for the whole time, so we check disk space and watch retained WAL. max_sync_workers_per_subscription controls how many tables copy in parallel, and splitting the largest tables into separate subscriptions can spread the load. Creating secondary indexes on the subscriber after the copy, rather than before, also makes it faster.

4Sequences and DDL During the Copy

Sequences are not replicated in most versions. The 17 cluster's sequences still start at 1, while the rows it received have ids up to 48 million. If applications switched over like that, the first insert would fail with a duplicate key. So at switchover we read every sequence's last value on 14 and set it on 17 with a margin, such as 10,000, which costs nothing but a gap in ids.

SQL
-- On 14, generate the statements
SELECT format('SELECT setval(%L, %s);', schemaname || '.' || sequencename, coalesce(last_value, 0) + 10000)
FROM pg_sequences;
-- SELECT setval('public.orders_id_seq', 48231917);
-- Run the generated statements on 17

DDL is not replicated either. The safest rule is a schema freeze for the duration of the migration. If a change cannot wait, we apply it on the subscriber first and then on the publisher for additive changes, such as a new nullable column, so that rows with the new column always find it on 17. Dropping a column goes the other way round, publisher first.

5The Switchover

Switchover is a rehearsed script with a time box. First we stop writes to 14: scale the writers to zero, or set the database to read-only and terminate sessions. Then we wait until the subscriber has received everything, by comparing the publisher's current LSN with the slot's confirmed position. Then we sync the sequences, disable the subscription, and repoint the Spring Boot services, through a proxy or DNS name if we have one, otherwise through a configuration change and a rolling restart.

SQL
-- On 14, after writes stop
ALTER DATABASE shop SET default_transaction_read_only = on; -- applies to new sessions
SELECT pg_current_wal_lsn(), confirmed_flush_lsn
FROM pg_replication_slots WHERE slot_name = 'upgrade_sub';
-- 7A/1F0021C8 | 7A/1F0021C8 (caught up)
-- On 17
ALTER SUBSCRIPTION upgrade_sub DISABLE;

With a proxy in front, the write pause is usually under a minute. Before reopening traffic, we run a quick check, such as row counts and the latest id on the busiest tables, on both sides.

6Rollback and Testing the Application on 17

Rollback has to be planned before the switchover. Right after cutover, we create a publication on 17 and a subscription on 14 with copy_data = false, so the old cluster keeps receiving every new write. If a serious problem appears in the next days, we can switch back without losing data. After a few calm days, we drop the reverse replication and retire 14.

Most surprises are in the application, not the database, so we test the services on PostgreSQL 17 weeks before. That means running the full test suite against 17, checking that the PostgreSQL JDBC driver version supports 17, comparing the plans of the most important queries with EXPLAIN on both versions, and replaying a sample of real traffic if we can. A query that was fast on 14 and slow on 17 is much cheaper to find in a test than on the night.

7Follow-up questions

How do you handle DDL changes made during the copy?

The simplest answer is a schema freeze until switchover. If a change is unavoidable, we apply additive changes such as new columns or tables on the 17 subscriber first and then on 14, and for a new table we also run ALTER SUBSCRIPTION ... REFRESH PUBLICATION so it gets copied. Destructive changes, such as dropping a column, go on the publisher first. Every such change is recorded and replayed in the rehearsal.

Why not use pg_upgrade instead?

pg_upgrade with --link is fast, often minutes, but it needs downtime for the whole process, and rolling back means restoring the old cluster. Logical replication costs more preparation but keeps downtime to a short pause and leaves a working old cluster for rollback.

What happens to replication slots and standbys of the old cluster?

They are not carried over. The 17 cluster needs its own standbys, built from a base backup after the initial copy, and any CDC consumers need new slots on 17, usually with a fresh snapshot.

8Conclusion

In this answer, we planned a near-zero-downtime upgrade of a 3 TB PostgreSQL 14 database to 17 with logical replication. We checked primary keys, replica identity, wal_level and extensions, copied the schema, created a publication and subscription and watched the initial copy. We handled sequences by copying their values with a margin and DDL with a freeze. The switchover stopped writes, waited for zero lag, synced sequences and repointed the services, with reverse replication kept for rollback, and the application was tested on 17 beforehand, including the JDBC driver and query plans.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start with prerequisites on the 14 side: every table needs a primary key, or at least REPLICA IDENTITY, so updates and deletes can be replicated; wal_level must be logical, which needs a restart; and every extension must exist on 17.

Then, if they want more

Then I build the 17 cluster, restore the schema with pg_dump --schema-only, create a publication for all tables on 14 and a subscription on 17, and let the initial copy run. For 3 TB that takes hours, so I watch the slot's retained WAL and disk, and add secondary indexes after the copy if I can.

During the migration I freeze DDL; if a change is needed, additive changes go to 17 first. Sequences are not replicated, so at switchover I copy every sequence's last value plus a margin.

The switchover itself is rehearsed: stop writes to 14, wait until the slot's confirmed position matches the current WAL position, sync sequences, disable the subscription and repoint the Spring Boot services, ideally through a proxy, then check counts. For rollback I set up reverse replication from 17 to 14 with copy_data false for a few days.

And weeks before, I test the application on 17: the full test suite, the JDBC driver version, and plans of the important queries.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 17, PostgreSQL 14
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Plan a near-zero-downtime upgrade of a 3 TB PostgreSQL 14 database behind a Spring Boot...": https://scrivio-interview-library.netlify.app/databases/postgresql/ab7b40fdae/