| 1 |
-- suppress CONTEXT so that table OIDs aren't in output |
| 2 |
\set VERBOSITY terse |
| 3 |
-- |
| 4 |
-- Create the tables used in the test queries |
| 5 |
-- |
| 6 |
-- T_pkey1 is the primary key table for T_dta1. Entries from T_pkey1 |
| 7 |
-- Cannot be changed or deleted if they are referenced from T_dta1. |
| 8 |
-- |
| 9 |
-- T_pkey2 is the primary key table for T_dta2. If the key values in |
| 10 |
-- T_pkey2 are changed, the references in T_dta2 follow. If entries |
| 11 |
-- are deleted, the referencing entries from T_dta2 are deleted too. |
| 12 |
-- The values for field key2 in T_pkey2 are silently converted to |
| 13 |
-- upper case on insert/update. |
| 14 |
-- |
| 15 |
create table T_pkey1 ( |
| 16 |
key1 int4, |
| 17 |
key2 char(20), |
| 18 |
txt char(40) |
| 19 |
); |
| 20 |
create table T_pkey2 ( |
| 21 |
key1 int4, |
| 22 |
key2 char(20), |
| 23 |
txt char(40) |
| 24 |
); |
| 25 |
create table T_dta1 ( |
| 26 |
tkey char(10), |
| 27 |
ref1 int4, |
| 28 |
ref2 char(20) |
| 29 |
); |
| 30 |
create table T_dta2 ( |
| 31 |
tkey char(10), |
| 32 |
ref1 int4, |
| 33 |
ref2 char(20) |
| 34 |
); |
| 35 |
-- |
| 36 |
-- Function to check key existence in T_pkey1 |
| 37 |
-- |
| 38 |
create function check_pkey1_exists(int4, bpchar) returns bool as E' |
| 39 |
if {![info exists GD]} { |
| 40 |
set GD(plan) [spi_prepare \\ |
| 41 |
"select 1 from T_pkey1 \\ |
| 42 |
where key1 = \\$1 and key2 = \\$2" \\ |
| 43 |
{int4 bpchar}] |
| 44 |
} |
| 45 |
|
| 46 |
set n [spi_execp -count 1 $GD(plan) [list $1 $2]] |
| 47 |
|
| 48 |
if {$n > 0} { |
| 49 |
return "t" |
| 50 |
} |
| 51 |
return "f" |
| 52 |
' language pltcl; |
| 53 |
-- dump trigger data |
| 54 |
CREATE TABLE trigger_test ( |
| 55 |
i int, |
| 56 |
v text, |
| 57 |
dropme text, |
| 58 |
test_skip boolean DEFAULT false, |
| 59 |
test_return_null boolean DEFAULT false, |
| 60 |
test_argisnull boolean DEFAULT false |
| 61 |
); |
| 62 |
-- Make certain dropped attributes are handled correctly |
| 63 |
ALTER TABLE trigger_test DROP dropme; |
| 64 |
CREATE TABLE trigger_test_generated ( |
| 65 |
i int, |
| 66 |
j int GENERATED ALWAYS AS (i * 2) STORED, |
| 67 |
k int GENERATED ALWAYS AS (i * 3) VIRTUAL |
| 68 |
); |
| 69 |
CREATE VIEW trigger_test_view AS SELECT i, v FROM trigger_test; |
| 70 |
CREATE FUNCTION trigger_data() returns trigger language pltcl as $_$ |
| 71 |
if {$TG_table_name eq "trigger_test" && $TG_level eq "ROW" && $TG_op ne "DELETE"} { |
| 72 |
# Special case tests |
| 73 |
if {$NEW(test_return_null) eq "t" } { |
| 74 |
return_null |
| 75 |
} |
| 76 |
if {$NEW(test_argisnull) eq "t" } { |
| 77 |
set should_error [argisnull 1] |
| 78 |
} |
| 79 |
if {$NEW(test_skip) eq "t" } { |
| 80 |
elog NOTICE "SKIPPING OPERATION $TG_op" |
| 81 |
return SKIP |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if { [info exists TG_relid] } { |
| 86 |
set TG_relid "bogus:12345" |
| 87 |
} |
| 88 |
|
| 89 |
set dnames [info locals {[a-zA-Z]*} ] |
| 90 |
|
| 91 |
foreach key [lsort $dnames] { |
| 92 |
|
| 93 |
if { [array exists $key] } { |
| 94 |
set str "{" |
| 95 |
foreach akey [lsort [ array names $key ] ] { |
| 96 |
if {[string length $str] > 1} { set str "$str, " } |
| 97 |
set cmd "($akey)" |
| 98 |
set cmd "set val \$$key$cmd" |
| 99 |
eval $cmd |
| 100 |
set str "$str$akey: $val" |
| 101 |
} |
| 102 |
set str "$str}" |
| 103 |
elog NOTICE "$key: $str" |
| 104 |
} else { |
| 105 |
set val [eval list "\$$key" ] |
| 106 |
elog NOTICE "$key: $val" |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
return OK |
| 112 |
|
| 113 |
$_$; |
| 114 |
CREATE TRIGGER show_trigger_data_trig |
| 115 |
BEFORE INSERT OR UPDATE OR DELETE ON trigger_test |
| 116 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo'); |
| 117 |
CREATE TRIGGER statement_trigger |
| 118 |
BEFORE INSERT OR UPDATE OR DELETE OR TRUNCATE ON trigger_test |
| 119 |
FOR EACH STATEMENT EXECUTE PROCEDURE trigger_data(42,'statement trigger'); |
| 120 |
CREATE TRIGGER show_trigger_data_trig_before |
| 121 |
BEFORE INSERT OR UPDATE OR DELETE ON trigger_test_generated |
| 122 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(); |
| 123 |
CREATE TRIGGER show_trigger_data_trig_after |
| 124 |
AFTER INSERT OR UPDATE OR DELETE ON trigger_test_generated |
| 125 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(); |
| 126 |
CREATE TRIGGER show_trigger_data_view_trig |
| 127 |
INSTEAD OF INSERT OR UPDATE OR DELETE ON trigger_test_view |
| 128 |
FOR EACH ROW EXECUTE PROCEDURE trigger_data(24,'skidoo view'); |
| 129 |
-- |
| 130 |
-- Trigger function on every change to T_pkey1 |
| 131 |
-- |
| 132 |
create function trig_pkey1_before() returns trigger as E' |
| 133 |
# |
| 134 |
# Create prepared plans on the first call |
| 135 |
# |
| 136 |
if {![info exists GD]} { |
| 137 |
# |
| 138 |
# Plan to check for duplicate key in T_pkey1 |
| 139 |
# |
| 140 |
set GD(plan_pkey1) [spi_prepare \\ |
| 141 |
"select check_pkey1_exists(\\$1, \\$2) as ret" \\ |
| 142 |
{int4 bpchar}] |
| 143 |
# |
| 144 |
# Plan to check for references from T_dta1 |
| 145 |
# |
| 146 |
set GD(plan_dta1) [spi_prepare \\ |
| 147 |
"select 1 from T_dta1 \\ |
| 148 |
where ref1 = \\$1 and ref2 = \\$2" \\ |
| 149 |
{int4 bpchar}] |
| 150 |
} |
| 151 |
|
| 152 |
# |
| 153 |
# Initialize flags |
| 154 |
# |
| 155 |
set check_old_ref 0 |
| 156 |
set check_new_dup 0 |
| 157 |
|
| 158 |
switch $TG_op { |
| 159 |
INSERT { |
| 160 |
# |
| 161 |
# Must check for duplicate key on INSERT |
| 162 |
# |
| 163 |
set check_new_dup 1 |
| 164 |
} |
| 165 |
UPDATE { |
| 166 |
# |
| 167 |
# Must check for duplicate key on UPDATE only if |
| 168 |
# the key changes. In that case we must check for |
| 169 |
# references to OLD values too. |
| 170 |
# |
| 171 |
if {[string compare $NEW(key1) $OLD(key1)] != 0} { |
| 172 |
set check_old_ref 1 |
| 173 |
set check_new_dup 1 |
| 174 |
} |
| 175 |
if {[string compare $NEW(key2) $OLD(key2)] != 0} { |
| 176 |
set check_old_ref 1 |
| 177 |
set check_new_dup 1 |
| 178 |
} |
| 179 |
} |
| 180 |
DELETE { |
| 181 |
# |
| 182 |
# Must only check for references to OLD on DELETE |
| 183 |
# |
| 184 |
set check_old_ref 1 |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if {$check_new_dup} { |
| 189 |
# |
| 190 |
# Check for duplicate key |
| 191 |
# |
| 192 |
spi_execp -count 1 $GD(plan_pkey1) [list $NEW(key1) $NEW(key2)] |
| 193 |
if {$ret == "t"} { |
| 194 |
elog ERROR \\ |
| 195 |
"duplicate key ''$NEW(key1)'', ''$NEW(key2)'' for T_pkey1" |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if {$check_old_ref} { |
| 200 |
# |
| 201 |
# Check for references to OLD |
| 202 |
# |
| 203 |
set n [spi_execp -count 1 $GD(plan_dta1) [list $OLD(key1) $OLD(key2)]] |
| 204 |
if {$n > 0} { |
| 205 |
elog ERROR \\ |
| 206 |
"key ''$OLD(key1)'', ''$OLD(key2)'' referenced by T_dta1" |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
# |
| 211 |
# Anything is fine - let operation pass through |
| 212 |
# |
| 213 |
return OK |
| 214 |
' language pltcl; |
| 215 |
create trigger pkey1_before before insert or update or delete on T_pkey1 |
| 216 |
for each row execute procedure |
| 217 |
trig_pkey1_before(); |
| 218 |
-- |
| 219 |
-- Trigger function to check for duplicate keys in T_pkey2 |
| 220 |
-- and to force key2 to be upper case only without leading whitespaces |
| 221 |
-- |
| 222 |
create function trig_pkey2_before() returns trigger as E' |
| 223 |
# |
| 224 |
# Prepare plan on first call |
| 225 |
# |
| 226 |
if {![info exists GD]} { |
| 227 |
set GD(plan_pkey2) [spi_prepare \\ |
| 228 |
"select 1 from T_pkey2 \\ |
| 229 |
where key1 = \\$1 and key2 = \\$2" \\ |
| 230 |
{int4 bpchar}] |
| 231 |
} |
| 232 |
|
| 233 |
# |
| 234 |
# Convert key2 value |
| 235 |
# |
| 236 |
set NEW(key2) [string toupper [string trim $NEW(key2)]] |
| 237 |
|
| 238 |
# |
| 239 |
# Check for duplicate key |
| 240 |
# |
| 241 |
set n [spi_execp -count 1 $GD(plan_pkey2) [list $NEW(key1) $NEW(key2)]] |
| 242 |
if {$n > 0} { |
| 243 |
elog ERROR \\ |
| 244 |
"duplicate key ''$NEW(key1)'', ''$NEW(key2)'' for T_pkey2" |
| 245 |
} |
| 246 |
|
| 247 |
# |
| 248 |
# Return modified tuple in NEW |
| 249 |
# |
| 250 |
return [array get NEW] |
| 251 |
' language pltcl; |
| 252 |
create trigger pkey2_before before insert or update on T_pkey2 |
| 253 |
for each row execute procedure |
| 254 |
trig_pkey2_before(); |
| 255 |
-- |
| 256 |
-- Trigger function to force references from T_dta2 follow changes |
| 257 |
-- in T_pkey2 or be deleted too. This must be done AFTER the changes |
| 258 |
-- in T_pkey2 are done so the trigger for primkey check on T_dta2 |
| 259 |
-- fired on our updates will see the new key values in T_pkey2. |
| 260 |
-- |
| 261 |
create function trig_pkey2_after() returns trigger as E' |
| 262 |
# |
| 263 |
# Prepare plans on first call |
| 264 |
# |
| 265 |
if {![info exists GD]} { |
| 266 |
# |
| 267 |
# Plan to update references from T_dta2 |
| 268 |
# |
| 269 |
set GD(plan_dta2_upd) [spi_prepare \\ |
| 270 |
"update T_dta2 set ref1 = \\$3, ref2 = \\$4 \\ |
| 271 |
where ref1 = \\$1 and ref2 = \\$2" \\ |
| 272 |
{int4 bpchar int4 bpchar}] |
| 273 |
# |
| 274 |
# Plan to delete references from T_dta2 |
| 275 |
# |
| 276 |
set GD(plan_dta2_del) [spi_prepare \\ |
| 277 |
"delete from T_dta2 \\ |
| 278 |
where ref1 = \\$1 and ref2 = \\$2" \\ |
| 279 |
{int4 bpchar}] |
| 280 |
} |
| 281 |
|
| 282 |
# |
| 283 |
# Initialize flags |
| 284 |
# |
| 285 |
set old_ref_follow 0 |
| 286 |
set old_ref_delete 0 |
| 287 |
|
| 288 |
switch $TG_op { |
| 289 |
UPDATE { |
| 290 |
# |
| 291 |
# On update we must let old references follow |
| 292 |
# |
| 293 |
set NEW(key2) [string toupper $NEW(key2)] |
| 294 |
|
| 295 |
if {[string compare $NEW(key1) $OLD(key1)] != 0} { |
| 296 |
set old_ref_follow 1 |
| 297 |
} |
| 298 |
if {[string compare $NEW(key2) $OLD(key2)] != 0} { |
| 299 |
set old_ref_follow 1 |
| 300 |
} |
| 301 |
} |
| 302 |
DELETE { |
| 303 |
# |
| 304 |
# On delete we must delete references too |
| 305 |
# |
| 306 |
set old_ref_delete 1 |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if {$old_ref_follow} { |
| 311 |
# |
| 312 |
# Let old references follow and fire NOTICE message if |
| 313 |
# there where some |
| 314 |
# |
| 315 |
set n [spi_execp $GD(plan_dta2_upd) \\ |
| 316 |
[list $OLD(key1) $OLD(key2) $NEW(key1) $NEW(key2)]] |
| 317 |
if {$n > 0} { |
| 318 |
elog NOTICE \\ |
| 319 |
"updated $n entries in T_dta2 for new key in T_pkey2" |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
if {$old_ref_delete} { |
| 324 |
# |
| 325 |
# delete references and fire NOTICE message if |
| 326 |
# there where some |
| 327 |
# |
| 328 |
set n [spi_execp $GD(plan_dta2_del) \\ |
| 329 |
[list $OLD(key1) $OLD(key2)]] |
| 330 |
if {$n > 0} { |
| 331 |
elog NOTICE \\ |
| 332 |
"deleted $n entries from T_dta2" |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return OK |
| 337 |
' language pltcl; |
| 338 |
create trigger pkey2_after after update or delete on T_pkey2 |
| 339 |
for each row execute procedure |
| 340 |
trig_pkey2_after(); |
| 341 |
-- |
| 342 |
-- Generic trigger function to check references in T_dta1 and T_dta2 |
| 343 |
-- |
| 344 |
create function check_primkey() returns trigger as E' |
| 345 |
# |
| 346 |
# For every trigger/relation pair we create |
| 347 |
# a saved plan and hold them in GD |
| 348 |
# |
| 349 |
set plankey [list "plan" $TG_name $TG_relid] |
| 350 |
set planrel [list "relname" $TG_relid] |
| 351 |
|
| 352 |
# |
| 353 |
# Extract the pkey relation name |
| 354 |
# |
| 355 |
set keyidx [expr [llength $args] / 2] |
| 356 |
set keyrel [string tolower [lindex $args $keyidx]] |
| 357 |
|
| 358 |
if {![info exists GD($plankey)]} { |
| 359 |
# |
| 360 |
# We must prepare a new plan. Build up a query string |
| 361 |
# for the primary key check. |
| 362 |
# |
| 363 |
set keylist [lrange $args [expr $keyidx + 1] end] |
| 364 |
|
| 365 |
set query "select 1 from $keyrel" |
| 366 |
set qual " where" |
| 367 |
set typlist "" |
| 368 |
set idx 1 |
| 369 |
foreach key $keylist { |
| 370 |
set key [string tolower $key] |
| 371 |
# |
| 372 |
# Add the qual part to the query string |
| 373 |
# |
| 374 |
append query "$qual $key = \\$$idx" |
| 375 |
set qual " and" |
| 376 |
|
| 377 |
# |
| 378 |
# Lookup the fields type in pg_attribute |
| 379 |
# |
| 380 |
set n [spi_exec "select T.typname \\ |
| 381 |
from pg_catalog.pg_type T, pg_catalog.pg_attribute A, pg_catalog.pg_class C \\ |
| 382 |
where C.relname = ''[quote $keyrel]'' \\ |
| 383 |
and C.oid = A.attrelid \\ |
| 384 |
and A.attname = ''[quote $key]'' \\ |
| 385 |
and A.atttypid = T.oid"] |
| 386 |
if {$n != 1} { |
| 387 |
elog ERROR "table $keyrel doesn''t have a field named $key" |
| 388 |
} |
| 389 |
|
| 390 |
# |
| 391 |
# Append the fields type to the argument type list |
| 392 |
# |
| 393 |
lappend typlist $typname |
| 394 |
incr idx |
| 395 |
} |
| 396 |
|
| 397 |
# |
| 398 |
# Prepare the plan |
| 399 |
# |
| 400 |
set GD($plankey) [spi_prepare $query $typlist] |
| 401 |
|
| 402 |
# |
| 403 |
# Lookup and remember the table name for later error messages |
| 404 |
# |
| 405 |
spi_exec "select relname from pg_catalog.pg_class \\ |
| 406 |
where oid = ''$TG_relid''::oid" |
| 407 |
set GD($planrel) $relname |
| 408 |
} |
| 409 |
|
| 410 |
# |
| 411 |
# Build the argument list from the NEW row |
| 412 |
# |
| 413 |
incr keyidx -1 |
| 414 |
set arglist "" |
| 415 |
foreach arg [lrange $args 0 $keyidx] { |
| 416 |
lappend arglist $NEW($arg) |
| 417 |
} |
| 418 |
|
| 419 |
# |
| 420 |
# Check for the primary key |
| 421 |
# |
| 422 |
set n [spi_execp -count 1 $GD($plankey) $arglist] |
| 423 |
if {$n <= 0} { |
| 424 |
elog ERROR "key for $GD($planrel) not in $keyrel" |
| 425 |
} |
| 426 |
|
| 427 |
# |
| 428 |
# Anything is fine |
| 429 |
# |
| 430 |
return OK |
| 431 |
' language pltcl; |
| 432 |
create trigger dta1_before before insert or update on T_dta1 |
| 433 |
for each row execute procedure |
| 434 |
check_primkey('ref1', 'ref2', 'T_pkey1', 'key1', 'key2'); |
| 435 |
create trigger dta2_before before insert or update on T_dta2 |
| 436 |
for each row execute procedure |
| 437 |
check_primkey('ref1', 'ref2', 'T_pkey2', 'key1', 'key2'); |
| 438 |
insert into T_pkey1 values (1, 'key1-1', 'test key'); |
| 439 |
insert into T_pkey1 values (1, 'key1-2', 'test key'); |
| 440 |
insert into T_pkey1 values (1, 'key1-3', 'test key'); |
| 441 |
insert into T_pkey1 values (2, 'key2-1', 'test key'); |
| 442 |
insert into T_pkey1 values (2, 'key2-2', 'test key'); |
| 443 |
insert into T_pkey1 values (2, 'key2-3', 'test key'); |
| 444 |
insert into T_pkey2 values (1, 'key1-1', 'test key'); |
| 445 |
insert into T_pkey2 values (1, 'key1-2', 'test key'); |
| 446 |
insert into T_pkey2 values (1, 'key1-3', 'test key'); |
| 447 |
insert into T_pkey2 values (2, 'key2-1', 'test key'); |
| 448 |
insert into T_pkey2 values (2, 'key2-2', 'test key'); |
| 449 |
insert into T_pkey2 values (2, 'key2-3', 'test key'); |
| 450 |
select * from T_pkey1; |
| 451 |
key1 | key2 | txt |
| 452 |
------+----------------------+------------------------------------------ |
| 453 |
1 | key1-1 | test key |
| 454 |
1 | key1-2 | test key |
| 455 |
1 | key1-3 | test key |
| 456 |
2 | key2-1 | test key |
| 457 |
2 | key2-2 | test key |
| 458 |
2 | key2-3 | test key |
| 459 |
(6 rows) |
| 460 |
|
| 461 |
-- key2 in T_pkey2 should have upper case only |
| 462 |
select * from T_pkey2; |
| 463 |
key1 | key2 | txt |
| 464 |
------+----------------------+------------------------------------------ |
| 465 |
1 | KEY1-1 | test key |
| 466 |
1 | KEY1-2 | test key |
| 467 |
1 | KEY1-3 | test key |
| 468 |
2 | KEY2-1 | test key |
| 469 |
2 | KEY2-2 | test key |
| 470 |
2 | KEY2-3 | test key |
| 471 |
(6 rows) |
| 472 |
|
| 473 |
insert into T_pkey1 values (1, 'KEY1-3', 'should work'); |
| 474 |
-- Due to the upper case translation in trigger this must fail |
| 475 |
insert into T_pkey2 values (1, 'KEY1-3', 'should fail'); |
| 476 |
ERROR: duplicate key '1', 'KEY1-3' for T_pkey2 |
| 477 |
insert into T_dta1 values ('trec 1', 1, 'key1-1'); |
| 478 |
insert into T_dta1 values ('trec 2', 1, 'key1-2'); |
| 479 |
insert into T_dta1 values ('trec 3', 1, 'key1-3'); |
| 480 |
-- Must fail due to unknown key in T_pkey1 |
| 481 |
insert into T_dta1 values ('trec 4', 1, 'key1-4'); |
| 482 |
ERROR: key for t_dta1 not in t_pkey1 |
| 483 |
insert into T_dta2 values ('trec 1', 1, 'KEY1-1'); |
| 484 |
insert into T_dta2 values ('trec 2', 1, 'KEY1-2'); |
| 485 |
insert into T_dta2 values ('trec 3', 1, 'KEY1-3'); |
| 486 |
-- Must fail due to unknown key in T_pkey2 |
| 487 |
insert into T_dta2 values ('trec 4', 1, 'KEY1-4'); |
| 488 |
ERROR: key for t_dta2 not in t_pkey2 |
| 489 |
select * from T_dta1; |
| 490 |
tkey | ref1 | ref2 |
| 491 |
------------+------+---------------------- |
| 492 |
trec 1 | 1 | key1-1 |
| 493 |
trec 2 | 1 | key1-2 |
| 494 |
trec 3 | 1 | key1-3 |
| 495 |
(3 rows) |
| 496 |
|
| 497 |
select * from T_dta2; |
| 498 |
tkey | ref1 | ref2 |
| 499 |
------------+------+---------------------- |
| 500 |
trec 1 | 1 | KEY1-1 |
| 501 |
trec 2 | 1 | KEY1-2 |
| 502 |
trec 3 | 1 | KEY1-3 |
| 503 |
(3 rows) |
| 504 |
|
| 505 |
update T_pkey1 set key2 = 'key2-9' where key1 = 2 and key2 = 'key2-1'; |
| 506 |
update T_pkey1 set key2 = 'key1-9' where key1 = 1 and key2 = 'key1-1'; |
| 507 |
ERROR: key '1', 'key1-1 ' referenced by T_dta1 |
| 508 |
delete from T_pkey1 where key1 = 2 and key2 = 'key2-2'; |
| 509 |
delete from T_pkey1 where key1 = 1 and key2 = 'key1-2'; |
| 510 |
ERROR: key '1', 'key1-2 ' referenced by T_dta1 |
| 511 |
update T_pkey2 set key2 = 'KEY2-9' where key1 = 2 and key2 = 'KEY2-1'; |
| 512 |
update T_pkey2 set key2 = 'KEY1-9' where key1 = 1 and key2 = 'KEY1-1'; |
| 513 |
NOTICE: updated 1 entries in T_dta2 for new key in T_pkey2 |
| 514 |
delete from T_pkey2 where key1 = 2 and key2 = 'KEY2-2'; |
| 515 |
delete from T_pkey2 where key1 = 1 and key2 = 'KEY1-2'; |
| 516 |
NOTICE: deleted 1 entries from T_dta2 |
| 517 |
select * from T_pkey1; |
| 518 |
key1 | key2 | txt |
| 519 |
------+----------------------+------------------------------------------ |
| 520 |
1 | key1-1 | test key |
| 521 |
1 | key1-2 | test key |
| 522 |
1 | key1-3 | test key |
| 523 |
2 | key2-3 | test key |
| 524 |
1 | KEY1-3 | should work |
| 525 |
2 | key2-9 | test key |
| 526 |
(6 rows) |
| 527 |
|
| 528 |
select * from T_pkey2; |
| 529 |
key1 | key2 | txt |
| 530 |
------+----------------------+------------------------------------------ |
| 531 |
1 | KEY1-3 | test key |
| 532 |
2 | KEY2-3 | test key |
| 533 |
2 | KEY2-9 | test key |
| 534 |
1 | KEY1-9 | test key |
| 535 |
(4 rows) |
| 536 |
|
| 537 |
select * from T_dta1; |
| 538 |
tkey | ref1 | ref2 |
| 539 |
------------+------+---------------------- |
| 540 |
trec 1 | 1 | key1-1 |
| 541 |
trec 2 | 1 | key1-2 |
| 542 |
trec 3 | 1 | key1-3 |
| 543 |
(3 rows) |
| 544 |
|
| 545 |
select * from T_dta2; |
| 546 |
tkey | ref1 | ref2 |
| 547 |
------------+------+---------------------- |
| 548 |
trec 3 | 1 | KEY1-3 |
| 549 |
trec 1 | 1 | KEY1-9 |
| 550 |
(2 rows) |
| 551 |
|
| 552 |
select tcl_avg(key1) from T_pkey1; |
| 553 |
tcl_avg |
| 554 |
--------- |
| 555 |
1 |
| 556 |
(1 row) |
| 557 |
|
| 558 |
select tcl_sum(key1) from T_pkey1; |
| 559 |
tcl_sum |
| 560 |
--------- |
| 561 |
8 |
| 562 |
(1 row) |
| 563 |
|
| 564 |
select tcl_avg(key1) from T_pkey2; |
| 565 |
tcl_avg |
| 566 |
--------- |
| 567 |
1 |
| 568 |
(1 row) |
| 569 |
|
| 570 |
select tcl_sum(key1) from T_pkey2; |
| 571 |
tcl_sum |
| 572 |
--------- |
| 573 |
6 |
| 574 |
(1 row) |
| 575 |
|
| 576 |
-- The following should return NULL instead of 0 |
| 577 |
select tcl_avg(key1) from T_pkey1 where key1 = 99; |
| 578 |
tcl_avg |
| 579 |
--------- |
| 580 |
|
| 581 |
(1 row) |
| 582 |
|
| 583 |
select tcl_sum(key1) from T_pkey1 where key1 = 99; |
| 584 |
tcl_sum |
| 585 |
--------- |
| 586 |
0 |
| 587 |
(1 row) |
| 588 |
|
| 589 |
select 1 @< 2; |
| 590 |
?column? |
| 591 |
---------- |
| 592 |
t |
| 593 |
(1 row) |
| 594 |
|
| 595 |
select 100 @< 4; |
| 596 |
?column? |
| 597 |
---------- |
| 598 |
f |
| 599 |
(1 row) |
| 600 |
|
| 601 |
select * from T_pkey1 order by key1 using @<, key2 collate "C"; |
| 602 |
key1 | key2 | txt |
| 603 |
------+----------------------+------------------------------------------ |
| 604 |
1 | KEY1-3 | should work |
| 605 |
1 | key1-1 | test key |
| 606 |
1 | key1-2 | test key |
| 607 |
1 | key1-3 | test key |
| 608 |
2 | key2-3 | test key |
| 609 |
2 | key2-9 | test key |
| 610 |
(6 rows) |
| 611 |
|
| 612 |
select * from T_pkey2 order by key1 using @<, key2 collate "C"; |
| 613 |
key1 | key2 | txt |
| 614 |
------+----------------------+------------------------------------------ |
| 615 |
1 | KEY1-3 | test key |
| 616 |
1 | KEY1-9 | test key |
| 617 |
2 | KEY2-3 | test key |
| 618 |
2 | KEY2-9 | test key |
| 619 |
(4 rows) |
| 620 |
|
| 621 |
-- show dump of trigger data |
| 622 |
insert into trigger_test values(1,'insert'); |
| 623 |
NOTICE: NEW: {} |
| 624 |
NOTICE: OLD: {} |
| 625 |
NOTICE: TG_level: STATEMENT |
| 626 |
NOTICE: TG_name: statement_trigger |
| 627 |
NOTICE: TG_op: INSERT |
| 628 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 629 |
NOTICE: TG_relid: bogus:12345 |
| 630 |
NOTICE: TG_table_name: trigger_test |
| 631 |
NOTICE: TG_table_schema: public |
| 632 |
NOTICE: TG_when: BEFORE |
| 633 |
NOTICE: args: {42 {statement trigger}} |
| 634 |
NOTICE: NEW: {i: 1, test_argisnull: f, test_return_null: f, test_skip: f, v: insert} |
| 635 |
NOTICE: OLD: {} |
| 636 |
NOTICE: TG_level: ROW |
| 637 |
NOTICE: TG_name: show_trigger_data_trig |
| 638 |
NOTICE: TG_op: INSERT |
| 639 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 640 |
NOTICE: TG_relid: bogus:12345 |
| 641 |
NOTICE: TG_table_name: trigger_test |
| 642 |
NOTICE: TG_table_schema: public |
| 643 |
NOTICE: TG_when: BEFORE |
| 644 |
NOTICE: args: {23 skidoo} |
| 645 |
insert into trigger_test_generated (i) values (1); |
| 646 |
NOTICE: NEW: {i: 1} |
| 647 |
NOTICE: OLD: {} |
| 648 |
NOTICE: TG_level: ROW |
| 649 |
NOTICE: TG_name: show_trigger_data_trig_before |
| 650 |
NOTICE: TG_op: INSERT |
| 651 |
NOTICE: TG_relatts: {{} i j k} |
| 652 |
NOTICE: TG_relid: bogus:12345 |
| 653 |
NOTICE: TG_table_name: trigger_test_generated |
| 654 |
NOTICE: TG_table_schema: public |
| 655 |
NOTICE: TG_when: BEFORE |
| 656 |
NOTICE: args: {} |
| 657 |
NOTICE: NEW: {i: 1, j: 2} |
| 658 |
NOTICE: OLD: {} |
| 659 |
NOTICE: TG_level: ROW |
| 660 |
NOTICE: TG_name: show_trigger_data_trig_after |
| 661 |
NOTICE: TG_op: INSERT |
| 662 |
NOTICE: TG_relatts: {{} i j k} |
| 663 |
NOTICE: TG_relid: bogus:12345 |
| 664 |
NOTICE: TG_table_name: trigger_test_generated |
| 665 |
NOTICE: TG_table_schema: public |
| 666 |
NOTICE: TG_when: AFTER |
| 667 |
NOTICE: args: {} |
| 668 |
update trigger_test_generated set i = 11 where i = 1; |
| 669 |
NOTICE: NEW: {i: 11} |
| 670 |
NOTICE: OLD: {i: 1, j: 2} |
| 671 |
NOTICE: TG_level: ROW |
| 672 |
NOTICE: TG_name: show_trigger_data_trig_before |
| 673 |
NOTICE: TG_op: UPDATE |
| 674 |
NOTICE: TG_relatts: {{} i j k} |
| 675 |
NOTICE: TG_relid: bogus:12345 |
| 676 |
NOTICE: TG_table_name: trigger_test_generated |
| 677 |
NOTICE: TG_table_schema: public |
| 678 |
NOTICE: TG_when: BEFORE |
| 679 |
NOTICE: args: {} |
| 680 |
NOTICE: NEW: {i: 11, j: 22} |
| 681 |
NOTICE: OLD: {i: 1, j: 2} |
| 682 |
NOTICE: TG_level: ROW |
| 683 |
NOTICE: TG_name: show_trigger_data_trig_after |
| 684 |
NOTICE: TG_op: UPDATE |
| 685 |
NOTICE: TG_relatts: {{} i j k} |
| 686 |
NOTICE: TG_relid: bogus:12345 |
| 687 |
NOTICE: TG_table_name: trigger_test_generated |
| 688 |
NOTICE: TG_table_schema: public |
| 689 |
NOTICE: TG_when: AFTER |
| 690 |
NOTICE: args: {} |
| 691 |
delete from trigger_test_generated; |
| 692 |
NOTICE: NEW: {} |
| 693 |
NOTICE: OLD: {i: 11, j: 22} |
| 694 |
NOTICE: TG_level: ROW |
| 695 |
NOTICE: TG_name: show_trigger_data_trig_before |
| 696 |
NOTICE: TG_op: DELETE |
| 697 |
NOTICE: TG_relatts: {{} i j k} |
| 698 |
NOTICE: TG_relid: bogus:12345 |
| 699 |
NOTICE: TG_table_name: trigger_test_generated |
| 700 |
NOTICE: TG_table_schema: public |
| 701 |
NOTICE: TG_when: BEFORE |
| 702 |
NOTICE: args: {} |
| 703 |
NOTICE: NEW: {} |
| 704 |
NOTICE: OLD: {i: 11, j: 22} |
| 705 |
NOTICE: TG_level: ROW |
| 706 |
NOTICE: TG_name: show_trigger_data_trig_after |
| 707 |
NOTICE: TG_op: DELETE |
| 708 |
NOTICE: TG_relatts: {{} i j k} |
| 709 |
NOTICE: TG_relid: bogus:12345 |
| 710 |
NOTICE: TG_table_name: trigger_test_generated |
| 711 |
NOTICE: TG_table_schema: public |
| 712 |
NOTICE: TG_when: AFTER |
| 713 |
NOTICE: args: {} |
| 714 |
insert into trigger_test_view values(2,'insert'); |
| 715 |
NOTICE: NEW: {i: 2, v: insert} |
| 716 |
NOTICE: OLD: {} |
| 717 |
NOTICE: TG_level: ROW |
| 718 |
NOTICE: TG_name: show_trigger_data_view_trig |
| 719 |
NOTICE: TG_op: INSERT |
| 720 |
NOTICE: TG_relatts: {{} i v} |
| 721 |
NOTICE: TG_relid: bogus:12345 |
| 722 |
NOTICE: TG_table_name: trigger_test_view |
| 723 |
NOTICE: TG_table_schema: public |
| 724 |
NOTICE: TG_when: {INSTEAD OF} |
| 725 |
NOTICE: args: {24 {skidoo view}} |
| 726 |
update trigger_test_view set v = 'update' where i=1; |
| 727 |
NOTICE: NEW: {i: 1, v: update} |
| 728 |
NOTICE: OLD: {i: 1, v: insert} |
| 729 |
NOTICE: TG_level: ROW |
| 730 |
NOTICE: TG_name: show_trigger_data_view_trig |
| 731 |
NOTICE: TG_op: UPDATE |
| 732 |
NOTICE: TG_relatts: {{} i v} |
| 733 |
NOTICE: TG_relid: bogus:12345 |
| 734 |
NOTICE: TG_table_name: trigger_test_view |
| 735 |
NOTICE: TG_table_schema: public |
| 736 |
NOTICE: TG_when: {INSTEAD OF} |
| 737 |
NOTICE: args: {24 {skidoo view}} |
| 738 |
delete from trigger_test_view; |
| 739 |
NOTICE: NEW: {} |
| 740 |
NOTICE: OLD: {i: 1, v: insert} |
| 741 |
NOTICE: TG_level: ROW |
| 742 |
NOTICE: TG_name: show_trigger_data_view_trig |
| 743 |
NOTICE: TG_op: DELETE |
| 744 |
NOTICE: TG_relatts: {{} i v} |
| 745 |
NOTICE: TG_relid: bogus:12345 |
| 746 |
NOTICE: TG_table_name: trigger_test_view |
| 747 |
NOTICE: TG_table_schema: public |
| 748 |
NOTICE: TG_when: {INSTEAD OF} |
| 749 |
NOTICE: args: {24 {skidoo view}} |
| 750 |
update trigger_test set v = 'update', test_skip=true where i = 1; |
| 751 |
NOTICE: NEW: {} |
| 752 |
NOTICE: OLD: {} |
| 753 |
NOTICE: TG_level: STATEMENT |
| 754 |
NOTICE: TG_name: statement_trigger |
| 755 |
NOTICE: TG_op: UPDATE |
| 756 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 757 |
NOTICE: TG_relid: bogus:12345 |
| 758 |
NOTICE: TG_table_name: trigger_test |
| 759 |
NOTICE: TG_table_schema: public |
| 760 |
NOTICE: TG_when: BEFORE |
| 761 |
NOTICE: args: {42 {statement trigger}} |
| 762 |
NOTICE: SKIPPING OPERATION UPDATE |
| 763 |
update trigger_test set v = 'update' where i = 1; |
| 764 |
NOTICE: NEW: {} |
| 765 |
NOTICE: OLD: {} |
| 766 |
NOTICE: TG_level: STATEMENT |
| 767 |
NOTICE: TG_name: statement_trigger |
| 768 |
NOTICE: TG_op: UPDATE |
| 769 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 770 |
NOTICE: TG_relid: bogus:12345 |
| 771 |
NOTICE: TG_table_name: trigger_test |
| 772 |
NOTICE: TG_table_schema: public |
| 773 |
NOTICE: TG_when: BEFORE |
| 774 |
NOTICE: args: {42 {statement trigger}} |
| 775 |
NOTICE: NEW: {i: 1, test_argisnull: f, test_return_null: f, test_skip: f, v: update} |
| 776 |
NOTICE: OLD: {i: 1, test_argisnull: f, test_return_null: f, test_skip: f, v: insert} |
| 777 |
NOTICE: TG_level: ROW |
| 778 |
NOTICE: TG_name: show_trigger_data_trig |
| 779 |
NOTICE: TG_op: UPDATE |
| 780 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 781 |
NOTICE: TG_relid: bogus:12345 |
| 782 |
NOTICE: TG_table_name: trigger_test |
| 783 |
NOTICE: TG_table_schema: public |
| 784 |
NOTICE: TG_when: BEFORE |
| 785 |
NOTICE: args: {23 skidoo} |
| 786 |
delete from trigger_test; |
| 787 |
NOTICE: NEW: {} |
| 788 |
NOTICE: OLD: {} |
| 789 |
NOTICE: TG_level: STATEMENT |
| 790 |
NOTICE: TG_name: statement_trigger |
| 791 |
NOTICE: TG_op: DELETE |
| 792 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 793 |
NOTICE: TG_relid: bogus:12345 |
| 794 |
NOTICE: TG_table_name: trigger_test |
| 795 |
NOTICE: TG_table_schema: public |
| 796 |
NOTICE: TG_when: BEFORE |
| 797 |
NOTICE: args: {42 {statement trigger}} |
| 798 |
NOTICE: NEW: {} |
| 799 |
NOTICE: OLD: {i: 1, test_argisnull: f, test_return_null: f, test_skip: f, v: update} |
| 800 |
NOTICE: TG_level: ROW |
| 801 |
NOTICE: TG_name: show_trigger_data_trig |
| 802 |
NOTICE: TG_op: DELETE |
| 803 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 804 |
NOTICE: TG_relid: bogus:12345 |
| 805 |
NOTICE: TG_table_name: trigger_test |
| 806 |
NOTICE: TG_table_schema: public |
| 807 |
NOTICE: TG_when: BEFORE |
| 808 |
NOTICE: args: {23 skidoo} |
| 809 |
truncate trigger_test; |
| 810 |
NOTICE: NEW: {} |
| 811 |
NOTICE: OLD: {} |
| 812 |
NOTICE: TG_level: STATEMENT |
| 813 |
NOTICE: TG_name: statement_trigger |
| 814 |
NOTICE: TG_op: TRUNCATE |
| 815 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 816 |
NOTICE: TG_relid: bogus:12345 |
| 817 |
NOTICE: TG_table_name: trigger_test |
| 818 |
NOTICE: TG_table_schema: public |
| 819 |
NOTICE: TG_when: BEFORE |
| 820 |
NOTICE: args: {42 {statement trigger}} |
| 821 |
DROP TRIGGER show_trigger_data_trig_before ON trigger_test_generated; |
| 822 |
DROP TRIGGER show_trigger_data_trig_after ON trigger_test_generated; |
| 823 |
-- should error |
| 824 |
insert into trigger_test(test_argisnull) values(true); |
| 825 |
NOTICE: NEW: {} |
| 826 |
NOTICE: OLD: {} |
| 827 |
NOTICE: TG_level: STATEMENT |
| 828 |
NOTICE: TG_name: statement_trigger |
| 829 |
NOTICE: TG_op: INSERT |
| 830 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 831 |
NOTICE: TG_relid: bogus:12345 |
| 832 |
NOTICE: TG_table_name: trigger_test |
| 833 |
NOTICE: TG_table_schema: public |
| 834 |
NOTICE: TG_when: BEFORE |
| 835 |
NOTICE: args: {42 {statement trigger}} |
| 836 |
ERROR: argisnull cannot be used in triggers |
| 837 |
-- should error |
| 838 |
insert into trigger_test(test_return_null) values(true); |
| 839 |
NOTICE: NEW: {} |
| 840 |
NOTICE: OLD: {} |
| 841 |
NOTICE: TG_level: STATEMENT |
| 842 |
NOTICE: TG_name: statement_trigger |
| 843 |
NOTICE: TG_op: INSERT |
| 844 |
NOTICE: TG_relatts: {{} i v {} test_skip test_return_null test_argisnull} |
| 845 |
NOTICE: TG_relid: bogus:12345 |
| 846 |
NOTICE: TG_table_name: trigger_test |
| 847 |
NOTICE: TG_table_schema: public |
| 848 |
NOTICE: TG_when: BEFORE |
| 849 |
NOTICE: args: {42 {statement trigger}} |
| 850 |
ERROR: return_null cannot be used in triggers |
| 851 |
-- test transition table visibility |
| 852 |
create table transition_table_test (id int, name text); |
| 853 |
insert into transition_table_test values (1, 'a'); |
| 854 |
create function transition_table_test_f() returns trigger language pltcl as |
| 855 |
$$ |
| 856 |
spi_exec -array C "SELECT id, name FROM old_table" { |
| 857 |
elog INFO "old: $C(id) -> $C(name)" |
| 858 |
} |
| 859 |
spi_exec -array C "SELECT id, name FROM new_table" { |
| 860 |
elog INFO "new: $C(id) -> $C(name)" |
| 861 |
} |
| 862 |
return OK |
| 863 |
$$; |
| 864 |
CREATE TRIGGER a_t AFTER UPDATE ON transition_table_test |
| 865 |
REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table |
| 866 |
FOR EACH STATEMENT EXECUTE PROCEDURE transition_table_test_f(); |
| 867 |
update transition_table_test set name = 'b'; |
| 868 |
INFO: old: 1 -> a |
| 869 |
INFO: new: 1 -> b |
| 870 |
drop table transition_table_test; |
| 871 |
drop function transition_table_test_f(); |
| 872 |
-- dealing with generated columns |
| 873 |
CREATE FUNCTION generated_test_func1() RETURNS trigger |
| 874 |
LANGUAGE pltcl |
| 875 |
AS $$ |
| 876 |
# not allowed |
| 877 |
set NEW(j) 5 |
| 878 |
return [array get NEW] |
| 879 |
$$; |
| 880 |
CREATE TRIGGER generated_test_trigger1 BEFORE INSERT ON trigger_test_generated |
| 881 |
FOR EACH ROW EXECUTE PROCEDURE generated_test_func1(); |
| 882 |
TRUNCATE trigger_test_generated; |
| 883 |
INSERT INTO trigger_test_generated (i) VALUES (1); |
| 884 |
ERROR: cannot set generated column "j" |
| 885 |
SELECT * FROM trigger_test_generated; |
| 886 |
i | j | k |
| 887 |
---+---+--- |
| 888 |
(0 rows) |
| 889 |
|