@@ -1443,3 +1443,120 @@ NOTICE: drop cascades to 2 other objects
1443
1443
DETAIL: drop cascades to view city_view
1444
1444
drop cascades to view european_city_view
1445
1445
DROP TABLE country_table;
1446
+ -- Test pg_trigger_depth()
1447
+ create table depth_a (id int not null primary key);
1448
+ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "depth_a_pkey" for table "depth_a"
1449
+ create table depth_b (id int not null primary key);
1450
+ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "depth_b_pkey" for table "depth_b"
1451
+ create table depth_c (id int not null primary key);
1452
+ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "depth_c_pkey" for table "depth_c"
1453
+ create function depth_a_tf() returns trigger
1454
+ language plpgsql as $$
1455
+ begin
1456
+ raise notice '%: depth = %', tg_name, pg_trigger_depth();
1457
+ insert into depth_b values (new.id);
1458
+ raise notice '%: depth = %', tg_name, pg_trigger_depth();
1459
+ return new;
1460
+ end;
1461
+ $$;
1462
+ create trigger depth_a_tr before insert on depth_a
1463
+ for each row execute procedure depth_a_tf();
1464
+ create function depth_b_tf() returns trigger
1465
+ language plpgsql as $$
1466
+ begin
1467
+ raise notice '%: depth = %', tg_name, pg_trigger_depth();
1468
+ begin
1469
+ execute 'insert into depth_c values (' || new.id::text || ')';
1470
+ exception
1471
+ when sqlstate 'U9999' then
1472
+ raise notice 'SQLSTATE = U9999: depth = %', pg_trigger_depth();
1473
+ end;
1474
+ raise notice '%: depth = %', tg_name, pg_trigger_depth();
1475
+ if new.id = 1 then
1476
+ execute 'insert into depth_c values (' || new.id::text || ')';
1477
+ end if;
1478
+ return new;
1479
+ end;
1480
+ $$;
1481
+ create trigger depth_b_tr before insert on depth_b
1482
+ for each row execute procedure depth_b_tf();
1483
+ create function depth_c_tf() returns trigger
1484
+ language plpgsql as $$
1485
+ begin
1486
+ raise notice '%: depth = %', tg_name, pg_trigger_depth();
1487
+ if new.id = 1 then
1488
+ raise exception sqlstate 'U9999';
1489
+ end if;
1490
+ raise notice '%: depth = %', tg_name, pg_trigger_depth();
1491
+ return new;
1492
+ end;
1493
+ $$;
1494
+ create trigger depth_c_tr before insert on depth_c
1495
+ for each row execute procedure depth_c_tf();
1496
+ select pg_trigger_depth();
1497
+ pg_trigger_depth
1498
+ ------------------
1499
+ 0
1500
+ (1 row)
1501
+
1502
+ insert into depth_a values (1);
1503
+ NOTICE: depth_a_tr: depth = 1
1504
+ NOTICE: depth_b_tr: depth = 2
1505
+ CONTEXT: SQL statement "insert into depth_b values (new.id)"
1506
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1507
+ NOTICE: depth_c_tr: depth = 3
1508
+ CONTEXT: SQL statement "insert into depth_c values (1)"
1509
+ PL/pgSQL function "depth_b_tf" line 5 at EXECUTE statement
1510
+ SQL statement "insert into depth_b values (new.id)"
1511
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1512
+ NOTICE: SQLSTATE = U9999: depth = 2
1513
+ CONTEXT: SQL statement "insert into depth_b values (new.id)"
1514
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1515
+ NOTICE: depth_b_tr: depth = 2
1516
+ CONTEXT: SQL statement "insert into depth_b values (new.id)"
1517
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1518
+ NOTICE: depth_c_tr: depth = 3
1519
+ CONTEXT: SQL statement "insert into depth_c values (1)"
1520
+ PL/pgSQL function "depth_b_tf" line 12 at EXECUTE statement
1521
+ SQL statement "insert into depth_b values (new.id)"
1522
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1523
+ ERROR: U9999
1524
+ CONTEXT: SQL statement "insert into depth_c values (1)"
1525
+ PL/pgSQL function "depth_b_tf" line 12 at EXECUTE statement
1526
+ SQL statement "insert into depth_b values (new.id)"
1527
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1528
+ select pg_trigger_depth();
1529
+ pg_trigger_depth
1530
+ ------------------
1531
+ 0
1532
+ (1 row)
1533
+
1534
+ insert into depth_a values (2);
1535
+ NOTICE: depth_a_tr: depth = 1
1536
+ NOTICE: depth_b_tr: depth = 2
1537
+ CONTEXT: SQL statement "insert into depth_b values (new.id)"
1538
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1539
+ NOTICE: depth_c_tr: depth = 3
1540
+ CONTEXT: SQL statement "insert into depth_c values (2)"
1541
+ PL/pgSQL function "depth_b_tf" line 5 at EXECUTE statement
1542
+ SQL statement "insert into depth_b values (new.id)"
1543
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1544
+ NOTICE: depth_c_tr: depth = 3
1545
+ CONTEXT: SQL statement "insert into depth_c values (2)"
1546
+ PL/pgSQL function "depth_b_tf" line 5 at EXECUTE statement
1547
+ SQL statement "insert into depth_b values (new.id)"
1548
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1549
+ NOTICE: depth_b_tr: depth = 2
1550
+ CONTEXT: SQL statement "insert into depth_b values (new.id)"
1551
+ PL/pgSQL function "depth_a_tf" line 4 at SQL statement
1552
+ NOTICE: depth_a_tr: depth = 1
1553
+ select pg_trigger_depth();
1554
+ pg_trigger_depth
1555
+ ------------------
1556
+ 0
1557
+ (1 row)
1558
+
1559
+ drop table depth_a, depth_b, depth_c;
1560
+ drop function depth_a_tf();
1561
+ drop function depth_b_tf();
1562
+ drop function depth_c_tf();
0 commit comments