Skip to content

Commit

Permalink
In emaj_gen_sql_group() and emaj_gen_sql_groups() functions, process …
Browse files Browse the repository at this point in the history
…the tables or sequences that have been removed from their group in the requested mark range. They were initialy excluded.
  • Loading branch information
beaud76 committed Jan 15, 2018
1 parent 690ebff commit 6c3c947
Show file tree
Hide file tree
Showing 24 changed files with 808 additions and 447 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ E-Maj - Change log
* Adjust the behaviour of some functions that process past time frame,
when tables have been removed from their group. Now
emaj_delete_mark_group(), emaj_get_consolidable_rollbacks(),
emaj_consolidate_rollback_group() and emaj_snap_log_group() functions take
emaj_consolidate_rollback_group(), emaj_snap_log_group(),
emaj_gen_sql_group() and emaj_gen_sql_groups() functions take
into account the real group content on the time frame they process.
* The emaj_snap_log_group() function now returns the number of generated
files, and the generated file names are directly derived from log table
Expand Down
242 changes: 242 additions & 0 deletions sql/emaj--2.2.1--next_version.sql

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions sql/emaj--next_version.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6549,6 +6549,7 @@ $_gen_sql_groups$
v_fullSeqName TEXT;
v_endComment TEXT;
v_conditions TEXT;
v_endTimeId BIGINT;
v_rqSeq TEXT;
r_tblsq RECORD;
r_rel emaj.emaj_relation%ROWTYPE;
Expand Down Expand Up @@ -6645,7 +6646,7 @@ $_gen_sql_groups$
SELECT t FROM unnest(v_tblseqs) AS t
EXCEPT
SELECT rel_schema || '.' || rel_tblseq FROM emaj.emaj_relation
WHERE upper_inf(rel_time_range) AND rel_group = ANY (v_groupNames)
WHERE rel_time_range @> v_firstMarkTimeId AND rel_group = ANY (v_groupNames) -- tables/sequences that belong to their group at the start mark time
) AS t2;
IF v_tblseqErr IS NOT NULL THEN
RAISE EXCEPTION '_gen_sql_groups: Some tables and/or sequences (%) do not belong to any of the selected tables groups.', v_tblseqErr;
Expand Down Expand Up @@ -6676,9 +6677,11 @@ $_gen_sql_groups$
END IF;
FOR r_rel IN
SELECT * FROM emaj.emaj_relation
WHERE upper_inf(rel_time_range) AND rel_group = ANY (v_groupNames) AND rel_kind = 'r' -- tables currently belonging to the groups
WHERE rel_group = ANY (v_groupNames) AND rel_kind = 'r' -- tables belonging to the groups
AND rel_time_range @> v_firstMarkTimeId -- at the first mark time
AND (v_tblseqs IS NULL OR rel_schema || '.' || rel_tblseq = ANY (v_tblseqs)) -- filtered or not by the user
AND emaj._log_stat_tbl(emaj_relation, v_firstMarkTimeId, v_lastMarkTimeId) > 0 -- only tables having updates to process
AND emaj._log_stat_tbl(emaj_relation, v_firstMarkTimeId, -- only tables having updates to process
CASE WHEN v_lastMarkTimeId < upper(rel_time_range) THEN v_lastMarkTimeId ELSE upper(rel_time_range) END) > 0
ORDER BY rel_priority, rel_schema, rel_tblseq
LOOP
-- process the application table, by calling the _gen_sql_tbl function
Expand All @@ -6688,14 +6691,15 @@ $_gen_sql_groups$
-- process sequences
v_nbSeq = 0;
FOR r_tblsq IN
SELECT rel_priority, rel_schema, rel_tblseq FROM emaj.emaj_relation
WHERE upper_inf(rel_time_range) AND rel_group = ANY (v_groupNames) AND rel_kind = 'S' -- sequences currently belonging to the groups
AND (v_tblseqs IS NULL OR rel_schema || '.' || rel_tblseq = ANY (v_tblseqs)) -- filtered or not by the user
SELECT rel_priority, rel_schema, rel_tblseq, rel_time_range FROM emaj.emaj_relation
WHERE rel_group = ANY (v_groupNames) AND rel_kind = 'S'
AND rel_time_range @> v_firstMarkTimeId -- sequences belonging to the groups at the start mark
AND (v_tblseqs IS NULL OR rel_schema || '.' || rel_tblseq = ANY (v_tblseqs)) -- filtered or not by the user
ORDER BY rel_priority DESC, rel_schema DESC, rel_tblseq DESC
LOOP
v_fullSeqName = quote_ident(r_tblsq.rel_schema) || '.' || quote_ident(r_tblsq.rel_tblseq);
IF v_lastMarkTimeId IS NULL THEN
-- no supplied last mark, so get current sequence characteritics
IF v_lastMarkTimeId IS NULL AND upper_inf(r_tblsq.rel_time_range) THEN
-- no supplied last mark and the sequence currently belongs to its group, so get current sequence characteritics
IF emaj._pg_version_num() < 100000 THEN
EXECUTE 'SELECT ''ALTER SEQUENCE ' || replace(v_fullSeqName,'''','''''')
|| ''' || '' RESTART '' || CASE WHEN is_called THEN last_value + increment_by ELSE last_value END || '' START '' || start_value || '' INCREMENT '' || increment_by || '' MAXVALUE '' || max_value || '' MINVALUE '' || min_value || '' CACHE '' || cache_value || CASE WHEN NOT is_cycled THEN '' NO'' ELSE '''' END || '' CYCLE;'' '
Expand All @@ -6705,15 +6709,17 @@ $_gen_sql_groups$
|| ''' || '' RESTART '' || CASE WHEN rel.is_called THEN rel.last_value + increment_by ELSE rel.last_value END || '' START '' || start_value || '' INCREMENT '' || increment_by || '' MAXVALUE '' || max_value || '' MINVALUE '' || min_value || '' CACHE '' || cache_size || CASE WHEN NOT cycle THEN '' NO'' ELSE '''' END || '' CYCLE;'' '
|| 'FROM ' || v_fullSeqName || ' rel, pg_catalog.pg_sequences ' ||
' WHERE schemaname = ' || quote_literal(r_tblsq.rel_schema) || ' AND sequencename = ' || quote_literal(r_tblsq.rel_tblseq) INTO v_rqSeq;
END IF;
END IF;
ELSE
-- a last mark is supplied, so get sequence characteristics from emaj_sequence table
-- a last mark is supplied, or the sequence does not belong to its groupe anymore, so get sequence characteristics from the emaj_sequence table
v_endTimeId = CASE WHEN upper_inf(r_tblsq.rel_time_range) OR v_lastMarkTimeId < upper(r_tblsq.rel_time_range) THEN v_lastMarkTimeId
ELSE upper(r_tblsq.rel_time_range) END;
EXECUTE 'SELECT ''ALTER SEQUENCE ' || replace(v_fullSeqName,'''','''''')
|| ''' || '' RESTART '' || CASE WHEN sequ_is_called THEN sequ_last_val + sequ_increment ELSE sequ_last_val END || '' START '' || sequ_start_val || '' INCREMENT '' || sequ_increment || '' MAXVALUE '' || sequ_max_val || '' MINVALUE '' || sequ_min_val || '' CACHE '' || sequ_cache_val || CASE WHEN NOT sequ_is_cycled THEN '' NO'' ELSE '''' END || '' CYCLE;'' '
|| 'FROM emaj.emaj_sequence '
|| 'WHERE sequ_schema = ' || quote_literal(r_tblsq.rel_schema)
|| ' AND sequ_name = ' || quote_literal(r_tblsq.rel_tblseq)
|| ' AND sequ_time_id = ' || v_lastMarkTimeId INTO v_rqSeq;
|| ' AND sequ_time_id = ' || v_endTimeId INTO v_rqSeq;
END IF;
-- insert into temp table
v_nbSeq = v_nbSeq + 1;
Expand Down
6 changes: 3 additions & 3 deletions test/10/expected/adm2.out
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ mkdir: cannot create directory ‘/tmp/emaj_test/sql_scripts’: No such file or
-- generate sql script for each active group (and check the result with detailed log statistics + number of sequences)
select emaj.emaj_gen_sql_group('myGroup1', 'Multi-1', NULL, '/tmp/emaj_test/sql_scripts/myGroup1.sql');
ERROR: _gen_sql_groups: The file "/tmp/emaj_test/sql_scripts/myGroup1.sql" cannot be used as script output file.
CONTEXT: PL/pgSQL function emaj._gen_sql_groups(text[],text,text,text,text[]) line 143 at RAISE
CONTEXT: PL/pgSQL function emaj._gen_sql_groups(text[],text,text,text,text[]) line 144 at RAISE
SQL statement "SELECT emaj._gen_sql_groups(CASE WHEN v_groupName IS NOT NULL THEN array[v_groupName] ELSE NULL END, v_firstMark, v_lastMark, v_location, v_tblseqs)"
PL/pgSQL function emaj.emaj_gen_sql_group(text,text,text,text,text[]) line 22 at SQL statement
select coalesce(sum(stat_rows),0) + 1 as check from emaj.emaj_detailed_log_stat_group('myGroup1', 'Multi-1', NULL);
Expand All @@ -1178,7 +1178,7 @@ select emaj.emaj_gen_sql_group('myGroup2', 'Multi-1', NULL, '/tmp/emaj_test/sql_
'myschema2.mytbl1','myschema2.mytbl2','myschema2.myTbl3','myschema2.mytbl4',
'myschema2.mytbl5','myschema2.mytbl6','myschema2.myseq1','myschema2.myTbl3_col31_seq']);
ERROR: _gen_sql_groups: The file "/tmp/emaj_test/sql_scripts/myGroup2.sql" cannot be used as script output file.
CONTEXT: PL/pgSQL function emaj._gen_sql_groups(text[],text,text,text,text[]) line 143 at RAISE
CONTEXT: PL/pgSQL function emaj._gen_sql_groups(text[],text,text,text,text[]) line 144 at RAISE
SQL statement "SELECT emaj._gen_sql_groups(CASE WHEN v_groupName IS NOT NULL THEN array[v_groupName] ELSE NULL END, v_firstMark, v_lastMark, v_location, v_tblseqs)"
PL/pgSQL function emaj.emaj_gen_sql_group(text,text,text,text,text[]) line 22 at SQL statement
select sum(stat_rows) + 2 as check from emaj.emaj_detailed_log_stat_group('myGroup2', 'Multi-1', NULL);
Expand All @@ -1189,7 +1189,7 @@ select sum(stat_rows) + 2 as check from emaj.emaj_detailed_log_stat_group('myGro

select emaj.emaj_gen_sql_group('phil''s group#3",', 'M1_rollbackable', NULL, '/tmp/emaj_test/sql_scripts/Group3.sql');
ERROR: _gen_sql_groups: The file "/tmp/emaj_test/sql_scripts/Group3.sql" cannot be used as script output file.
CONTEXT: PL/pgSQL function emaj._gen_sql_groups(text[],text,text,text,text[]) line 143 at RAISE
CONTEXT: PL/pgSQL function emaj._gen_sql_groups(text[],text,text,text,text[]) line 144 at RAISE
SQL statement "SELECT emaj._gen_sql_groups(CASE WHEN v_groupName IS NOT NULL THEN array[v_groupName] ELSE NULL END, v_firstMark, v_lastMark, v_location, v_tblseqs)"
PL/pgSQL function emaj.emaj_gen_sql_group(text,text,text,text,text[]) line 22 at SQL statement
select sum(stat_rows) + 1 as check from emaj.emaj_detailed_log_stat_group('phil''s group#3",', 'M1_rollbackable', NULL);
Expand Down

0 comments on commit 6c3c947

Please sign in to comment.