row_number()over(orderbyv.depthdesc,v.source_table)asordering,-- Ordre important : on commence avec les tables les plus "éloignées"
v.depth,
v.source_table::varchar,
format('Parcours des tables liées de près ou de loin à %I en amont (tables mères)',ct.table_name)::varchar,
format('delete from %s where id not in (%s)',v.source_table,string_agg(v.sql,chr(10)||'union'||chr(10)))::varchar
fromdb_prune_tmp_v_fks_amont_sqlv,
db_prune_tmp_central_data_paramsct
groupbyct.table_name,v.depth,v.source_table
unionall
select2,
1,
0,
ct.table_name,
format('Table centrale "%I"',ct.table_name)::varchar,
format('delete from %I where id not in (%s)',ct.table_name,ct.fetch_ids_sql)::varchar
fromdb_prune_tmp_central_data_paramsct
unionall
select3,
row_number()over(orderbyv.depth,v.target_table),-- Ordre important : on commence avec les tables les plus "proches"
v.depth,
v.target_table::varchar,
format('Parcours des tables liées de près ou de loin à %I en aval (tables filles)',ct.table_name)::varchar,
format('delete from %I where id not in (select distinct id from (%s) t)',v.target_table,string_agg(v.sql,chr(10)||'union'||chr(10)orderbyv.subdepth))::varchar
fromdb_prune_tmp_v_fks_aval_sqlv,
db_prune_tmp_central_data_paramsct
groupbyct.table_name,v.depth,v.target_table
--
orderbystep,ordering;
end
$$;
--
-- Lancement des delete, avec affichage du nombre de suppressions.
--
dropprocedureifexistsdb_prune_tmp_proc_prune;
createorreplaceproceduredb_prune_tmp_proc_prune()
languageplpgsql
as
$$declare
v_resultrecord;
v_countint;
v_templatevarchar='with deleted as (%s returning *) select count(*) from deleted';