diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 2e768dd5e4..bdce4164d6 100644 *** a/src/backend/commands/tablecmds.c --- b/src/backend/commands/tablecmds.c *************** *** 1404,1419 **** ExecuteTruncate(TruncateStmt *stmt) } /* ! * Check foreign key references. In CASCADE mode, this should be ! * unnecessary since we just pulled in all the references; but as a ! * cross-check, do it anyway if in an Assert-enabled build. */ #ifdef USE_ASSERT_CHECKING - heap_truncate_check_FKs(rels, false); - #else - if (stmt->behavior == DROP_RESTRICT) heap_truncate_check_FKs(rels, false); #endif /* * If we are asked to restart sequences, find all the sequences, lock them --- 1404,1427 ---- } /* ! * Suppress foreign key references check if session replication role is ! * set to REPLICA. */ + if (SessionReplicationRole != SESSION_REPLICATION_ROLE_REPLICA) + { + + /* + * Check foreign key references. In CASCADE mode, this should be + * unnecessary since we just pulled in all the references; but as a + * cross-check, do it anyway if in an Assert-enabled build. + */ #ifdef USE_ASSERT_CHECKING heap_truncate_check_FKs(rels, false); + #else + if (stmt->behavior == DROP_RESTRICT) + heap_truncate_check_FKs(rels, false); #endif + } /* * If we are asked to restart sequences, find all the sequences, lock them diff --git a/src/test/regress/expected/index d967e8dd21..86748430c5 100644 *** a/src/test/regress/expected/truncate.out --- b/src/test/regress/expected/truncate.out *************** *** 68,73 **** HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE. --- 68,77 ---- TRUNCATE TABLE truncate_a CASCADE; -- ok NOTICE: truncate cascades to table "trunc_b" NOTICE: truncate cascades to table "trunc_e" + -- Ignore foreign-key checks with session_replication_role = replica + SET session_replication_role = replica; + TRUNCATE TABLE truncate_a; -- ok + RESET session_replication_role; -- circular references ALTER TABLE truncate_a ADD FOREIGN KEY (col1) REFERENCES trunc_c; -- Add some data to verify that truncating actually works ... diff --git a/src/test/regress/sql/truncate.sqindex fbd1d1a8a5..0d0a3705d2 100644 *** a/src/test/regress/sql/truncate.sql --- b/src/test/regress/sql/truncate.sql *************** *** 33,38 **** TRUNCATE TABLE trunc_c,trunc_d,trunc_e,truncate_a,trunc_b; -- ok --- 33,43 ---- TRUNCATE TABLE truncate_a RESTRICT; -- fail TRUNCATE TABLE truncate_a CASCADE; -- ok + -- Ignore foreign-key checks with session_replication_role = replica + SET session_replication_role = replica; + TRUNCATE TABLE truncate_a; -- ok + RESET session_replication_role; + -- circular references ALTER TABLE truncate_a ADD FOREIGN KEY (col1) REFERENCES trunc_c;