fix(rds): terminate target sessions during PostgreSQL restore (#6550)

This commit is contained in:
jwoa95 authored and GitHub committed 2026-10-25 17:29:09 -04:00
1 parent a79cc4d8c4
commit 5f63ffb761
3 files changed
+12 -2

No files matched your search

+8
View File
@@ -783,6 +783,14 @@ A failed backend password rotation retains the managed secret and stored credent
If Secrets Manager support is unavailable, opt-out fails before changing cluster state.
This does not provide a transaction across database backends and persistent storage.
## PostgreSQL restore connections
Snapshot and point in time restores drop the target's non-template databases with
`DROP DATABASE ... WITH (FORCE)` before replaying the dump. This terminates connections
on the restore target, including preloaded workers such as pg_cron; the source is unchanged.
Custom PostgreSQL images must support this syntax (PostgreSQL 13 or later). Prepared
transactions and active logical replication slots or subscriptions can still prevent a drop.
## Point in time restore
`RestoreDBInstanceToPointInTime` and `RestoreDBClusterToPointInTime` check the request as AWS does:
@@ -1167,12 +1167,12 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
set -e
USER="$1"
# Drop all non-template, non-postgres databases
# Terminate target connections (including pg_cron workers) when dropping databases
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -tAc \\
"SELECT datname FROM pg_database WHERE datistemplate = false AND datname <> 'postgres'" \\
| while read -r db; do
[ -z "$db" ] && continue
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -c "DROP DATABASE IF EXISTS \\"$db\\""
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -c "DROP DATABASE IF EXISTS \\"$db\\" WITH (FORCE)"
done
# Exclude the master user CREATE ROLE statement to avoid conflicts during restore
@@ -1439,6 +1439,8 @@ class RdsContainerManagerTest {
void restoreScriptDropsNonSystemDatabasesAndRoles() {
String script = RdsContainerManager.postgresRestoreScript();
assertTrue(script.contains("WITH (FORCE)"),
"Restore must terminate sessions such as pg_cron connected to the target database");
assertTrue(script.contains("datistemplate = false"),
"Script must only drop non-template databases");
assertTrue(script.contains("datname <> 'postgres'"),