Skip to content

Commit

Permalink
Fix rollback operations when a foreing key references a partitionned …
Browse files Browse the repository at this point in the history
…table. With PG12+, it is possible to have a global FK on a partitionned table rather than multiple FK, each on a single partition. If all involved elementary partitions and tables belong to the groups to rollback, allow to set the session_replication_mode to replica. Note that this does not solve the E-Maj rollback abort encountered when at least one partition does not belong to the groups to rollback and the FK constraint is not deferrable. In such a case, the FK should only be defined at elementary partitions level.
  • Loading branch information
beaud76 committed Mar 6, 2022
1 parent 74bf90a commit 3772122
Show file tree
Hide file tree
Showing 72 changed files with 5,432 additions and 3,847 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ E-Maj - Change log
* Fix warnings reported by emaj_verify_all() or at set mark time when
foreign keys are created on parent tables whose partitions belong to
tables groups.
* Fix rollback operations when a foreing key references a partitionned table
(instead of separate foreign keys referencing each elementary partitions).

4.0.0 (2021-May-29)
------
Expand Down
532 changes: 532 additions & 0 deletions sql/emaj--4.0.0--devel.sql

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions sql/emaj--devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8273,6 +8273,8 @@ $_rlbk_planning$
WHERE rlbp_rlbk_id = p_rlbkId -- The RLBK_TABLE steps for this rollback operation
AND rlbp_step = 'RLBK_TABLE'
AND contype = 'f' -- FK constraints
AND tf.relkind = 'r' -- only constraints referencing true tables, ie. excluding
-- partitionned tables
AND t.relname = rlbp_table
AND n.nspname = rlbp_schema
UNION
Expand All @@ -8290,6 +8292,8 @@ $_rlbk_planning$
WHERE rlbp_rlbk_id = p_rlbkId -- The RLBK_TABLE steps for this rollback operation
AND rlbp_step = 'RLBK_TABLE'
AND contype = 'f' -- FK constraints
AND t.relkind = 'r' -- only constraints referenced by true tables, ie. excluding
-- partitionned tables
AND tf.relname = rlbp_table
AND nf.nspname = rlbp_schema
), fkeys_agg AS (
Expand Down
4 changes: 4 additions & 0 deletions sql/emaj-devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8280,6 +8280,8 @@ $_rlbk_planning$
WHERE rlbp_rlbk_id = p_rlbkId -- The RLBK_TABLE steps for this rollback operation
AND rlbp_step = 'RLBK_TABLE'
AND contype = 'f' -- FK constraints
AND tf.relkind = 'r' -- only constraints referencing true tables, ie. excluding
-- partitionned tables
AND t.relname = rlbp_table
AND n.nspname = rlbp_schema
UNION
Expand All @@ -8297,6 +8299,8 @@ $_rlbk_planning$
WHERE rlbp_rlbk_id = p_rlbkId -- The RLBK_TABLE steps for this rollback operation
AND rlbp_step = 'RLBK_TABLE'
AND contype = 'f' -- FK constraints
AND t.relkind = 'r' -- only constraints referenced by true tables, ie. excluding
-- partitionned tables
AND tf.relname = rlbp_table
AND nf.nspname = rlbp_schema
), fkeys_agg AS (
Expand Down
6 changes: 3 additions & 3 deletions test/10/expected/adm1.out
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ select count(*) from emaj.emaj_rlbk_session;
select count(*) from emaj.emaj_rlbk_plan;
count
-------
133
138
(1 row)

select count(*) from emaj.emaj_rlbk_stat;
Expand All @@ -246,7 +246,7 @@ insert into myschema2.myTbl6 (col61) values (0);
insert into myschema2.myTbl8 (col81) values (0);
alter sequence mySchema2.mySeq1 restart 1000;
truncate mySchema4.myTblM, mySchema4.myTblC1, mySchema4.myTblC2;
truncate mySchema4.myTblP, mySchema4.myPartP1, mySchema4.myPartP2;
truncate mySchema4.myTblP, mySchema4.myPartP1, mySchema4.myPartP2 CASCADE;
-- analyze to get some statistics
analyze;
set role emaj_regression_tests_adm_user2;
Expand All @@ -264,7 +264,7 @@ select hist_function, hist_event, hist_wording
hist_function | hist_event | hist_wording
-----------------+------------+--------------------------------------------------------------------------------------------------------------------------
PURGE_HISTORIES | BEGIN | Retention delay @ 0
PURGE_HISTORIES | | 20 emaj_hist rows deleted ; 133 relation history rows deleted ; 91 relation changes deleted ; 37 rollback events deleted
PURGE_HISTORIES | | 20 emaj_hist rows deleted ; 134 relation history rows deleted ; 92 relation changes deleted ; 37 rollback events deleted
PURGE_HISTORIES | END |
(3 rows)

Expand Down
311 changes: 173 additions & 138 deletions test/10/expected/adm3.out

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions test/10/expected/alter.out
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ select emaj.emaj_drop_group('phil''s group#3",');
select emaj.emaj_force_stop_group('myGroup4');
emaj_force_stop_group
-----------------------
5
6
(1 row)

select emaj.emaj_drop_group('myGroup4');
emaj_drop_group
-----------------
5
6
(1 row)

select emaj.emaj_force_stop_group('emptyGroup');
Expand Down Expand Up @@ -346,6 +346,7 @@ select * from emaj.emaj_relation_change where rlchg_time_id >= 8000 order by 1,2
8005 | myschema4 | mytblc1 | REMOVE_TABLE | myGroup4 | | | | | | | | | | | |
8005 | myschema4 | mytblc2 | REMOVE_TABLE | myGroup4 | | | | | | | | | | | |
8005 | myschema4 | mytblm | REMOVE_TABLE | myGroup4 | | | | | | | | | | | |
8005 | myschema4 | mytblr | REMOVE_TABLE | myGroup4 | | | | | | | | | | | |
8008 | myschema1 | myTbl3 | ADD_TABLE | myGroup1 | | | | | | | | | | | |
8008 | myschema1 | myTbl3_col31_seq | ADD_SEQUENCE | myGroup1 | | | | | | | | | | | |
8008 | myschema1 | mytbl1 | ADD_TABLE | myGroup1 | | | | | | | | | | | |
Expand Down Expand Up @@ -384,7 +385,7 @@ select * from emaj.emaj_relation_change where rlchg_time_id >= 8000 order by 1,2
8016 | myschema1 | mytbl4 | ADD_TABLE | myGroup1 | | | | | | | | | | | |
8016 | myschema2 | myTbl3_col31_seq | ADD_SEQUENCE | myGroup2 | | | | | | | | | | | |
8016 | myschema2 | myseq1 | ADD_SEQUENCE | myGroup2 | | | | | | | | | | | |
(64 rows)
(65 rows)

select time_id, time_last_emaj_gid, time_event from emaj.emaj_time_stamp where time_id >= 8000 order by time_id;
time_id | time_last_emaj_gid | time_event
Expand Down Expand Up @@ -442,11 +443,11 @@ select hist_function, hist_event, hist_object,
DROP_GROUP | END | phil's group#3", | 4 tables/sequences processed | postgres
FORCE_STOP_GROUP | BEGIN | myGroup4 | | postgres
LOCK_GROUP | BEGIN | myGroup4 | | postgres
LOCK_GROUP | END | myGroup4 | 5 tables locked, 0 deadlock(s) | postgres
FORCE_STOP_GROUP | END | myGroup4 | 5 tables/sequences processed | postgres
LOCK_GROUP | END | myGroup4 | 6 tables locked, 0 deadlock(s) | postgres
FORCE_STOP_GROUP | END | myGroup4 | 6 tables/sequences processed | postgres
DROP_GROUP | BEGIN | myGroup4 | | postgres
DROP_GROUP | LOG_SCHEMA DROPPED | emaj_myschema4 | | postgres
DROP_GROUP | END | myGroup4 | 5 tables/sequences processed | postgres
DROP_GROUP | END | myGroup4 | 6 tables/sequences processed | postgres
FORCE_STOP_GROUP | BEGIN | emptyGroup | | postgres
LOCK_GROUP | BEGIN | emptyGroup | | postgres
LOCK_GROUP | END | emptyGroup | 0 tables locked, 0 deadlock(s) | postgres
Expand Down
3 changes: 2 additions & 1 deletion test/10/expected/alter_logging.out
Original file line number Diff line number Diff line change
Expand Up @@ -3565,12 +3565,13 @@ select * from emaj.emaj_rel_hist order by 1,2,3;
myschema4 | mytblm | [10027,10029) | myGroup1 | r
myschema4 | mytblm | [10029,10031) | myGroup2 | r
myschema4 | mytblm | [10031,10045) | myGroup4 | r
myschema4 | mytblr | [4046,8005) | myGroup4 | r
phil's schema3 | myTbl2\ | [1019,8004) | phil's group#3", | r
phil's schema3 | myTbl2\_col21_seq | [1028,8004) | phil's group#3", | S
phil's schema3 | phil's seq\1 | [1028,8004) | phil's group#3", | S
phil's schema3 | phil's tbl1 | [1019,8004) | phil's group#3", | r
public | mytbl1_new_name | [10038,10039) | myGroup1 | r
(118 rows)
(119 rows)

truncate emaj.emaj_hist;
-- remove the temp directory
Expand Down
56 changes: 28 additions & 28 deletions test/10/expected/check.out
Original file line number Diff line number Diff line change
Expand Up @@ -88,60 +88,60 @@ select funcname, calls from pg_stat_user_functions
order by funcname, funcid;
funcname | calls
-------------------------------------------+-------
_add_seq | 45
_add_tbl | 155
_add_seq | 46
_add_tbl | 159
_assign_sequences | 26
_assign_tables | 64
_build_alter_seq | 219
_build_sql_tbl | 157
_assign_tables | 65
_build_alter_seq | 223
_build_sql_tbl | 161
_change_ignored_triggers_tbl | 13
_change_log_data_tsp_tbl | 15
_change_log_index_tsp_tbl | 9
_change_priority_tbl | 16
_check_fk_groups | 90
_check_group_names | 1065
_check_group_names | 1066
_check_json_groups_conf | 43
_check_json_param_conf | 15
_check_json_table_properties | 46
_check_mark_name | 375
_check_marks_range | 122
_check_new_mark | 454
_check_new_mark | 455
_cleanup_rollback_state | 369
_copy_from_file | 27
_copy_to_file | 257
_create_log_schema | 35
_create_log_schemas | 21
_create_seq | 45
_create_tbl | 157
_create_seq | 46
_create_tbl | 161
_dblink_close_cnx | 71
_dblink_open_cnx | 88
_dblink_sql_exec | 1947
_dblink_sql_exec | 1957
_delete_before_mark_group | 23
_delete_between_marks_group | 14
_delete_intermediate_mark_group | 23
_delete_log_tbl | 60
_delete_log_tbl | 61
_detailed_log_stat_groups | 33
_disable_event_triggers | 312
_disable_event_triggers | 313
_drop_group | 32
_drop_log_schemas | 130
_drop_seq | 66
_drop_tbl | 204
_drop_seq | 69
_drop_tbl | 210
_emaj_param_change_fnct | 42
_enable_event_triggers | 306
_estimate_rlbk_step_duration | 929
_enable_event_triggers | 307
_estimate_rlbk_step_duration | 932
_estimate_rollback_groups | 13
_event_trigger_sql_drop_fnct | 166
_event_trigger_table_rewrite_fnct | 4
_export_groups_conf | 11
_export_param_conf | 7
_gen_sql_groups | 28
_gen_sql_seq | 41
_gen_sql_seq | 42
_gen_sql_tbl | 55
_get_current_sequence_state | 763
_get_current_sequence_state | 783
_get_default_tablespace | 11
_get_log_sequence_last_value | 2228
_get_log_sequence_last_value | 2273
_get_previous_mark_group | 37
_handle_trigger_fk_tbl | 2743
_handle_trigger_fk_tbl | 2801
_import_groups_conf | 20
_import_groups_conf_alter | 20
_import_groups_conf_check | 26
Expand All @@ -151,10 +151,10 @@ select funcname, calls from pg_stat_user_functions
_lock_groups | 307
_log_stat_groups | 35
_modify_tables | 36
_move_seq | 21
_move_seq | 23
_move_sequences | 17
_move_tables | 20
_move_tbl | 42
_move_tbl | 44
_purge_histories | 80
_remove_seq | 15
_remove_sequences | 19
Expand All @@ -168,15 +168,15 @@ select funcname, calls from pg_stat_user_functions
_rlbk_groups | 75
_rlbk_init | 80
_rlbk_planning | 93
_rlbk_seq | 178
_rlbk_seq | 181
_rlbk_session_exec | 84
_rlbk_session_lock | 84
_rlbk_set_batch_number | 163
_rlbk_set_batch_number | 165
_rlbk_start_mark | 80
_rlbk_tbl | 137
_rlbk_tbl | 139
_rollback_activity | 17
_set_mark_groups | 354
_set_time_stamp | 565
_set_time_stamp | 566
_start_groups | 53
_stop_groups | 67
_truncate_trigger_fnct | 16
Expand All @@ -185,7 +185,7 @@ select funcname, calls from pg_stat_user_functions
emaj_assign_sequence | 6
emaj_assign_sequences | 7
emaj_assign_sequences | 13
emaj_assign_table | 31
emaj_assign_table | 32
emaj_assign_tables | 7
emaj_assign_tables | 26
emaj_cleanup_rollback_state | 9
Expand Down Expand Up @@ -262,7 +262,7 @@ select funcname, calls from pg_stat_user_functions
select sum(calls) from pg_stat_user_functions where funcname like E'emaj\\_%';
sum
-----
994
995
(1 row)

-----------------------------
Expand Down

0 comments on commit 3772122

Please sign in to comment.