-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpart_triggers.sql
93 lines (76 loc) · 2.39 KB
/
part_triggers.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
create or replace function partition.create_part_trigger
(
p_schemaname text,
p_tablename text
)
returns void
language plpgsql
as $BODY$
declare
part text ;
v_insert text ;
v_column text ;
v_pattern text ;
begin
select t.keycolumn, p.to_char_pattern into v_column, v_pattern
from partition.table t join partition.pattern p on t.pattern=p.id
where t.schemaname = p_schemaname and t.tablename = p_tablename ;
if FOUND then
execute 'create or replace function partition.partitionne_'||p_schemaname||'_'||p_tablename||'()
returns trigger
language plpgsql
as $PART$
declare
part text ;
v_column text ;
v_pattern text ;
begin
IF TG_OP = ''INSERT''
THEN
SELECT to_char( NEW.'|| v_column ||', ' || quote_literal( v_pattern ) || ') INTO part ;
execute ''INSERT INTO '' || TG_TABLE_SCHEMA || ''.'' || TG_TABLE_NAME || ''_'' || part
|| '' SELECT ('' || quote_literal(textin(record_out(NEW)))
|| ''::'' || TG_TABLE_SCHEMA || ''.'' || TG_TABLE_NAME || '').*;'' ;
RETURN null ;
else
return new ;
END IF;
end ;
$PART$ ; ' ;
execute 'create trigger _partitionne before insert on '
||p_schemaname||'.'||p_tablename||' for each row '
||'execute procedure partition.partitionne_'
||p_schemaname||'_'||p_tablename||'() ; ' ;
end if ;
end ;
$BODY$
;
create or replace function partition.set_trigger_def()
returns trigger
language plpgsql
as $BODY$
declare
r_trigg record ;
begin
set local search_path to pg_catalog ;
if TG_OP = 'INSERT' then
for r_trigg in select n.nspname, c.relname, t.tgname,
pg_get_triggerdef( t.oid ) as triggerdef
from pg_class c
join pg_namespace n on c.relnamespace=n.oid
join pg_trigger t on c.oid=t.tgrelid
where c.relkind='r'
and ( t.tgconstraint is null or t.tgconstraint = 0 )
and n.nspname = new.schemaname and c.relname = new.tablename
and t.tgname != '_partitionne'
loop
insert into partition.trigger values ( r_trigg.nspname, r_trigg.relname, r_trigg.tgname, r_trigg.triggerdef ) ;
execute 'drop trigger ' || r_trigg.tgname || ' on ' || r_trigg.nspname || '.' || r_trigg.relname ;
end loop ;
end if ;
return new ;
end ;
$BODY$ ;
create trigger _settrigg after insert on partition.table
for each row
execute procedure partition.set_trigger_def() ;