Top Banner
PRAGMA AUTONMOUS_TRANSACTIONS Илья Космодемьянский [email protected]
9

Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

Apr 15, 2017

Download

Education

Mail.Ru Group
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

PRAGMA AUTONMOUS_TRANSACTIONS

Илья Космодемьянский[email protected]

Page 2: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

Как ни удивительно

Автономные транзакции – одна из самых неприятных проблем миграции с Oracleна PostgreSQL

Page 3: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

Что это такое и для чего используется

• Вложенная транзакция внутри другой транзакции• Свой ROLLBACK и COMMIT• Автономная транзакция может быть закомичена, даже если внешняяоткатилась

• Удобно, например для логирования

Page 4: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

Пример

CREATE OR REPLACE function some_operation(err_msg IN VARCHAR) returns void ASBEGIN

INSERT INTO tt(id, description) VALUES (123, ‘foo bar’);INSERT INTO tt(id, description) VALUES (123, NULL);

EXCEPTIONWHEN OTHER THENPRAGMA AUTONOMOUS TRANSACTION;INSERT INTO err_log(id, timestamp, err_msg) VALUES(nextval(‘errno’), timenow(), err_msg);COMMIT;

RAISE not_null_violation;END;

Page 5: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

В PostgreSQL

• Готовых нет, но можно кое что предпринять• Способ с dblink или plproxy• Cпособ с savepoint• Сообщество в курсе проблемы

Page 6: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

savepoint

При входе в эксепшн plpgsql ставит неявный savepoint

do $code$begininsert into t values(1);raise notice ’1:%’, (select count(*) from t); --1:1begininsert into t values(1);raise notice ’2:%’, (select count(*) from t); --2:1raise exception sqlstate ’ZQ001’;

exceptionwhen sqlstate ’ZQ000’ thenraise notice ’3:%’, (select count(*) from t); --3:1

end;raise notice ’4:%’, (select count(*) from t); --4:1

end;$code$

Page 7: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

savepoint - results

NOTICE: 1:2NOTICE: 2:3NOTICE: 3:2NOTICE: 4:2DO

Page 8: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

dblink или plproxy

pl/proxy это способ вызова удаленной хранимой процедуры на языке plpgsql

• все то же самое что и с savepoint, только вместо raise notice вызов удаленнойпроцедуры

• не быстро и есть подводные камни

Page 9: Илья Космодемьянский, PostgreSQL-Consulting.com. «Pragma autonomous transaction»

Сообщество в курсе

• https://wiki.postgresql.org/wiki/AutonomousTransactionsUnconference2015• 2ndQuadrant имеет наработки• В PostgreSQL будут скорее sub-транзакции• Уровни вложенности вряд-ли будут когда-то реализованы