[PostgreSQL] Notify

2022. 3. 25. 21:03·DataBase/PostgreSQL

 

When to use?

postgreSQL 내에서 특정 row 가 변경되면 해당 이벤트를 특정 채널에 Notify 할 수 있는 기능.

ex) Node.js 에서 PostgreSQL 에서 발생한 특정 이벤트 페이로드를 받아온다.

 

 

 

Syntax

pg_notify() is just a convenient wrapper function for the SQL NOTIFY command.

pg_notify (channel-name, payload)

Channel

알림 채널명, 채널명은 테이블명 그대로 따라가는 것이 관례다.

Payload

8,000 바이트 이하의 문자열 데이터

 

💡 Notify in transaction

NOTIFY behaves is executed in a transaction, the notify events are not develiverd until and unless the transaction is committed. If transaction would be aborted, all the commands should have no effect.
Notification events are only delivered between transaction.

👉🏻 트랜잭션이 완결(COMMIT)되는 시점에만 알림이 푸시되고,
송신 순서가 보장된다.

 

트리거 함수 및 트리거 생성

예제 1.  1개 트랜잭션 내에 2개의 트리거 발동 조건

<test> 테이블에 pg_notify 트리거 함수와 트리거를 설정한다.

 

예상하기로는, 총 2개의 INSERT 이벤트로 2번의 트리거가 발동해야한다.

2개가 아닌 1개의 메시지만 전달된다.

 

예상 밖이다. 
트리거 조건인 INSERT 를 2번 실행했음에도 
알림 메시지는 1개만 왔다.
왜 그런 것일까?
If the same channel name is signaled multiple times with identical payload strings within the same transaction,
Only one instance of the notification event is delivered to listeners.

👉🏻 동일 트랜잭션 내에 같은 스트링을 같는 메시지를 여러번 전달 시 단 하나의 알림 메시지만 리스너에게 전달된다.

 

아래와 같이 페이로드를 변경하면(INSERT 마다 다른 메시지 스트링) 2개의 메시지가 온다.

2개의 메시지가 모두 전달됐다.

 

예제 2.  2개의 트랜잭션에 각각 1개씩 트리거 발동조건

 

 

📝 결론

pg_notify (NOTIFY) 는 트랜잭션 단위로 실행되고, 순서가 보장되며 `COMMIT` 된 것만 알림이 간다. ROLLBACK(aborted) 된 것은 버린다.

⚠️ 단, 전달 메시지 페이로드가 동일할 경우 단 하나의 알림 메시지만 전달된다.

 


🔗 Reference

 

NOTIFY

NOTIFY NOTIFY — generate a notification Synopsis NOTIFY channel [ , payload ] Description The NOTIFY command sends a notification …

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] Transaction + Pool 사용시 주의  (0) 2022.04.04
[PostgreSQL] Function (feat. trigger)  (0) 2022.03.29
[PostgreSQL] Transaction 왜, 언제, 어떻게 동작하나?  (0) 2022.03.25
[PostgreSQL] Sequence 함수 및 값 변경 (번역)  (0) 2022.03.25
Description on DB Column  (0) 2022.03.18
'DataBase/PostgreSQL' 카테고리의 다른 글
  • [PostgreSQL] Transaction + Pool 사용시 주의
  • [PostgreSQL] Function (feat. trigger)
  • [PostgreSQL] Transaction 왜, 언제, 어떻게 동작하나?
  • [PostgreSQL] Sequence 함수 및 값 변경 (번역)
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • 전체
    오늘
    어제
    • 분류 전체보기 (429) N
      • 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 (64) N
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (23) N
        • Error (7)
      • 기타 (68)
        • Kafka (3)
        • Kubernetes (3)
        • Docker (12)
        • git (19)
        • 잡동사니 (26)
      • 재테크 (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
  • 공지사항

  • 인기 글

  • 태그

    android
    프로그래머스
    PostgreSQL
    알고리즘
    ubuntu
    C++
    algorithm
    Bitcoin
    Spring
    kafka
    Programmers
    java
    linux
    database
    Kotlin
    docker
    Git
    javascript
    백준
    JPA
  • hELLO· Designed By정상우.v4.10.3
M_Falcon
[PostgreSQL] Notify
상단으로

티스토리툴바