[PostgreSQL] Trigger + Transaction

2022. 4. 18. 16:43ยทDataBase/PostgreSQL

 

๐ŸŽฏ Goals

  • ํŠธ๋ฆฌ๊ฑฐ , ํŠธ๋žœ์žญ์…˜ ๋‚ด์—์„œ ์–ธ์ œ ์‹คํ–‰๋ ๊นŒ?

3๊ฐ€์ง€ ํƒ€์ž…์˜ ํŠธ๋ฆฌ๊ฑฐ

  1. BEFORE
  2. โœ… AFTER (Only handle this in this post)
  3. INSTEAD OF

๊ฐ ํŠธ๋ฆฌ๊ฑฐ ํƒ€์ž…์— ๋”ฐ๋ผ Default Behavior ๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ๊ผญ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค. ์ด ๊ธ€์—์„  `AFTER` ํŠธ๋ฆฌ๊ฑฐ๋งŒ ๋‹ค๋ฃฌ๋‹ค.

 

 

์•„๋ž˜ SQL๋ฌธ์„ ๋ณด๊ณ  ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ƒํ•ด๋ณด์ž.

๊ณผ์—ฐ ์—ฌ๊ธฐ์„œ ์–ด๋–ค ์ˆœ์„œ๋กœ ๋กœ๊ทธ๊ฐ€ ์ฐํž๊นŒ?

 

Each statement? vs After all transaction `COMMIT()`?

๋‹น์—ฐํžˆ ๋‚˜๋Š” ํ›„์ž๋กœ ์•Œ๊ณ ์žˆ์—ˆ๋‹ค, ํŠธ๋žœ์žญ์…˜์˜ ๋ชจ๋“  Statement ๊ฐ€ ๋๋‚œ ํ›„์— `COMMIT()`  ๋œ ์‹œ์ ์— ํŠธ๋ฆฌ๊ฑฐ๊ฐ€ ๋ฐœ๋™ํ•  ์ค„ ์•Œ์•˜๋‹ค.

์˜ˆ์ƒํ•œ ๋กœ๊ทธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

 

โŒ ํ‹€๋ ธ๋‹ค, ๊ฐ ๋ฌธ์žฅ๋งˆ๋‹ค ๊ณง๋ฐ”๋กœ ํŠธ๋ฆฌ๊ฑฐ๊ฐ€ ์‹คํ–‰๋๋‹ค.

 

 

๊ณต์‹ ๋ฌธ์„œ ํ›”์ณ๋ณด๊ธฐ

Row-level `BEFORE` triggers fire immediately before a particular row is operated on,
while row-level `AFTER` triggers fire at the end of the statement (but before any statement-level 
`AFTER` triggers). 
The execution of an `AFTER` trigger can be deferred to the end of the transaction, rather than the end of the statement, if it was defined as a constraint trigger.
In all cases, a trigger is executed as part of the same transaction as the statement that triggered it, so if either the statement or the trigger causes an error, the effects of both will be rolled back.

 

๐Ÿ“ ํŠธ๋ฆฌ๊ฑฐ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Statement ๊ฐ€ ๋๋‚˜๋Š” ์‹œ์ ์— ์‹คํ–‰๋˜์ง€๋งŒ, `Constraint` ๋ฅผ ๊ฑธ๋ฉด, AFTER ํŠธ๋ฆฌ๊ฑฐ์˜ ์‹คํ–‰ ์‹œ์ ์„ ํŠธ๋žœ์žญ์…˜ ์ข…๋ฃŒ์‹œ๋กœ ๋ฏธ๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.

 

 

Constraint Trigger ๋กœ ์ง€์—ฐ ์‹คํ–‰ํ•˜๊ธฐ

๊ฒฐ๋ก ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค, ํŠธ๋ฆฌ๊ฑฐ ์ƒ์„ฑ์‹œ `CONSTRAINT` ํ‚ค์›Œ๋“œ์™€ `INITALLY DEFERRED` ์˜ต์…˜๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค.

โš ๏ธ CONSTRAINT TRIGGER ๋Š” ์˜ค๋กœ์ง€ `AFETR` ํŠธ๋ฆฌ๊ฑฐ์— FOR EACH ROW ๋ ˆ๋ฒจ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์ด์™€ ๊ด€๋ จ๋œ ๋‘๊ฐ€์ง€ ์˜ต์…˜์„ ๋ชจ๋‘ ์•Œ๊ณ ์žˆ์–ด์•ผ ์ ์ ˆํ•œ ์šฉ๋„๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

[DEFERRABLE]
(์ค‘๋žต..)
A constraint that is not deferrable will be checked immediately after every command.
Checking of constraints that are deferrable can be postponed until the end of the transaction (using the SET CONSTRAINTS command). NOT DEFERRABLE is the default. Currently, only UNIQUE, PRIMARY KEY, EXCLUDE, and REFERENCES (foreign key) constraints accept this clause. NOT NULL and CHECK constraints are not deferrable.

[INITIALLY DEFERRED]
If a constraint is deferrable, this clause specifies the default time to check the constraint. If the constraint is 
INITIALLY IMMEDIATE, it is checked after each statement. This is the default.
If the constraint is INITIALLY DEFERRED, it is checked only at the end of the transaction. The constraint check time can be altered with the SET CONSTRAINTS command.

 

 

์˜๋ฌธ ๊ทธ๋Œ€๋กœ ์ง์—ญํ–ˆ์„ ๋•Œ๋Š” ์šฉ๋„์™€ ๋™์ž‘ ๋ฐฉ์‹์˜ ์ฐจ์ด๊ฐ€ ์•„๋ฆฌ์†กํ•˜๋‹ค.

โš ๏ธ DEFERRABLE ์˜ต์…˜๊ณผ
INITIALLY DEFERRED ์˜ต์…˜์€ ๋‹ค๋ฅด๋‹ค.

DEFERRABLE

๐Ÿ‘‰๐Ÿป Unique, PK, EXCLUDE, FK ์ œ์•ฝ์กฐ๊ฑด ๊ฒ€์‚ฌ๋ฅผ ํŠธ๋žœ์žญ์…˜ ์ข…๋ฃŒ ์‹œ์ ๊นŒ์ง€  ์ง€์—ฐ์‹œํ‚จ๋‹ค.

(Default:  NOT DEFERRABLE, (SQL ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ์ œ์•ฝ์กฐ๊ฑด ๊ฒ€์ฆ))

 

INITIALLY DEFERRED

๐Ÿ‘‰๐Ÿป ๋ชจ๋“  ๋ฌธ์žฅ์˜ ์ œ์•ฝ ์กฐ๊ฑด ๊ฒ€์ฆ์„ ํŠธ๋žœ์žญ์…˜ ์ข…๋ฃŒ ์‹œ์ ๊นŒ์ง€ ์ง€์—ฐ์‹œํ‚จ๋‹ค.

(Default: INITIALLY IMMEDIATE)

 

 

โš ๏ธ ์œ„ ์˜ˆ์ œ์—์„œ `DEFFERABLE' ๋งŒ ์„ค์ •ํ•˜๋ฉด ์ด์ „ ์˜ˆ์ œ์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค.
(SQL 1๋ฒˆ -> ํŠธ๋ฆฌ๊ฑฐ ๋ฐœ๋™-> SQL 2๋ฒˆ -> ํŠธ๋ฆฌ๊ฑฐ ๋ฐœ๋™)

 

 

 

 

 


๐Ÿค” ์ด๊ฒŒ ๋ญ” ๋œป์ด์ง€? (์ถ”ํ›„ ๋ถ„์„)

 In all cases, a trigger is executed as part of the same transaction as the statement that triggered it, so if either the statement or the trigger causes an error, the effects of both will be rolled back.

๋ฌธ์žฅ์ด ํŠธ๋ฆฌ๊ฑฐ ์‹œํ‚ด์œผ๋กœ์จ, ํŠธ๋ฆฌ๊ฑฐ๋Š” ํŠธ๋žœ์žญ์…˜์˜ ์ผ๋ถ€๋กœ ์‹คํ–‰๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ฌธ์žฅ์ด๋‚˜ ํŠธ๋ฆฌ๊ฑฐ๊ฐ€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, ์–‘์ชฝ๋‹ค ๋กค๋ฐฑ๋œ๋‹ค.

์ง์—ญ์€ ์ด๋Ÿฌํ•œ๋ฐ ์™€๋‹ฟ์ง€ ์•Š๋Š”๋‹ค. ์ •ํ™•ํ•œ ์˜ˆ์‹œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

 

 

 

 

 


๐Ÿ”— Reference

 

SQL Server Triggers and Transactions – Gavin Draper

 

gavindraper.com

 

CREATE CONSTRAINT TRIGGER

CREATE CONSTRAINT TRIGGER Name CREATE CONSTRAINT TRIGGER -- define a new constraint trigger Synopsis CREATE CONSTRAINT TRIGGER name AFTER event [ OR ... ] ON table_name [ FROM referenced_table_name ] { NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIAT

www.postgresql.org

 

CREATE TABLE

CREATE TABLE Name CREATE TABLE -- define a new table Synopsis CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( [ { column_name data_type [ DEFAULT default_expr ] [ column_constraint [ ... ] ] | table_constraint | LIKE parent_table [

www.postgresql.org

 

 

Executing a trigger AFTER the completion of a transaction

In PostgreSQL, are DEFERRED triggers executed before (within) the completion of the transaction or just after it? The documentation says: DEFERRABLE NOT DEFERRABLE This controls whether ...

stackoverflow.com

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'DataBase > PostgreSQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[PostgreSQL] Use `identity` instead of `serial`  (0) 2022.06.16
[PostgreSQL] CHECK Constraint  (0) 2022.05.04
[PostgreSQL] Transaction + Pool ์‚ฌ์šฉ์‹œ ์ฃผ์˜  (0) 2022.04.04
[PostgreSQL] Function (feat. trigger)  (0) 2022.03.29
[PostgreSQL] Notify  (0) 2022.03.25
'DataBase/PostgreSQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [PostgreSQL] Use `identity` instead of `serial`
  • [PostgreSQL] CHECK Constraint
  • [PostgreSQL] Transaction + Pool ์‚ฌ์šฉ์‹œ ์ฃผ์˜
  • [PostgreSQL] Function (feat. trigger)
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (432)
      • Web (16)
        • Nodejs (14)
        • Javascript (23)
        • FrontEnd (4)
      • DataBase (39)
        • Fundamental (1)
        • Redis (4)
        • PostgreSQL (10)
        • NoSQL (4)
        • MySQL (9)
        • MSSQL (3)
        • Error (4)
      • Algorithm (79)
        • Algorithm (๋ฌธ์ œํ’€์ด) (56)
        • Algorithm (์ด๋ก ) (23)
      • JVM (65)
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (24)
        • Error (7)
      • ๊ธฐํƒ€ (70)
        • Kafka (3)
        • Kubernetes (3)
        • Docker (13)
        • git (19)
        • ์žก๋™์‚ฌ๋‹ˆ (27)
      • ์žฌํ…Œํฌ (11)
        • ์„ธ๋ฌด (4)
        • ํˆฌ์ž (3)
        • ๋ณดํ—˜ (0)
      • BlockChain (2)
        • BitCoin (0)
      • C (32)
        • C (10)
        • C++ (17)
        • Error (3)
      • Low Level (8)
        • OS (3)
        • ์‹œ์Šคํ…œ ๋ณด์•ˆ (5)
      • ๋„คํŠธ์›Œํฌ (3)
      • LINUX (30)
        • Linux (26)
        • Error (4)
      • ์ €์ž‘๊ถŒ๊ณผ ์Šค๋งˆํŠธํฐ์˜ ์ดํ•ด (0)
      • ์ƒ๊ฐ ๋ญ‰์น˜ (6)
      • ๊ถ๊ธˆ์ฆ (2)
      • Private (4)
        • ์ด์ง ๊ฒฝํ—˜ (0)
        • ๊ฟˆ์„ ์ฐพ์•„์„œ (1)
      • Android (21)
        • OS (4)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • WEB
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜
    • DataBase
    • Linux
    • Mobile
    • C
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

    • github
  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    linux
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    java
    Git
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    docker
    PostgreSQL
    JPA
    javascript
    android
    Bitcoin
    kafka
    Kotlin
    Spring
    ๋ฐฑ์ค€
    algorithm
    ubuntu
    Programmers
    database
    C++
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
M_Falcon
[PostgreSQL] Trigger + Transaction
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”