Skip to content

Commit

Permalink
When importing a groups configuration, report the REPAIR_TBL action i…
Browse files Browse the repository at this point in the history
…nto the emaj_hist table. Move the code of the repair action into a new _repair_tbl() function.
  • Loading branch information
beaud76 committed Oct 4, 2020
1 parent 7487606 commit 80f4a49
Show file tree
Hide file tree
Showing 33 changed files with 282 additions and 169 deletions.
2 changes: 2 additions & 0 deletions docs/en/traces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ The *hist_event* column can take the following values:
+------------------------------+----------------------------------------------------------------+
| TABLE REMOVED | table removed from a logging tables group |
+------------------------------+----------------------------------------------------------------+
| TABLE REPAIRED | table repaired for E-Maj |
+------------------------------+----------------------------------------------------------------+
| TRIGGERS TO IGNORE CHANGED | set of application triggers to ignore at rollback time changed |
+------------------------------+----------------------------------------------------------------+
| UPDATED PARAMETER | parameter updated in *emaj_param* |
Expand Down
2 changes: 2 additions & 0 deletions docs/fr/traces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ La colonne *hist_event* peut prendre les valeurs suivantes.
+------------------------------+------------------------------------------------------------------------+
| TABLE REMOVED | table supprimée d’un groupe de tables actif |
+------------------------------+------------------------------------------------------------------------+
| TABLE REPAIRED | table réparée pour E-Maj |
+------------------------------+------------------------------------------------------------------------+
| TRIGGERS TO IGNORE CHANGED | ensemble des triggers applicatifs à ignorer lors des rollbacks modifié |
+------------------------------+------------------------------------------------------------------------+
| UPDATED PARAMETER | paramètre modifié dans *emaj_param* |
Expand Down
66 changes: 43 additions & 23 deletions sql/emaj--3.4.0--devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,46 @@ $_drop_tbl$
END;
$_drop_tbl$;

CREATE OR REPLACE FUNCTION emaj._repair_tbl(v_schema TEXT, v_table TEXT, v_group TEXT, v_groupIsLogging BOOLEAN,
v_timeId BIGINT, v_function TEXT)
RETURNS VOID LANGUAGE plpgsql AS
$_repair_tbl$
-- The function repairs a table detected as corrupted, i.e. with any trouble detected by the emaj_verify_all() and similar functions.
-- Inputs: the schema and table names to repair
-- the group that currently owns the table, and its state
-- the time_id of the operation
-- the calling function name
DECLARE
v_isRollbackable BOOLEAN;
BEGIN
IF v_groupIsLogging THEN
RAISE EXCEPTION '_repair_tbl: Cannot repair the table %.%. Its group % is in LOGGING state. Remove first the table from its group.',
v_schema, v_table, v_group;
ELSE
-- remove the table from its group
PERFORM emaj._drop_tbl(emaj.emaj_relation.*, v_timeId)
FROM emaj.emaj_relation
WHERE rel_schema = v_schema
AND rel_tblseq = v_table
AND upper_inf(rel_time_range);
-- get the is_rollbackable status of the related group
SELECT group_is_rollbackable INTO v_isRollbackable
FROM emaj.emaj_group
WHERE group_name = v_group;
-- and recreate it
PERFORM emaj._create_tbl(v_schema, v_table, v_group, tmp_priority, tmp_log_dat_tsp, tmp_log_idx_tsp,
tmp_ignored_triggers, v_timeId, v_isRollbackable, v_groupIsLogging)
FROM tmp_app_table
WHERE tmp_group = v_group
AND tmp_schema = v_schema
AND tmp_tbl_name = v_table;
INSERT INTO emaj.emaj_hist(hist_function, hist_event, hist_object, hist_wording)
VALUES (v_function, 'TABLE REPAIRED', quote_ident(v_schema) || '.' || quote_ident(v_table), 'In group ' || v_group);
END IF;
RETURN;
END;
$_repair_tbl$;

CREATE OR REPLACE FUNCTION emaj.emaj_assign_sequences(v_schema TEXT, v_sequencesIncludeFilter TEXT, v_sequencesExcludeFilter TEXT,
v_group TEXT, v_mark TEXT DEFAULT 'ASSIGN_%')
RETURNS INTEGER LANGUAGE plpgsql AS
Expand Down Expand Up @@ -4971,7 +5011,6 @@ $_alter_exec$
v_logDatTsp TEXT;
v_logIdxTsp TEXT;
v_ignoredTriggers TEXT[];
v_isRollbackable BOOLEAN;
r_plan emaj.emaj_alter_plan%ROWTYPE;
r_rel emaj.emaj_relation%ROWTYPE;
BEGIN
Expand All @@ -4994,28 +5033,9 @@ $_alter_exec$
v_timeId, v_callingFunction);
--
WHEN 'REPAIR_TBL' THEN
IF r_plan.altr_group_is_logging THEN
RAISE EXCEPTION 'alter_exec: Cannot repair the table %.%. Its group % is in LOGGING state.',
r_plan.altr_schema, r_plan.altr_tblseq, r_plan.altr_group;
ELSE
-- remove the table from its group
PERFORM emaj._drop_tbl(emaj.emaj_relation.*, v_timeId)
FROM emaj.emaj_relation
WHERE rel_schema = r_plan.altr_schema
AND rel_tblseq = r_plan.altr_tblseq
AND upper_inf(rel_time_range);
-- get the is_rollbackable status of the related group
SELECT group_is_rollbackable INTO v_isRollbackable
FROM emaj.emaj_group
WHERE group_name = r_plan.altr_group;
-- and recreate it
PERFORM emaj._create_tbl(tmp_schema, tmp_tbl_name, tmp_group, tmp_priority, tmp_log_dat_tsp, tmp_log_idx_tsp,
tmp_ignored_triggers, v_timeId, v_isRollbackable, r_plan.altr_group_is_logging)
FROM tmp_app_table
WHERE tmp_group = coalesce (r_plan.altr_new_group, r_plan.altr_group)
AND tmp_schema = r_plan.altr_schema
AND tmp_tbl_name = r_plan.altr_tblseq;
END IF;
-- repair a table
PERFORM emaj._repair_tbl(r_plan.altr_schema, r_plan.altr_tblseq, coalesce(r_plan.altr_new_group, r_plan.altr_group),
r_plan.altr_group_is_logging, v_timeId, v_callingFunction);
--
WHEN 'REPAIR_SEQ' THEN
RAISE EXCEPTION 'alter_exec: Internal error, trying to repair a sequence (%.%) is abnormal.',
Expand Down
66 changes: 43 additions & 23 deletions sql/emaj--devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3640,6 +3640,46 @@ $_drop_tbl$
END;
$_drop_tbl$;

CREATE OR REPLACE FUNCTION emaj._repair_tbl(v_schema TEXT, v_table TEXT, v_group TEXT, v_groupIsLogging BOOLEAN,
v_timeId BIGINT, v_function TEXT)
RETURNS VOID LANGUAGE plpgsql AS
$_repair_tbl$
-- The function repairs a table detected as corrupted, i.e. with any trouble detected by the emaj_verify_all() and similar functions.
-- Inputs: the schema and table names to repair
-- the group that currently owns the table, and its state
-- the time_id of the operation
-- the calling function name
DECLARE
v_isRollbackable BOOLEAN;
BEGIN
IF v_groupIsLogging THEN
RAISE EXCEPTION '_repair_tbl: Cannot repair the table %.%. Its group % is in LOGGING state. Remove first the table from its group.',
v_schema, v_table, v_group;
ELSE
-- remove the table from its group
PERFORM emaj._drop_tbl(emaj.emaj_relation.*, v_timeId)
FROM emaj.emaj_relation
WHERE rel_schema = v_schema
AND rel_tblseq = v_table
AND upper_inf(rel_time_range);
-- get the is_rollbackable status of the related group
SELECT group_is_rollbackable INTO v_isRollbackable
FROM emaj.emaj_group
WHERE group_name = v_group;
-- and recreate it
PERFORM emaj._create_tbl(v_schema, v_table, v_group, tmp_priority, tmp_log_dat_tsp, tmp_log_idx_tsp,
tmp_ignored_triggers, v_timeId, v_isRollbackable, v_groupIsLogging)
FROM tmp_app_table
WHERE tmp_group = v_group
AND tmp_schema = v_schema
AND tmp_tbl_name = v_table;
INSERT INTO emaj.emaj_hist(hist_function, hist_event, hist_object, hist_wording)
VALUES (v_function, 'TABLE REPAIRED', quote_ident(v_schema) || '.' || quote_ident(v_table), 'In group ' || v_group);
END IF;
RETURN;
END;
$_repair_tbl$;

CREATE OR REPLACE FUNCTION emaj.emaj_assign_sequence(v_schema TEXT, v_sequence TEXT, v_group TEXT, v_mark TEXT DEFAULT 'ASSIGN_%')
RETURNS INTEGER LANGUAGE plpgsql AS
$emaj_assign_sequence$
Expand Down Expand Up @@ -5860,7 +5900,6 @@ $_alter_exec$
v_logDatTsp TEXT;
v_logIdxTsp TEXT;
v_ignoredTriggers TEXT[];
v_isRollbackable BOOLEAN;
r_plan emaj.emaj_alter_plan%ROWTYPE;
r_rel emaj.emaj_relation%ROWTYPE;
BEGIN
Expand All @@ -5883,28 +5922,9 @@ $_alter_exec$
v_timeId, v_callingFunction);
--
WHEN 'REPAIR_TBL' THEN
IF r_plan.altr_group_is_logging THEN
RAISE EXCEPTION 'alter_exec: Cannot repair the table %.%. Its group % is in LOGGING state.',
r_plan.altr_schema, r_plan.altr_tblseq, r_plan.altr_group;
ELSE
-- remove the table from its group
PERFORM emaj._drop_tbl(emaj.emaj_relation.*, v_timeId)
FROM emaj.emaj_relation
WHERE rel_schema = r_plan.altr_schema
AND rel_tblseq = r_plan.altr_tblseq
AND upper_inf(rel_time_range);
-- get the is_rollbackable status of the related group
SELECT group_is_rollbackable INTO v_isRollbackable
FROM emaj.emaj_group
WHERE group_name = r_plan.altr_group;
-- and recreate it
PERFORM emaj._create_tbl(tmp_schema, tmp_tbl_name, tmp_group, tmp_priority, tmp_log_dat_tsp, tmp_log_idx_tsp,
tmp_ignored_triggers, v_timeId, v_isRollbackable, r_plan.altr_group_is_logging)
FROM tmp_app_table
WHERE tmp_group = coalesce (r_plan.altr_new_group, r_plan.altr_group)
AND tmp_schema = r_plan.altr_schema
AND tmp_tbl_name = r_plan.altr_tblseq;
END IF;
-- repair a table
PERFORM emaj._repair_tbl(r_plan.altr_schema, r_plan.altr_tblseq, coalesce(r_plan.altr_new_group, r_plan.altr_group),
r_plan.altr_group_is_logging, v_timeId, v_callingFunction);
--
WHEN 'REPAIR_SEQ' THEN
RAISE EXCEPTION 'alter_exec: Internal error, trying to repair a sequence (%.%) is abnormal.',
Expand Down
66 changes: 43 additions & 23 deletions sql/emaj-devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3647,6 +3647,46 @@ $_drop_tbl$
END;
$_drop_tbl$;

CREATE OR REPLACE FUNCTION emaj._repair_tbl(v_schema TEXT, v_table TEXT, v_group TEXT, v_groupIsLogging BOOLEAN,
v_timeId BIGINT, v_function TEXT)
RETURNS VOID LANGUAGE plpgsql AS
$_repair_tbl$
-- The function repairs a table detected as corrupted, i.e. with any trouble detected by the emaj_verify_all() and similar functions.
-- Inputs: the schema and table names to repair
-- the group that currently owns the table, and its state
-- the time_id of the operation
-- the calling function name
DECLARE
v_isRollbackable BOOLEAN;
BEGIN
IF v_groupIsLogging THEN
RAISE EXCEPTION '_repair_tbl: Cannot repair the table %.%. Its group % is in LOGGING state. Remove first the table from its group.',
v_schema, v_table, v_group;
ELSE
-- remove the table from its group
PERFORM emaj._drop_tbl(emaj.emaj_relation.*, v_timeId)
FROM emaj.emaj_relation
WHERE rel_schema = v_schema
AND rel_tblseq = v_table
AND upper_inf(rel_time_range);
-- get the is_rollbackable status of the related group
SELECT group_is_rollbackable INTO v_isRollbackable
FROM emaj.emaj_group
WHERE group_name = v_group;
-- and recreate it
PERFORM emaj._create_tbl(v_schema, v_table, v_group, tmp_priority, tmp_log_dat_tsp, tmp_log_idx_tsp,
tmp_ignored_triggers, v_timeId, v_isRollbackable, v_groupIsLogging)
FROM tmp_app_table
WHERE tmp_group = v_group
AND tmp_schema = v_schema
AND tmp_tbl_name = v_table;
INSERT INTO emaj.emaj_hist(hist_function, hist_event, hist_object, hist_wording)
VALUES (v_function, 'TABLE REPAIRED', quote_ident(v_schema) || '.' || quote_ident(v_table), 'In group ' || v_group);
END IF;
RETURN;
END;
$_repair_tbl$;

CREATE OR REPLACE FUNCTION emaj.emaj_assign_sequence(v_schema TEXT, v_sequence TEXT, v_group TEXT, v_mark TEXT DEFAULT 'ASSIGN_%')
RETURNS INTEGER LANGUAGE plpgsql AS
$emaj_assign_sequence$
Expand Down Expand Up @@ -5867,7 +5907,6 @@ $_alter_exec$
v_logDatTsp TEXT;
v_logIdxTsp TEXT;
v_ignoredTriggers TEXT[];
v_isRollbackable BOOLEAN;
r_plan emaj.emaj_alter_plan%ROWTYPE;
r_rel emaj.emaj_relation%ROWTYPE;
BEGIN
Expand All @@ -5890,28 +5929,9 @@ $_alter_exec$
v_timeId, v_callingFunction);
--
WHEN 'REPAIR_TBL' THEN
IF r_plan.altr_group_is_logging THEN
RAISE EXCEPTION 'alter_exec: Cannot repair the table %.%. Its group % is in LOGGING state.',
r_plan.altr_schema, r_plan.altr_tblseq, r_plan.altr_group;
ELSE
-- remove the table from its group
PERFORM emaj._drop_tbl(emaj.emaj_relation.*, v_timeId)
FROM emaj.emaj_relation
WHERE rel_schema = r_plan.altr_schema
AND rel_tblseq = r_plan.altr_tblseq
AND upper_inf(rel_time_range);
-- get the is_rollbackable status of the related group
SELECT group_is_rollbackable INTO v_isRollbackable
FROM emaj.emaj_group
WHERE group_name = r_plan.altr_group;
-- and recreate it
PERFORM emaj._create_tbl(tmp_schema, tmp_tbl_name, tmp_group, tmp_priority, tmp_log_dat_tsp, tmp_log_idx_tsp,
tmp_ignored_triggers, v_timeId, v_isRollbackable, r_plan.altr_group_is_logging)
FROM tmp_app_table
WHERE tmp_group = coalesce (r_plan.altr_new_group, r_plan.altr_group)
AND tmp_schema = r_plan.altr_schema
AND tmp_tbl_name = r_plan.altr_tblseq;
END IF;
-- repair a table
PERFORM emaj._repair_tbl(r_plan.altr_schema, r_plan.altr_tblseq, coalesce(r_plan.altr_new_group, r_plan.altr_group),
r_plan.altr_group_is_logging, v_timeId, v_callingFunction);
--
WHEN 'REPAIR_SEQ' THEN
RAISE EXCEPTION 'alter_exec: Internal error, trying to repair a sequence (%.%) is abnormal.',
Expand Down
5 changes: 3 additions & 2 deletions test/10/expected/check.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ select count(*) from pg_proc, pg_namespace
where pg_namespace.oid=pronamespace and nspname = 'emaj' and (proname like E'emaj\\_%' or proname like E'\\_%');
count
-------
172
173
(1 row)

select count(*) from pg_proc, pg_namespace
Expand Down Expand Up @@ -160,6 +160,7 @@ select funcname, calls from pg_stat_user_functions
_remove_sequences | 19
_remove_tables | 32
_remove_tbl | 36
_repair_tbl | 2
_reset_groups | 98
_rlbk_async | 2
_rlbk_check | 100
Expand Down Expand Up @@ -256,7 +257,7 @@ select funcname, calls from pg_stat_user_functions
emaj_unprotect_group | 7
emaj_unprotect_mark_group | 6
emaj_verify_all | 47
(168 rows)
(169 rows)

-- count the total number of user-callable function calls (those who failed are not counted)
select sum(calls) from pg_stat_user_functions where funcname like E'emaj\\_%';
Expand Down
3 changes: 2 additions & 1 deletion test/10/expected/create_drop.out
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ select hist_function, hist_event, hist_object,
DISABLE_PROTECTION | EVENT TRIGGERS DISABLED | | emaj_protection_trg, emaj_sql_drop_trg, emaj_table_rewrite_trg | postgres
ENABLE_PROTECTION | EVENT TRIGGERS ENABLED | | emaj_protection_trg, emaj_sql_drop_trg, emaj_table_rewrite_trg | postgres
IMPORT_GROUPS | BEGIN | emptyGroup, myGroup1, myGroup2, myGroup4, myGroup5, myGroup6, phil's group#3", | Input file: '/tmp/emaj_10/create_drop/orig_groups_config_all.json' | postgres
IMPORT_GROUPS | TABLE REPAIRED | myschema1.mytbl2 | In group myGroup1 | postgres
IMPORT_GROUPS | END | | 7 created or altered tables groups | postgres
START_GROUP | BEGIN | myGroup1 | With log reset | postgres
LOCK_GROUP | BEGIN | myGroup1 | | postgres
Expand Down Expand Up @@ -1703,7 +1704,7 @@ select hist_function, hist_event, hist_object,
FORCE_DROP_GROUP | BEGIN | myGroup1 | | postgres
FORCE_DROP_GROUP | LOG_SCHEMA DROPPED | emaj_myschema1 | | postgres
FORCE_DROP_GROUP | END | myGroup1 | 1 tables/sequences processed | postgres
(228 rows)
(229 rows)

select time_id, time_last_emaj_gid, time_event from emaj.emaj_time_stamp order by time_id;
time_id | time_last_emaj_gid | time_event
Expand Down

0 comments on commit 80f4a49

Please sign in to comment.