| 1 |
-- suppress CONTEXT so that table OIDs aren't in output |
| 2 |
\set VERBOSITY terse |
| 3 |
|
| 4 |
-- |
| 5 |
-- Create the tables used in the test queries |
| 6 |
-- |
| 7 |
-- T_pkey1 is the primary key table for T_dta1. Entries from T_pkey1 |
| 8 |
-- Cannot be changed or deleted if they are referenced from T_dta1. |
| 9 |
-- |
| 10 |
-- T_pkey2 is the primary key table for T_dta2. If the key values in |
| 11 |
-- T_pkey2 are changed, the references in T_dta2 follow. If entries |
| 12 |
-- are deleted, the referencing entries from T_dta2 are deleted too. |
| 13 |
-- The values for field key2 in T_pkey2 are silently converted to |
| 14 |
-- upper case on insert/update. |
| 15 |
-- |
| 16 |
create table T_pkey1 ( |
| 17 |
key1 int4, |
| 18 |
key2 char(20), |
| 19 |
txt char(40) |
| 20 |
); |
| 21 |
|
| 22 |
create table T_pkey2 ( |
| 23 |
key1 int4, |
| 24 |
key2 char(20), |
| 25 |
txt char(40) |
| 26 |
); |
| 27 |
|
| 28 |
create table T_dta1 ( |
| 29 |
tkey char(10), |
| 30 |
ref1 int4, |
| 31 |
ref2 char(20) |
| 32 |
); |
| 33 |
|
| 34 |
create table T_dta2 ( |
| 35 |
tkey char(10), |
| 36 |
ref1 int4, |
| 37 |
ref2 char(20) |
| 38 |
); |
| 39 |
|
| 40 |
|
| 41 |
-- |
| 42 |
-- Function to check key existence in T_pkey1 |
| 43 |
-- |
| 44 |
create function check_pkey1_exists(int4, bpchar) returns bool as E' |
| 45 |
if {![info exists GD]} { |
| 46 |
set GD(plan) [spi_prepare \\ |
| 47 |
"select 1 from T_pkey1 \\ |
| 48 |
where key1 = \\$1 and key2 = \\$2" \\ |
| 49 |
{int4 bpchar}] |
| 50 |
} |
| 51 |
|
| 52 |
set n [spi_execp -count 1 $GD(plan) [list $1 $2]] |
| 53 |
|
| 54 |
if {$n > 0} { |
| 55 |
return "t" |
| 56 |
} |
| 57 |
return "f" |
| 58 |
' language pltcl; |
| 59 |
|
| 60 |
|
| 61 |
-- dump trigger data |
| 62 |
|
| 63 |
CREATE TABLE trigger_test ( |
| 64 |
i int, |
| 65 |
v text, |
| 66 |
dropme text, |
| 67 |
test_skip boolean DEFAULT false, |
| 68 |
test_return_null boolean DEFAULT false, |
| 69 |
test_argisnull boolean DEFAULT false |
| 70 |
); |
| 71 |
-- Make certain dropped attributes are handled correctly |
| 72 |
ALTER TABLE trigger_test DROP dropme; |
| 73 |
|
| 74 |
CREATE TABLE trigger_test_generated ( |
| 75 |
i int, |
| 76 |
j int GENERATED ALWAYS AS (i * 2) STORED, |
| 77 |
k int GENERATED ALWAYS AS (i * 3) VIRTUAL |
| 78 |
); |
| 79 |
|
| 80 |
CREATE VIEW trigger_test_view AS SELECT i, v FROM trigger_test; |
| 81 |
|
| 82 |
CREATE FUNCTION trigger_data() returns trigger language pltcl as $_$ |
| 83 |
if {$TG_table_name eq "trigger_test" && $TG_level eq "ROW" && $TG_op ne "DELETE"} { |
| 84 |
# Special case tests |
| 85 |
if {$NEW(test_return_null) eq "t" } { |
| 86 |
return_null |
| 87 |
} |
| 88 |
if {$NEW(test_argisnull) eq "t" } { |
| 89 |
set should_error [argisnull 1] |
| 90 |
} |
| 91 |
if {$NEW(test_skip) eq "t" } { |
| 92 |
elog NOTICE "SKIPPING OPERATION $TG_op" |
| 93 |
return SKIP |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if { [info exists TG_relid] } { |
| 98 |
set TG_relid "bogus:12345" |
| 99 |
} |
| 100 |
|
| 101 |
set dnames [info locals {[a-zA-Z]*} ] |
| 102 |
|
| 103 |
foreach key [lsort $dnames] { |
| 104 |
|
| 105 |
if { [array exists $key] } { |
| 106 |
set str "{" |
| 107 |
foreach akey [lsort [ array names $key ] ] { |
| 108 |
if {[string length $str] > 1} { set str "$str, " } |
| 109 |
set cmd "($akey)" |
| 110 |
set cmd "set val \$$key$cmd" |
| 111 |
eval $cmd |
| 112 |
set str "$str$akey: $val" |
| 113 |
} |
| 114 |
set str "$str}" |
| 115 |
elog NOTICE "$key: $str" |
| 116 |
} else { |
| 117 |
set val [eval list "\$$key" ] |
| 118 |
elog NOTICE "$key: $val" |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
return OK |
| 124 |
|
| 125 |
$_$; |
| 126 |
|
| 127 |
CREATE TRIGGER show_trigger_data_trig |
| 128 |
BEFORE INSERT OR UPDATE OR DELETE ON trigger_test |
| 129 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo'); |
| 130 |
CREATE TRIGGER statement_trigger |
| 131 |
BEFORE INSERT OR UPDATE OR DELETE OR TRUNCATE ON trigger_test |
| 132 |
FOR EACH STATEMENT EXECUTE PROCEDURE trigger_data(42,'statement trigger'); |
| 133 |
|
| 134 |
CREATE TRIGGER show_trigger_data_trig_before |
| 135 |
BEFORE INSERT OR UPDATE OR DELETE ON trigger_test_generated |
| 136 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(); |
| 137 |
CREATE TRIGGER show_trigger_data_trig_after |
| 138 |
AFTER INSERT OR UPDATE OR DELETE ON trigger_test_generated |
| 139 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(); |
| 140 |
|
| 141 |
CREATE TRIGGER show_trigger_data_view_trig |
| 142 |
INSTEAD OF INSERT OR UPDATE OR DELETE ON trigger_test_view |
| 143 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(24,'skidoo view'); |
| 144 |
|
| 145 |
-- |
| 146 |
-- Trigger function on every change to T_pkey1 |
| 147 |
-- |
| 148 |
create function trig_pkey1_before() returns trigger as E' |
| 149 |
# |
| 150 |
# Create prepared plans on the first call |
| 151 |
# |
| 152 |
if {![info exists GD]} { |
| 153 |
# |
| 154 |
# Plan to check for duplicate key in T_pkey1 |
| 155 |
# |
| 156 |
set GD(plan_pkey1) [spi_prepare \\ |
| 157 |
"select check_pkey1_exists(\\$1, \\$2) as ret" \\ |
| 158 |
{int4 bpchar}] |
| 159 |
# |
| 160 |
# Plan to check for references from T_dta1 |
| 161 |
# |
| 162 |
set GD(plan_dta1) [spi_prepare \\ |
| 163 |
"select 1 from T_dta1 \\ |
| 164 |
where ref1 = \\$1 and ref2 = \\$2" \\ |
| 165 |
{int4 bpchar}] |
| 166 |
} |
| 167 |
|
| 168 |
# |
| 169 |
# Initialize flags |
| 170 |
# |
| 171 |
set check_old_ref 0 |
| 172 |
set check_new_dup 0 |
| 173 |
|
| 174 |
switch $TG_op { |
| 175 |
INSERT { |
| 176 |
# |
| 177 |
# Must check for duplicate key on INSERT |
| 178 |
# |
| 179 |
set check_new_dup 1 |
| 180 |
} |
| 181 |
UPDATE { |
| 182 |
# |
| 183 |
# Must check for duplicate key on UPDATE only if |
| 184 |
# the key changes. In that case we must check for |
| 185 |
# references to OLD values too. |
| 186 |
# |
| 187 |
if {[string compare $NEW(key1) $OLD(key1)] != 0} { |
| 188 |
set check_old_ref 1 |
| 189 |
set check_new_dup 1 |
| 190 |
} |
| 191 |
if {[string compare $NEW(key2) $OLD(key2)] != 0} { |
| 192 |
set check_old_ref 1 |
| 193 |
set check_new_dup 1 |
| 194 |
} |
| 195 |
} |
| 196 |
DELETE { |
| 197 |
# |
| 198 |
# Must only check for references to OLD on DELETE |
| 199 |
# |
| 200 |
set check_old_ref 1 |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if {$check_new_dup} { |
| 205 |
# |
| 206 |
# Check for duplicate key |
| 207 |
# |
| 208 |
spi_execp -count 1 $GD(plan_pkey1) [list $NEW(key1) $NEW(key2)] |
| 209 |
if {$ret == "t"} { |
| 210 |
elog ERROR \\ |
| 211 |
"duplicate key ''$NEW(key1)'', ''$NEW(key2)'' for T_pkey1" |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if {$check_old_ref} { |
| 216 |
# |
| 217 |
# Check for references to OLD |
| 218 |
# |
| 219 |
set n [spi_execp -count 1 $GD(plan_dta1) [list $OLD(key1) $OLD(key2)]] |
| 220 |
if {$n > 0} { |
| 221 |
elog ERROR \\ |
| 222 |
"key ''$OLD(key1)'', ''$OLD(key2)'' referenced by T_dta1" |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
# |
| 227 |
# Anything is fine - let operation pass through |
| 228 |
# |
| 229 |
return OK |
| 230 |
' language pltcl; |
| 231 |
|
| 232 |
|
| 233 |
create trigger pkey1_before before insert or update or delete on T_pkey1 |
| 234 |
for each row execute procedure |
| 235 |
trig_pkey1_before(); |
| 236 |
|
| 237 |
|
| 238 |
-- |
| 239 |
-- Trigger function to check for duplicate keys in T_pkey2 |
| 240 |
-- and to force key2 to be upper case only without leading whitespaces |
| 241 |
-- |
| 242 |
create function trig_pkey2_before() returns trigger as E' |
| 243 |
# |
| 244 |
# Prepare plan on first call |
| 245 |
# |
| 246 |
if {![info exists GD]} { |
| 247 |
set GD(plan_pkey2) [spi_prepare \\ |
| 248 |
"select 1 from T_pkey2 \\ |
| 249 |
where key1 = \\$1 and key2 = \\$2" \\ |
| 250 |
{int4 bpchar}] |
| 251 |
} |
| 252 |
|
| 253 |
# |
| 254 |
# Convert key2 value |
| 255 |
# |
| 256 |
set NEW(key2) [string toupper [string trim $NEW(key2)]] |
| 257 |
|
| 258 |
# |
| 259 |
# Check for duplicate key |
| 260 |
# |
| 261 |
set n [spi_execp -count 1 $GD(plan_pkey2) [list $NEW(key1) $NEW(key2)]] |
| 262 |
if {$n > 0} { |
| 263 |
elog ERROR \\ |
| 264 |
"duplicate key ''$NEW(key1)'', ''$NEW(key2)'' for T_pkey2" |
| 265 |
} |
| 266 |
|
| 267 |
# |
| 268 |
# Return modified tuple in NEW |
| 269 |
# |
| 270 |
return [array get NEW] |
| 271 |
' language pltcl; |
| 272 |
|
| 273 |
|
| 274 |
create trigger pkey2_before before insert or update on T_pkey2 |
| 275 |
for each row execute procedure |
| 276 |
trig_pkey2_before(); |
| 277 |
|
| 278 |
|
| 279 |
-- |
| 280 |
-- Trigger function to force references from T_dta2 follow changes |
| 281 |
-- in T_pkey2 or be deleted too. This must be done AFTER the changes |
| 282 |
-- in T_pkey2 are done so the trigger for primkey check on T_dta2 |
| 283 |
-- fired on our updates will see the new key values in T_pkey2. |
| 284 |
-- |
| 285 |
create function trig_pkey2_after() returns trigger as E' |
| 286 |
# |
| 287 |
# Prepare plans on first call |
| 288 |
# |
| 289 |
if {![info exists GD]} { |
| 290 |
# |
| 291 |
# Plan to update references from T_dta2 |
| 292 |
# |
| 293 |
set GD(plan_dta2_upd) [spi_prepare \\ |
| 294 |
"update T_dta2 set ref1 = \\$3, ref2 = \\$4 \\ |
| 295 |
where ref1 = \\$1 and ref2 = \\$2" \\ |
| 296 |
{int4 bpchar int4 bpchar}] |
| 297 |
# |
| 298 |
# Plan to delete references from T_dta2 |
| 299 |
# |
| 300 |
set GD(plan_dta2_del) [spi_prepare \\ |
| 301 |
"delete from T_dta2 \\ |
| 302 |
where ref1 = \\$1 and ref2 = \\$2" \\ |
| 303 |
{int4 bpchar}] |
| 304 |
} |
| 305 |
|
| 306 |
# |
| 307 |
# Initialize flags |
| 308 |
# |
| 309 |
set old_ref_follow 0 |
| 310 |
set old_ref_delete 0 |
| 311 |
|
| 312 |
switch $TG_op { |
| 313 |
UPDATE { |
| 314 |
# |
| 315 |
# On update we must let old references follow |
| 316 |
# |
| 317 |
set NEW(key2) [string toupper $NEW(key2)] |
| 318 |
|
| 319 |
if {[string compare $NEW(key1) $OLD(key1)] != 0} { |
| 320 |
set old_ref_follow 1 |
| 321 |
} |
| 322 |
if {[string compare $NEW(key2) $OLD(key2)] != 0} { |
| 323 |
set old_ref_follow 1 |
| 324 |
} |
| 325 |
} |
| 326 |
DELETE { |
| 327 |
# |
| 328 |
# On delete we must delete references too |
| 329 |
# |
| 330 |
set old_ref_delete 1 |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if {$old_ref_follow} { |
| 335 |
# |
| 336 |
# Let old references follow and fire NOTICE message if |
| 337 |
# there where some |
| 338 |
# |
| 339 |
set n [spi_execp $GD(plan_dta2_upd) \\ |
| 340 |
[list $OLD(key1) $OLD(key2) $NEW(key1) $NEW(key2)]] |
| 341 |
if {$n > 0} { |
| 342 |
elog NOTICE \\ |
| 343 |
"updated $n entries in T_dta2 for new key in T_pkey2" |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if {$old_ref_delete} { |
| 348 |
# |
| 349 |
# delete references and fire NOTICE message if |
| 350 |
# there where some |
| 351 |
# |
| 352 |
set n [spi_execp $GD(plan_dta2_del) \\ |
| 353 |
[list $OLD(key1) $OLD(key2)]] |
| 354 |
if {$n > 0} { |
| 355 |
elog NOTICE \\ |
| 356 |
"deleted $n entries from T_dta2" |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
return OK |
| 361 |
' language pltcl; |
| 362 |
|
| 363 |
|
| 364 |
create trigger pkey2_after after update or delete on T_pkey2 |
| 365 |
for each row execute procedure |
| 366 |
trig_pkey2_after(); |
| 367 |
|
| 368 |
|
| 369 |
-- |
| 370 |
-- Generic trigger function to check references in T_dta1 and T_dta2 |
| 371 |
-- |
| 372 |
create function check_primkey() returns trigger as E' |
| 373 |
# |
| 374 |
# For every trigger/relation pair we create |
| 375 |
# a saved plan and hold them in GD |
| 376 |
# |
| 377 |
set plankey [list "plan" $TG_name $TG_relid] |
| 378 |
set planrel [list "relname" $TG_relid] |
| 379 |
|
| 380 |
# |
| 381 |
# Extract the pkey relation name |
| 382 |
# |
| 383 |
set keyidx [expr [llength $args] / 2] |
| 384 |
set keyrel [string tolower [lindex $args $keyidx]] |
| 385 |
|
| 386 |
if {![info exists GD($plankey)]} { |
| 387 |
# |
| 388 |
# We must prepare a new plan. Build up a query string |
| 389 |
# for the primary key check. |
| 390 |
# |
| 391 |
set keylist [lrange $args [expr $keyidx + 1] end] |
| 392 |
|
| 393 |
set query "select 1 from $keyrel" |
| 394 |
set qual " where" |
| 395 |
set typlist "" |
| 396 |
set idx 1 |
| 397 |
foreach key $keylist { |
| 398 |
set key [string tolower $key] |
| 399 |
# |
| 400 |
# Add the qual part to the query string |
| 401 |
# |
| 402 |
append query "$qual $key = \\$$idx" |
| 403 |
set qual " and" |
| 404 |
|
| 405 |
# |
| 406 |
# Lookup the fields type in pg_attribute |
| 407 |
# |
| 408 |
set n [spi_exec "select T.typname \\ |
| 409 |
from pg_catalog.pg_type T, pg_catalog.pg_attribute A, pg_catalog.pg_class C \\ |
| 410 |
where C.relname = ''[quote $keyrel]'' \\ |
| 411 |
and C.oid = A.attrelid \\ |
| 412 |
and A.attname = ''[quote $key]'' \\ |
| 413 |
and A.atttypid = T.oid"] |
| 414 |
if {$n != 1} { |
| 415 |
elog ERROR "table $keyrel doesn''t have a field named $key" |
| 416 |
} |
| 417 |
|
| 418 |
# |
| 419 |
# Append the fields type to the argument type list |
| 420 |
# |
| 421 |
lappend typlist $typname |
| 422 |
incr idx |
| 423 |
} |
| 424 |
|
| 425 |
# |
| 426 |
# Prepare the plan |
| 427 |
# |
| 428 |
set GD($plankey) [spi_prepare $query $typlist] |
| 429 |
|
| 430 |
# |
| 431 |
# Lookup and remember the table name for later error messages |
| 432 |
# |
| 433 |
spi_exec "select relname from pg_catalog.pg_class \\ |
| 434 |
where oid = ''$TG_relid''::oid" |
| 435 |
set GD($planrel) $relname |
| 436 |
} |
| 437 |
|
| 438 |
# |
| 439 |
# Build the argument list from the NEW row |
| 440 |
# |
| 441 |
incr keyidx -1 |
| 442 |
set arglist "" |
| 443 |
foreach arg [lrange $args 0 $keyidx] { |
| 444 |
lappend arglist $NEW($arg) |
| 445 |
} |
| 446 |
|
| 447 |
# |
| 448 |
# Check for the primary key |
| 449 |
# |
| 450 |
set n [spi_execp -count 1 $GD($plankey) $arglist] |
| 451 |
if {$n <= 0} { |
| 452 |
elog ERROR "key for $GD($planrel) not in $keyrel" |
| 453 |
} |
| 454 |
|
| 455 |
# |
| 456 |
# Anything is fine |
| 457 |
# |
| 458 |
return OK |
| 459 |
' language pltcl; |
| 460 |
|
| 461 |
|
| 462 |
create trigger dta1_before before insert or update on T_dta1 |
| 463 |
for each row execute procedure |
| 464 |
check_primkey('ref1', 'ref2', 'T_pkey1', 'key1', 'key2'); |
| 465 |
|
| 466 |
|
| 467 |
create trigger dta2_before before insert or update on T_dta2 |
| 468 |
for each row execute procedure |
| 469 |
check_primkey('ref1', 'ref2', 'T_pkey2', 'key1', 'key2'); |
| 470 |
|
| 471 |
|
| 472 |
insert into T_pkey1 values (1, 'key1-1', 'test key'); |
| 473 |
insert into T_pkey1 values (1, 'key1-2', 'test key'); |
| 474 |
insert into T_pkey1 values (1, 'key1-3', 'test key'); |
| 475 |
insert into T_pkey1 values (2, 'key2-1', 'test key'); |
| 476 |
insert into T_pkey1 values (2, 'key2-2', 'test key'); |
| 477 |
insert into T_pkey1 values (2, 'key2-3', 'test key'); |
| 478 |
|
| 479 |
insert into T_pkey2 values (1, 'key1-1', 'test key'); |
| 480 |
insert into T_pkey2 values (1, 'key1-2', 'test key'); |
| 481 |
insert into T_pkey2 values (1, 'key1-3', 'test key'); |
| 482 |
insert into T_pkey2 values (2, 'key2-1', 'test key'); |
| 483 |
insert into T_pkey2 values (2, 'key2-2', 'test key'); |
| 484 |
insert into T_pkey2 values (2, 'key2-3', 'test key'); |
| 485 |
|
| 486 |
select * from T_pkey1; |
| 487 |
|
| 488 |
-- key2 in T_pkey2 should have upper case only |
| 489 |
select * from T_pkey2; |
| 490 |
|
| 491 |
insert into T_pkey1 values (1, 'KEY1-3', 'should work'); |
| 492 |
|
| 493 |
-- Due to the upper case translation in trigger this must fail |
| 494 |
insert into T_pkey2 values (1, 'KEY1-3', 'should fail'); |
| 495 |
|
| 496 |
insert into T_dta1 values ('trec 1', 1, 'key1-1'); |
| 497 |
insert into T_dta1 values ('trec 2', 1, 'key1-2'); |
| 498 |
insert into T_dta1 values ('trec 3', 1, 'key1-3'); |
| 499 |
|
| 500 |
-- Must fail due to unknown key in T_pkey1 |
| 501 |
insert into T_dta1 values ('trec 4', 1, 'key1-4'); |
| 502 |
|
| 503 |
insert into T_dta2 values ('trec 1', 1, 'KEY1-1'); |
| 504 |
insert into T_dta2 values ('trec 2', 1, 'KEY1-2'); |
| 505 |
insert into T_dta2 values ('trec 3', 1, 'KEY1-3'); |
| 506 |
|
| 507 |
-- Must fail due to unknown key in T_pkey2 |
| 508 |
insert into T_dta2 values ('trec 4', 1, 'KEY1-4'); |
| 509 |
|
| 510 |
select * from T_dta1; |
| 511 |
|
| 512 |
select * from T_dta2; |
| 513 |
|
| 514 |
update T_pkey1 set key2 = 'key2-9' where key1 = 2 and key2 = 'key2-1'; |
| 515 |
update T_pkey1 set key2 = 'key1-9' where key1 = 1 and key2 = 'key1-1'; |
| 516 |
delete from T_pkey1 where key1 = 2 and key2 = 'key2-2'; |
| 517 |
delete from T_pkey1 where key1 = 1 and key2 = 'key1-2'; |
| 518 |
|
| 519 |
update T_pkey2 set key2 = 'KEY2-9' where key1 = 2 and key2 = 'KEY2-1'; |
| 520 |
update T_pkey2 set key2 = 'KEY1-9' where key1 = 1 and key2 = 'KEY1-1'; |
| 521 |
delete from T_pkey2 where key1 = 2 and key2 = 'KEY2-2'; |
| 522 |
delete from T_pkey2 where key1 = 1 and key2 = 'KEY1-2'; |
| 523 |
|
| 524 |
select * from T_pkey1; |
| 525 |
select * from T_pkey2; |
| 526 |
select * from T_dta1; |
| 527 |
select * from T_dta2; |
| 528 |
|
| 529 |
select tcl_avg(key1) from T_pkey1; |
| 530 |
select tcl_sum(key1) from T_pkey1; |
| 531 |
select tcl_avg(key1) from T_pkey2; |
| 532 |
select tcl_sum(key1) from T_pkey2; |
| 533 |
|
| 534 |
-- The following should return NULL instead of 0 |
| 535 |
select tcl_avg(key1) from T_pkey1 where key1 = 99; |
| 536 |
select tcl_sum(key1) from T_pkey1 where key1 = 99; |
| 537 |
|
| 538 |
select 1 @< 2; |
| 539 |
select 100 @< 4; |
| 540 |
|
| 541 |
select * from T_pkey1 order by key1 using @<, key2 collate "C"; |
| 542 |
select * from T_pkey2 order by key1 using @<, key2 collate "C"; |
| 543 |
|
| 544 |
-- show dump of trigger data |
| 545 |
insert into trigger_test values(1,'insert'); |
| 546 |
|
| 547 |
insert into trigger_test_generated (i) values (1); |
| 548 |
update trigger_test_generated set i = 11 where i = 1; |
| 549 |
delete from trigger_test_generated; |
| 550 |
|
| 551 |
insert into trigger_test_view values(2,'insert'); |
| 552 |
update trigger_test_view set v = 'update' where i=1; |
| 553 |
delete from trigger_test_view; |
| 554 |
|
| 555 |
update trigger_test set v = 'update', test_skip=true where i = 1; |
| 556 |
update trigger_test set v = 'update' where i = 1; |
| 557 |
delete from trigger_test; |
| 558 |
truncate trigger_test; |
| 559 |
|
| 560 |
DROP TRIGGER show_trigger_data_trig_before ON trigger_test_generated; |
| 561 |
DROP TRIGGER show_trigger_data_trig_after ON trigger_test_generated; |
| 562 |
|
| 563 |
-- should error |
| 564 |
insert into trigger_test(test_argisnull) values(true); |
| 565 |
|
| 566 |
-- should error |
| 567 |
insert into trigger_test(test_return_null) values(true); |
| 568 |
|
| 569 |
-- test transition table visibility |
| 570 |
create table transition_table_test (id int, name text); |
| 571 |
insert into transition_table_test values (1, 'a'); |
| 572 |
create function transition_table_test_f() returns trigger language pltcl as |
| 573 |
$$ |
| 574 |
spi_exec -array C "SELECT id, name FROM old_table" { |
| 575 |
elog INFO "old: $C(id) -> $C(name)" |
| 576 |
} |
| 577 |
spi_exec -array C "SELECT id, name FROM new_table" { |
| 578 |
elog INFO "new: $C(id) -> $C(name)" |
| 579 |
} |
| 580 |
return OK |
| 581 |
$$; |
| 582 |
CREATE TRIGGER a_t AFTER UPDATE ON transition_table_test |
| 583 |
REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table |
| 584 |
FOR EACH STATEMENT EXECUTE PROCEDURE transition_table_test_f(); |
| 585 |
update transition_table_test set name = 'b'; |
| 586 |
drop table transition_table_test; |
| 587 |
drop function transition_table_test_f(); |
| 588 |
|
| 589 |
-- dealing with generated columns |
| 590 |
|
| 591 |
CREATE FUNCTION generated_test_func1() RETURNS trigger |
| 592 |
LANGUAGE pltcl |
| 593 |
AS $$ |
| 594 |
# not allowed |
| 595 |
set NEW(j) 5 |
| 596 |
return [array get NEW] |
| 597 |
$$; |
| 598 |
|
| 599 |
CREATE TRIGGER generated_test_trigger1 BEFORE INSERT ON trigger_test_generated |
| 600 |
FOR EACH ROW EXECUTE PROCEDURE generated_test_func1(); |
| 601 |
|
| 602 |
TRUNCATE trigger_test_generated; |
| 603 |
INSERT INTO trigger_test_generated (i) VALUES (1); |
| 604 |
SELECT * FROM trigger_test_generated; |