Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

jwoa955f63ffb76187
diff --git a/docs/services/rds.md b/docs/services/rds.md
index 62a751ee6e..149e335058 100644
--- a/docs/services/rds.md
+++ b/docs/services/rds.md
@@ -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:
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java
index 9043000f2c..a0e3af6ec8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java
@@ -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
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java
index 3ad0b5ad20..6361560d12 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java
@@ -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'"),