[PostgreSQL] Function (feat. trigger)

2022. 3. 29. 16:14·DataBase/PostgreSQL

 

 

 

1. 함수 사용 방법

함수 선언

 

 

리턴 타입이 `TRIGGER` 일 때

이 때는 반드시 트리거가 파라미터를 넘겼을 때 `TG_ARGV` 로 받아야한다.

plpgsql 언어로 함수를 정의하고 리턴타입이 `TRIGGER` 인 경우, 자동으로 다음과 같은 변수가 블록 최상단에 선언된다.

 

  • NEW
    레코드 타입, 새로 `INSERT | UPDATE` 된 로우
    DELTE에 대해서는 null 값을 가짐
  • OLD
    레코드 타입, 변경 전 `UPDATE | DELETE` 된 로우
    INSERT에 대해서는 null 값을 가짐
  • TG_ARGV[]
    text 배열, 파라미터를 받아올 때 이 키워드로 받아와야함.
트리거 함수는 반드시
NULL이나 레코드 타입을 반환해야함.

트리거

트리거 선언

 

 

네이밍 컨벤션

함수

fn_<동사>

postgreSQL 문서를 보면 절대적인 네이밍 컨벤션은 없는 것 같다.

 

트리거 

tr_<테이블명>_<C || R || U || D>
  • users 테이블 업데이트 트리거=> tr_users_U
  • users 테이블 삭제 트리거 => tr_users_D

💡 트리거 함수 정의 팁

트리거 함수는 선언 전에 미리 EXECUTE 할 함수 원형이 정의 되어 있어야 하고,  

Argument 를 함수 프로토타입에서 정의해선 안된다.

The trigger function must be defined before the trigger itself can be created.
The trigger function must be declared as a function taking no arguments and returning type trigger
(The trigger function receives its input through a specially-passed `TriggerData` structure, not in the form of ordinary function arguments.)
 - PostgresSQL Document

 

TG_ARGV 인자를 사용해서 넘겨야한다.

 

 

 


🔗 Reference

 

PostgreSQL CREATE TRIGGER Statement By Practical Examples

Summary: in this tutorial, you will learn how to use the PostgreSQL CREATE TRIGGER statement to create a trigger. To create a new trigger in PostgreSQL, you follow these steps: First, create a trigger function using CREATE FUNCTION statement.Second, bind t

www.postgresqltutorial.com

 

PostgreSQL CREATE FUNCTION By Practical Examples

Summary: in this tutorial, you will learn how to use the PostgreSQL CREATE FUNCTION statement to develop user-defined functions. Introduction to Create Function statement The create function statement allows you to define a new user-defined function. The f

www.postgresqltutorial.com

 

 

Data Points: Exploring SQL Server Triggers

Table of contents Article 10/22/2019 17 minutes to read In this article --> Data Points Exploring SQL Server Triggers John Papa Contents Trigger Types Firing Sequence and Referential Integrity Special Tables Performance Until Next Time Triggers are one of

docs.microsoft.com

 

User-Defined Functions - SQL Server

User-Defined Functions

docs.microsoft.com

 

 

Postgresql: How to pass an array to trigger function in TG_ARGV?

I’ve got some fairly generic Postgres trigger functions that I’d like to re-use across a few different tables. To that end, I’m passing arguments as part of the CREATE TRIGGER statement so they’ll be available in the TG_ARGV array, as described [

getridbug.com

 

저작자표시 (새창열림)

'DataBase > PostgreSQL' 카테고리의 다른 글

[PostgreSQL] Trigger + Transaction  (0) 2022.04.18
[PostgreSQL] Transaction + Pool 사용시 주의  (0) 2022.04.04
[PostgreSQL] Notify  (0) 2022.03.25
[PostgreSQL] Transaction 왜, 언제, 어떻게 동작하나?  (0) 2022.03.25
[PostgreSQL] Sequence 함수 및 값 변경 (번역)  (0) 2022.03.25
'DataBase/PostgreSQL' 카테고리의 다른 글
  • [PostgreSQL] Trigger + Transaction
  • [PostgreSQL] Transaction + Pool 사용시 주의
  • [PostgreSQL] Notify
  • [PostgreSQL] Transaction 왜, 언제, 어떻게 동작하나?
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
  • 공지사항

  • 인기 글

  • 태그

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

티스토리툴바