Il 22/01/18 19:41, Petr Jelinek ha scritto: > On 19/01/18 12:41, Marco Nenciarini wrote: >> Hi Peter, >> >> Il 18/01/18 17:30, Peter Eisentraut ha scritto: >>> On 1/17/18 11:33, Petr Jelinek wrote: >>>>> P.S: I'm struggling to understand why we have two possible values of >>>>> session_replication_role settings that behave identically (origin and >>>>> local). I'm unable to see any difference according to the code or the >>>>> documentation, so I'm wondering if we should document that they are the >>>>> same. >>>>> >>>> It's for use by 3rd party tools (afaik both londiste and slony use it) >>>> to differentiate between replicated and not replicated changes. >>> >>> I have committed some documentation updates and tests to cover this a >>> bit better. >>> >> >> Thanks, the documentation is a lot clearer now. >> >> This superseded the documentation change that was in the patch, so I've >> removed it from the v3 version. >> > > Now that we have tests for this, I think it would be good idea to expand > them to cover the new behavior of TRUNCATE in this patch. >
You are right. Attached the new v4 version. Regards, Marco -- Marco Nenciarini - 2ndQuadrant Italy PostgreSQL Training, Services and Support [email protected] | www.2ndQuadrant.it
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;
signature.asc
Description: OpenPGP digital signature
