规格约束

规格约束 - 图1 注意:

  • 自治事务执行时,将会在后台启动自治事务session,我们可以通过max_concurrent_autonomous_transactions设置自治事务执行的最大并行数量,该参数取值范围为0~1024,默认值为10。
  • 当max_concurrent_autonomous_transactions参数设置为0时,自治事务将无法执行。
  • 自治事务新启session后,将使用默认session参数,不共享主session下对象(包括session级别变量,本地临时变量,全局临时表的数据等)。
  • 触发器函数不支持自治事务。

    1. CREATE TABLE test_trigger_des_tbl(id1 INT, id2 INT, id3 INT);
    2. CREATE OR REPLACE FUNCTION tri_insert_func() RETURNS TRIGGER AS
    3. $$
    4. DECLARE
    5. PRAGMA AUTONOMOUS_TRANSACTION;
    6. BEGIN
    7. INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
    8. RETURN NEW;
    9. END
    10. $$ LANGUAGE PLPGSQL;
  • 自治事务不支持非顶层匿名块调用(仅支持顶层自治事务,包括存储过程、函数、匿名块)。

  • 自治事务仅支持PROCEDURE OUT参数传递ref cursor参数,不支持IN、INOUT以及FUNCTION传递ref cursor参数。

    1. create table sections(section_ID int);
    2. insert into sections values(1);
    3. insert into sections values(1);
    4. insert into sections values(1);
    5. insert into sections values(1);
    6. 1. PROCEDURE OUT出参传递ref cursor(支持)
    7. CREATE OR REPLACE PROCEDURE proc_sys_ref(OUT c1 refcursor)
    8. IS
    9. declare
    10. PRAGMA AUTONOMOUS_TRANSACTION;
    11. BEGIN
    12. OPEN c1 FOR SELECT section_ID FROM sections ORDER BY section_ID;
    13. END;
    14. /
    15. CREATE OR REPLACE PROCEDURE proc_sys_call() AS
    16. DECLARE
    17. c1 SYS_REFCURSOR;
    18. TEMP NUMBER(4);
    19. BEGIN
    20. proc_sys_ref(c1);
    21. if c1%isopen then
    22. raise notice '%','ok';
    23. end if;
    24. LOOP
    25. FETCH C1 INTO TEMP;
    26. raise notice '%',C1%ROWCOUNT;
    27. EXIT WHEN C1%NOTFOUND;
    28. END LOOP;
    29. END;
    30. /
    31. 2. PROCEDURE ININOUT出参传递ref cursor(不支持)
    32. CREATE OR REPLACE PROCEDURE proc_sys_ref(IN c1 refcursor)
    33. IS
    34. declare
    35. PRAGMA AUTONOMOUS_TRANSACTION;
    36. BEGIN
    37. if c1%isopen then
    38. raise notice '%','ok';
    39. end if;
    40. LOOP
    41. FETCH C1 INTO TEMP;
    42. raise notice '%',C1%ROWCOUNT;
    43. EXIT WHEN C1%NOTFOUND;
    44. END LOOP;
    45. END;
    46. /
    47. CREATE OR REPLACE PROCEDURE proc_sys_call() AS
    48. DECLARE
    49. c1 SYS_REFCURSOR;
    50. TEMP NUMBER(4);
    51. BEGIN
    52. OPEN c1 FOR SELECT section_ID FROM sections ORDER BY section_ID;
    53. proc_sys_ref(c1);
    54. END;
    55. /
    56. 3. FUNCTION RETURN传递ref cursor(不支持)
    57. CREATE OR REPLACE function proc_sys_ref()
    58. return SYS_REFCURSOR
    59. IS
    60. declare
    61. PRAGMA AUTONOMOUS_TRANSACTION;
    62. C1 SYS_REFCURSOR;
    63. BEGIN
    64. OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID;
    65. return C1;
    66. END;
    67. /
    68. 4. FUNCTION OUT出参传递ref cursor(不支持)
    69. CREATE OR REPLACE function proc_sys_ref(C1 out SYS_REFCURSOR)
    70. return SYS_REFCURSOR
    71. IS
    72. declare
    73. PRAGMA AUTONOMOUS_TRANSACTION;
    74. BEGIN
    75. OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID;
    76. return 1;
    77. END;
    78. /
  • 自治事务函数不支持返回非out形式的record类型。

  • 不支持修改自治事务的隔离级别。

  • 不支持自治事务返回集合类型(setof)。

    1. create table test_in (id int,a date);
    2. create table test_main (id int,a date);
    3. insert into test_main values (1111,'2021-01-01'),(2222,'2021-02-02');
    4. truncate test_in,test_main;
    5. CREATE OR REPLACE FUNCTION autonomous_f_022(num1 int) RETURNS SETOF test_in
    6. LANGUAGE plpgsql AS $$
    7. DECLARE
    8. count int :=3;
    9. test_row test_in%ROWTYPE;
    10. PRAGMA AUTONOMOUS_TRANSACTION;
    11. BEGIN
    12. while true
    13. loop
    14. if count=3 then
    15. null;
    16. else
    17. if count=2 then
    18. insert into test_main values (count,'2021-03-03');
    19. goto pos1;
    20. end if;
    21. end if;
    22. count=count-1;
    23. end loop;
    24. insert into test_main values (1000,'2021-04-04');
    25. <<pos1>>
    26. for test_row in select * from test_main
    27. loop
    28. return next test_row;
    29. end loop;
    30. return;
    31. END;
    32. $$
    33. ;