7

Creating A Trigger in PostgreSQL

 2 years ago
source link: https://jdhao.github.io/2022/09/12/postgresql_trigger_function/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Creating A Trigger in PostgreSQL

2022-09-12 304 words 2 mins read 6 times read

We can use trigger to do things automatically, for example, preventing insertion of a record if certain condition is met. Here is an example trigger function and its usage.

Suppose we have a loan table with following column:

  • copy_id: id for an item
  • lend_date: date when it is lent
  • return_date: date when it is returned, can be NULL (has been returned yet)

Create a trigger function

In this example, we create a function to check whether the new record is a valid record. If lend_date for new record is later than existing record, and is smaller than return_date of the copy, or if this copy hasn’t been returned (loan.return_data IS NULL), then this is invalid record.

CREATE OR REPLACE FUNCTION check_loan_validity()
    RETURNS TRIGGER
    LANGUAGE PLPGSQL
AS
$func$
BEGIN
    IF EXISTS(SELECT 1
              FROM loan
              WHERE loan.copy_id = NEW.copy_id
                AND NEW.lend_date >= loan.lend_date
                AND (NEW.lend_date <= loan.return_date OR loan.return_date IS NULL))
    THEN
        RAISE EXCEPTION 'Invalid lend_date provided: %', NEW.lend_date;
    END IF;

    RETURN NEW;
END;
$func$;

The statement LANGUAGE PLPGSQL tells postgres that we can using plpgsql syntax, which is more powerful than plain SQL. See more discussion here.

You can also put the language declaration after the function body (see also this post):

$func$
<function body>
$func$ LANGUAGE PLPGSQL;

All string inside the dollar signs are function body definition. The use of dollar sign here is to simplify the writing of the function, as indicated by official doc on functions:

The function body is simply a string literal so far as CREATE FUNCTION is concerned. It is often helpful to use dollar quoting (see Section 4.1.2.4) to write the function body, rather than the normal single quote syntax. Without dollar quoting, any single quotes or backslashes in the function body must be escaped by doubling them

When creating a trigger function, the variable NEW represents the new records that is going to be inserted or updated.

Create the trigger

After creating the trigger function, we can now create a trigger on this table.

CREATE TRIGGER check_loan_trigger
    BEFORE INSERT
    ON loan
    FOR EACH ROW
EXECUTE PROCEDURE check_loan_validity();

References

Author jdhao

LastMod

2022-09-12

License CC BY-NC-ND 4.0

Reward

Next


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK