-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_for_keys--0.0.1.sql
624 lines (547 loc) · 18.8 KB
/
time_for_keys--0.0.1.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/* time_for_keys--0.0.1.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION time_for_keys" to load this file. \quit
-- TODO: Make this generic for any range type?:
CREATE OR REPLACE FUNCTION
completely_covers_transfn(internal, tstzrange, tstzrange)
RETURNS internal
AS 'time_for_keys', 'completely_covers_transfn'
LANGUAGE c;
CREATE OR REPLACE FUNCTION
completely_covers_finalfn(internal, tstzrange, tstzrange)
RETURNS boolean
AS 'time_for_keys', 'completely_covers_finalfn'
LANGUAGE c;
/*
* completely_covers(period tstzrange, target tstzrange) -
* Returns true if the fixed arg `target`
* is completely covered by the sum of the `period` values.
*/
CREATE AGGREGATE completely_covers(tstzrange, tstzrange) (
sfunc = completely_covers_transfn,
stype = internal,
finalfunc = completely_covers_finalfn,
finalfunc_extra
);
-- TODO: Use a more "private" prefix for all our helper functions:
-- These functions are named like the built-in foreign key functions,
-- which you can see in psql by saying `\dftS`.
-- TRI = Temporal Referential Integrity.
-- TODO: Instead of plpgsql we could implement these all as internal C functions,
-- like built-in foreign keys.
-- Then we could cache their query plans and cache the table/column details for each one.
-- Also we could present them better in `\d foo` output.
-- But using the normal `CREATE CONSTRAINT TRIGGER` approach
-- seems a lot easier for our initial feedback-wanted version:
-- TODO: TRI_FKey_cascade_del
CREATE OR REPLACE FUNCTION TRI_FKey_cascade_del()
RETURNS trigger
AS
$$
DECLARE
from_table TEXT;
fk_column TEXT;
from_range_column TEXT;
to_table TEXT;
pk_column TEXT;
to_range_column TEXT;
-- TODO: Support other types!
-- This is really tricky because we have to get the value from OLD.${fk_column}.
old_pk_val INTEGER;
old_pk_range tstzrange;
BEGIN
IF NOT TG_OP = 'DELETE' THEN
RAISE EXCEPTION 'TRI_FKey_cascade_del must be called from a DELETE';
END IF;
from_table := quote_ident(TG_ARGV[0]);
fk_column := quote_ident(TG_ARGV[1]);
from_range_column := quote_ident(TG_ARGV[2]);
to_table := quote_ident(TG_ARGV[3]);
pk_column := quote_ident(TG_ARGV[4]);
to_range_column := quote_ident(TG_ARGV[5]);
EXECUTE format('SELECT ($1:%1$s)::text, $1.%2$s', pk_column, to_range_column)
USING OLD INTO old_pk_val, old_pk_range;
-- TODO: This belongs in check_upd instead:
-- EXECUTE format('SELECT ($1:%1$s)::text, $1.%2$s, ($2:%1$s)::text, $2.%2$s',
-- fk_column, from_range_column)
-- USING OLD, NEW INTO old_fk_val, old_from_range, new_fk_val, new_from_range;
-- The FK didn't change so no need to check anything:
-- IF old_fk_val = new_fk_val AND old_from_range = new_from_range THEN RETURN NEW; END IF;
-- No FK:
-- IF new_fk_val IS NULL THEN RETURN NEW; END IF;
EXECUTE format($q$
DELETE FROM %1$s
WHERE %2$s = $1
AND %3$s <@ $2
$q$, from_table, fk_column, to_table, pk_column)
USING old_pk_val, old_pk_range;
-- TODO: Might need to split into two rows:
EXECUTE format($q$
UPDATE %1$s
-- TODO: .....
$q$, from_table, fk_column, to_table, pk_column)
USING old_pk_val, old_pk_range;
END;
$$
LANGUAGE plpgsql;
-- TODO: TRI_FKey_cascade_upd
-- Check Temporal Foreign Key existence (combined for INSERT AND UPDATE).
CREATE OR REPLACE FUNCTION
TRI_FKey_check(
from_table TEXT, fk_column TEXT, from_range_column TEXT,
to_table TEXT, pk_column TEXT, to_range_column TEXT,
fk_val INTEGER, from_range tstzrange, updating BOOLEAN)
RETURNS BOOLEAN
AS
$$
DECLARE
okay BOOLEAN;
BEGIN
-- If the FK column is NULL then there is nothing to check:
IF fk_val IS NULL THEN RETURN true; END IF;
EXECUTE format($q$
SELECT completely_covers(%1$s.%3$s, $2 ORDER BY %1$s.%3$s)
FROM %1$s
WHERE %1$s.%2$s = $1
$q$,
to_table, pk_column, to_range_column) USING fk_val, from_range INTO okay;
IF okay THEN
RETURN true;
ELSE
-- false or null both imply this:
IF updating THEN
RAISE EXCEPTION 'Tried to update to % in %.% but couldn''t find it in %.% for all of [%, %)',
fk_val::text, from_table, fk_column,
to_table, pk_column, lower(from_range)::text, upper(from_range)::text
USING ERRCODE = 23503;
ELSE
RAISE EXCEPTION 'Tried to insert % to %.% but couldn''t find it in %.% for all of [%, %)',
fk_val::text, from_table, fk_column,
to_table, pk_column, lower(from_range)::text, upper(from_range)::text
USING ERRCODE = 23503;
END IF;
END IF;
END;
$$
LANGUAGE plpgsql;
-- Checks the FK when a new row is added to the child table:
CREATE OR REPLACE FUNCTION
TRI_FKey_check_ins()
RETURNS trigger
AS
$$
DECLARE
from_table TEXT;
fk_column TEXT;
from_range_column TEXT;
to_table TEXT;
pk_column TEXT;
to_range_column TEXT;
fk_val INTEGER;
from_range tstzrange;
BEGIN
IF NOT TG_OP = 'INSERT' THEN
RAISE EXCEPTION 'TRI_FKey_check_ins must be called from an INSERT';
END IF;
from_table := quote_ident(TG_ARGV[0]);
fk_column := quote_ident(TG_ARGV[1]);
from_range_column := quote_ident(TG_ARGV[2]);
to_table := quote_ident(TG_ARGV[3]);
pk_column := quote_ident(TG_ARGV[4]);
to_range_column := quote_ident(TG_ARGV[5]);
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s', fk_column, from_range_column)
USING NEW INTO fk_val, from_range;
IF TRI_FKey_check(from_table, fk_column, from_range_column,
to_table, pk_column, to_range_column,
fk_val, from_range, false) THEN
RETURN NEW;
ELSE
-- Should have raised already:
RAISE EXCEPTION 'Should be unreachable';
END IF;
END
$$
LANGUAGE plpgsql;
-- Checks the FK when an old row is changed in the child table:
CREATE OR REPLACE FUNCTION
TRI_FKey_check_upd()
RETURNS trigger
AS
$$
DECLARE
from_table TEXT;
fk_column TEXT;
from_range_column TEXT;
to_table TEXT;
pk_column TEXT;
to_range_column TEXT;
fk_val INTEGER;
from_range tstzrange;
BEGIN
IF NOT TG_OP = 'UPDATE' THEN
RAISE EXCEPTION 'TRI_FKey_check_upd must be called from an UPDATE';
END IF;
from_table := quote_ident(TG_ARGV[0]);
fk_column := quote_ident(TG_ARGV[1]);
from_range_column := quote_ident(TG_ARGV[2]);
to_table := quote_ident(TG_ARGV[3]);
pk_column := quote_ident(TG_ARGV[4]);
to_range_column := quote_ident(TG_ARGV[5]);
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s', fk_column, from_range_column)
USING NEW INTO fk_val, from_range;
IF TRI_FKey_check(from_table, fk_column, from_range_column,
to_table, pk_column, to_range_column,
fk_val, from_range, true) THEN
RETURN NEW;
ELSE
-- Should have raised already:
RAISE EXCEPTION 'Should be unreachable';
END IF;
END
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION TRI_FKey_restrict(
from_table TEXT, fk_column TEXT, from_range_column TEXT,
to_table TEXT, pk_column TEXT, to_range_column TEXT,
old_pk_val INTEGER, old_pk_range tstzrange)
RETURNS BOOLEAN
AS
$$
DECLARE
tmp_table TEXT;
okay BOOLEAN;
BEGIN
IF to_table = 'old_references' THEN
tmp_table = 'old_references2';
ELSE
tmp_table = 'old_references';
END IF;
EXECUTE format($q$
SELECT NOT EXISTS (
WITH %7$s AS (
SELECT %1$s.%2$s fk, %1$s.%3$s valid_at
FROM ONLY %1$s
WHERE %1$s.%2$s = $1
AND %1$s.%3$s && $2
)
SELECT 1
FROM %7$s
LEFT OUTER JOIN ONLY %4$s
ON %4$s.%5$s = %7$s.fk
GROUP BY %7$s.fk, %7$s.valid_at
HAVING NOT COALESCE(completely_covers(%4$s.%6$s, %7$s.valid_at ORDER BY %4$s.%6$s), false)
)
$q$, from_table, fk_column, from_range_column, to_table, pk_column, to_range_column, tmp_table)
USING old_pk_val, old_pk_range
INTO okay;
RETURN okay;
END;
$$
LANGUAGE plpgsql;
-- Rejects the DELETE if any FKs stil reference the old row:
-- (This is identical to TRI_FKey_restrict_del.)
CREATE OR REPLACE FUNCTION TRI_FKey_noaction_del()
RETURNS trigger
AS
$$
DECLARE
from_table TEXT;
fk_column TEXT;
from_range_column TEXT;
to_table TEXT;
pk_column TEXT;
to_range_column TEXT;
old_pk_val INTEGER;
old_pk_range tstzrange;
okay BOOLEAN;
BEGIN
IF NOT TG_OP = 'DELETE' THEN
RAISE EXCEPTION 'TRI_FKey_noaction_del must be called from a DELETE';
END IF;
from_table := quote_ident(TG_ARGV[0]);
fk_column := quote_ident(TG_ARGV[1]);
from_range_column := quote_ident(TG_ARGV[2]);
to_table := quote_ident(TG_ARGV[3]);
pk_column := quote_ident(TG_ARGV[4]);
to_range_column := quote_ident(TG_ARGV[5]);
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s', pk_column, to_range_column)
USING OLD INTO old_pk_val, old_pk_range;
IF TRI_FKey_restrict(from_table, fk_column, from_range_column,
to_table, pk_column, to_range_column,
old_pk_val, old_pk_range) THEN
RETURN NULL;
ELSE
RAISE EXCEPTION 'Tried to delete % during [%, %) from % but there are overlapping references in %.%',
old_pk_val, lower(old_pk_range), upper(old_pk_range), to_table, from_table, fk_column
USING ERRCODE = 23503;
END IF;
END;
$$
LANGUAGE plpgsql;
-- Rejects the UPDATE if any FKs stil reference the old row:
-- (This is identical to TRI_FKey_restrict_upd.)
CREATE OR REPLACE FUNCTION TRI_FKey_noaction_upd()
RETURNS trigger
AS
$$
DECLARE
from_table TEXT;
fk_column TEXT;
from_range_column TEXT;
to_table TEXT;
pk_column TEXT;
to_range_column TEXT;
old_pk_val INTEGER;
new_pk_val INTEGER;
old_pk_range tstzrange;
new_pk_range tstzrange;
tmp_table TEXT;
BEGIN
IF NOT TG_OP = 'UPDATE' THEN
RAISE EXCEPTION 'TRI_FKey_noaction_upd must be called from an UPDATE';
END IF;
from_table := quote_ident(TG_ARGV[0]);
fk_column := quote_ident(TG_ARGV[1]);
from_range_column := quote_ident(TG_ARGV[2]);
to_table := quote_ident(TG_ARGV[3]);
pk_column := quote_ident(TG_ARGV[4]);
to_range_column := quote_ident(TG_ARGV[5]);
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s, ($2.%1$s)::text, $2.%2$s',
pk_column, to_range_column)
USING OLD, NEW INTO old_pk_val, old_pk_range, new_pk_val, new_pk_range;
-- If the PK+range didn't change then no need to check anything:
IF old_pk_val = new_pk_val AND old_pk_range = new_pk_range THEN RETURN NEW; END IF;
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s', pk_column, to_range_column)
USING OLD INTO old_pk_val, old_pk_range;
IF TRI_FKey_restrict(from_table, fk_column, from_range_column,
to_table, pk_column, to_range_column,
old_pk_val, old_pk_range) THEN
RETURN NEW;
ELSE
RAISE EXCEPTION 'Tried to update % during [%, %) from % but there are overlapping references in %.%',
old_pk_val, lower(old_pk_range), upper(old_pk_range), to_table, from_table, fk_column
USING ERRCODE = 23503;
END IF;
END;
$$
LANGUAGE plpgsql;
-- Rejects the DELETE if any FKs stil reference the old row:
CREATE OR REPLACE FUNCTION TRI_FKey_restrict_del()
RETURNS trigger
AS
$$
DECLARE
from_table TEXT;
fk_column TEXT;
from_range_column TEXT;
to_table TEXT;
pk_column TEXT;
to_range_column TEXT;
old_pk_val INTEGER;
old_pk_range tstzrange;
okay BOOLEAN;
BEGIN
IF NOT TG_OP = 'DELETE' THEN
RAISE EXCEPTION 'TRI_FKey_restrict_del must be called from a DELETE';
END IF;
from_table := quote_ident(TG_ARGV[0]);
fk_column := quote_ident(TG_ARGV[1]);
from_range_column := quote_ident(TG_ARGV[2]);
to_table := quote_ident(TG_ARGV[3]);
pk_column := quote_ident(TG_ARGV[4]);
to_range_column := quote_ident(TG_ARGV[5]);
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s', pk_column, to_range_column)
USING OLD INTO old_pk_val, old_pk_range;
IF TRI_FKey_restrict(from_table, fk_column, from_range_column,
to_table, pk_column, to_range_column,
old_pk_val, old_pk_range) THEN
RETURN NULL;
ELSE
RAISE EXCEPTION 'Tried to delete % during [%, %) from % but there are overlapping references in %.%',
old_pk_val, lower(old_pk_range), upper(old_pk_range), to_table, from_table, fk_column
USING ERRCODE = 23503;
END IF;
END;
$$
LANGUAGE plpgsql;
-- Rejects the UPDATE if any FKs stil reference the old row:
CREATE OR REPLACE FUNCTION TRI_FKey_restrict_upd()
RETURNS trigger
AS
$$
DECLARE
from_table TEXT;
fk_column TEXT;
from_range_column TEXT;
to_table TEXT;
pk_column TEXT;
to_range_column TEXT;
old_pk_val INTEGER;
new_pk_val INTEGER;
old_pk_range tstzrange;
new_pk_range tstzrange;
tmp_table TEXT;
BEGIN
IF NOT TG_OP = 'UPDATE' THEN
RAISE EXCEPTION 'TRI_FKey_restrict_upd must be called from an UPDATE';
END IF;
from_table := quote_ident(TG_ARGV[0]);
fk_column := quote_ident(TG_ARGV[1]);
from_range_column := quote_ident(TG_ARGV[2]);
to_table := quote_ident(TG_ARGV[3]);
pk_column := quote_ident(TG_ARGV[4]);
to_range_column := quote_ident(TG_ARGV[5]);
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s, ($2.%1$s)::text, $2.%2$s',
pk_column, to_range_column)
USING OLD, NEW INTO old_pk_val, old_pk_range, new_pk_val, new_pk_range;
-- If the PK+range didn't change then no need to check anything:
IF old_pk_val = new_pk_val AND old_pk_range = new_pk_range THEN RETURN NEW; END IF;
EXECUTE format('SELECT ($1.%1$s)::text, $1.%2$s', pk_column, to_range_column)
USING OLD INTO old_pk_val, old_pk_range;
IF TRI_FKey_restrict(from_table, fk_column, from_range_column,
to_table, pk_column, to_range_column,
old_pk_val, old_pk_range) THEN
RETURN NEW;
ELSE
RAISE EXCEPTION 'Tried to update % during [%, %) from % but there are overlapping references in %.%',
old_pk_val, lower(old_pk_range), upper(old_pk_range), to_table, from_table, fk_column
USING ERRCODE = 23503;
END IF;
END;
$$
LANGUAGE plpgsql;
-- TODO: TRI_FKey_setdefault_del
-- TODO: TRI_FKey_setdefault_upd
-- TODO: TRI_FKey_setnull_del
-- TODO: TRI_FKey_setnull_upd
-- TODO: need a version that takes schema names too:
CREATE OR REPLACE FUNCTION create_temporal_foreign_key(
constraint_name TEXT,
from_table TEXT, from_column TEXT, from_range_column TEXT,
to_table TEXT, to_column TEXT, to_range_column TEXT)
RETURNS VOID
AS
$$
DECLARE
fk_val INTEGER;
from_range tstzrange;
BEGIN
-- TODO: Support CASCADE/SET NULL/SET DEFAULT:
-- TODO: These should be deferrable to support moving a change's time.
-- TODO: I should probably have some kind of normalize operation....
-- Using the name like this is not ideal since it means `constraint_name` can't be 63 chars.
-- The built-in FKs save the user-provided name for the constraint,
-- and then create internal constraint triggers as a two-step process,
-- so they get the constraint trigger's oid before saving the name.
-- Oh well, there is still lots of room.
-- If we wanted to maintain our own catalog we could make it an oid-enabled table,
-- and then we could use the single "temporal foreign key constraint" oid
-- to name these triggers.
-- Check the PK when it's DELETEd:
EXECUTE format($q$
CREATE CONSTRAINT TRIGGER %1$s
AFTER DELETE ON %2$s
DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE TRI_FKey_restrict_del(%3$s, %4$s, %5$s, %6$s, %7$s, %8$s)
$q$,
quote_ident(concat('TRI_ConstraintTrigger_a_', constraint_name, '_del')),
quote_ident(to_table),
quote_nullable(from_table),
quote_nullable(from_column),
quote_nullable(from_range_column),
quote_nullable(to_table),
quote_nullable(to_column),
quote_nullable(to_range_column));
-- TODO: Support CASCASE/SET NULL/SET DEFAULT:
-- Check the PK when it's UPDATEd:
EXECUTE format($q$
CREATE CONSTRAINT TRIGGER %1$s
AFTER UPDATE ON %2$s
DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE TRI_FKey_restrict_upd(%3$s, %4$s, %5$s, %6$s, %7$s, %8$s)
$q$,
quote_ident(concat('TRI_ConstraintTrigger_a_', constraint_name, '_upd')),
quote_ident(to_table),
quote_nullable(from_table),
quote_nullable(from_column),
quote_nullable(from_range_column),
quote_nullable(to_table),
quote_nullable(to_column),
quote_nullable(to_range_column));
-- Check the FK when it's INSERTed:
EXECUTE format($q$
CREATE CONSTRAINT TRIGGER %1$s
AFTER INSERT ON %2$s
DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE TRI_FKey_check_ins(%3$s, %4$s, %5$s, %6$s, %7$s, %8$s)
$q$,
quote_ident(concat('TRI_ConstraintTrigger_c_', constraint_name, '_ins')),
quote_ident(from_table),
quote_nullable(from_table),
quote_nullable(from_column),
quote_nullable(from_range_column),
quote_nullable(to_table),
quote_nullable(to_column),
quote_nullable(to_range_column));
-- Check the FK when it's UPDATEd:
EXECUTE format($q$
CREATE CONSTRAINT TRIGGER %1$s
AFTER UPDATE ON %2$s
DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE TRI_FKey_check_upd(%3$s, %4$s, %5$s, %6$s, %7$s, %8$s)
$q$,
quote_ident(concat('TRI_ConstraintTrigger_c_', constraint_name, '_upd')),
quote_ident(from_table),
quote_nullable(from_table),
quote_nullable(from_column),
quote_nullable(from_range_column),
quote_nullable(to_table),
quote_nullable(to_column),
quote_nullable(to_range_column));
-- Validate all the existing rows.
-- The built-in FK triggers do this one-by-one instead of with a big query,
-- which seems less efficient, but it does have better code reuse.
-- I'm following their lead here:
FOR fk_val, from_range IN EXECUTE format(
'SELECT %2$s, %3$s FROM %1$s',
quote_ident(from_table), quote_ident(from_column), quote_ident(from_range_column)
) LOOP
PERFORM TRI_FKey_check(
from_table, from_column, from_range_column,
to_table, to_column, to_range_column,
fk_val, from_range, false);
END LOOP;
-- TODO: Keep it in a catalog?
END;
$$
LANGUAGE plpgsql;
-- TODO: install a listener for `alter table` and `drop table` commands.
CREATE OR REPLACE FUNCTION drop_temporal_foreign_key(
constraint_name TEXT,
from_table TEXT,
to_table TEXT
)
RETURNS VOID
AS
$$
DECLARE
BEGIN
EXECUTE format(
'DROP TRIGGER %2$s ON %1$s',
quote_ident(to_table),
quote_ident(concat('TRI_ConstraintTrigger_a_', constraint_name, '_del')));
EXECUTE format(
'DROP TRIGGER %2$s ON %1$s',
quote_ident(to_table),
quote_ident(concat('TRI_ConstraintTrigger_a_', constraint_name, '_upd')));
EXECUTE format(
'DROP TRIGGER %2$s ON %1$s',
quote_ident(from_table),
quote_ident(concat('TRI_ConstraintTrigger_c_', constraint_name, '_ins')));
EXECUTE format(
'DROP TRIGGER %2$s ON %1$s',
quote_ident(from_table),
quote_ident(concat('TRI_ConstraintTrigger_c_', constraint_name, '_upd')));
END;
$$
LANGUAGE plpgsql;