보관도 할겸 예제 올려드립니다.
postgresql v.11 부터 지원하는 듯 합니다.
https://www.2ndquadrant.com/en/blog/postgresql-11-server-side-procedures-part-2/
drop table if exists test1 ;
CREATE TABLE test1 (a int);
CREATE PROCEDURE transaction_test1()
AS $$
BEGIN
FOR i IN 0..9 LOOP
INSERT INTO test1 (a) VALUES (i);
IF i % 2 = 0 THEN
RAISE NOTICE 'i=%, txid=% will be committed', i, txid_current();
COMMIT;
ELSE
RAISE NOTICE 'i=%, txid=% will be rolledback', i, txid_current();
ROLLBACK;
END IF;
END LOOP;
END
$$
LANGUAGE PLPGSQL;
CALL transaction_test1();
SELECT xmin,* FROM test1;
|