moadeep
moadeep

Reputation: 4118

PostgreSQL Trigger/Function to ensure date is in past

I want to put a constraint on a date of birth field for one of my db tables. Essentially I want to ensure pat_dob_dt is at least 16 years ago (from current date). I am using PostgreSQL 8.4.20 and used here for guidance:

CREATE OR REPLACE FUNCTION patient_dob_in_past()
RETURNS TRIGGER AS $$
BEGIN
 -- check pat_dob_dt is in past --
    IF ( NEW.pat_dob_dt  > current_date - interval '16 years' ) THEN
       RAISE EXCEPTION '% must be 16 years in past', NEW.pat_dob_dt
    END IF;
    RETURN NEW;
END;
$$ language 'plpgsql';


CREATE OR REPLACE TRIGGER patient_dob_in_past BEFORE UPDATE OR INSERT
ON patients FOR EACH ROW EXECUTE PROCEDURE patient_dob_in_past();

Unfortunately I am met with the following error

ERROR:  syntax error at or near "END" at character 14
QUERY:  SELECT   $1  END IF
CONTEXT:  SQL statement in PL/PgSQL function "patient_dob_in_past" near line 4
LINE 1: SELECT   $1  END IF

Not sure where I am going wrong since I am following the psql docs for 8.4

EDIT Semicolon fixeds function issue. I also get an error for my trigger

ERROR: syntax error at or near "TRIGGER" at character 19 LINE 1: CREATE OR REPLACE TRIGGER patient_dob_in_past BEFORE UPDATE ...

Upvotes: 1

Views: 1629

Answers (2)

Vao Tsun
Vao Tsun

Reputation: 51649

try:

CREATE OR REPLACE FUNCTION patient_dob_in_past()
RETURNS TRIGGER AS $$
BEGIN
 -- check pat_dob_dt is in past --
    IF ( NEW.pat_dob_dt  > current_date - interval '16 years' ) THEN
       RAISE EXCEPTION '% must be 16 years in past', NEW.pat_dob_dt;
    END IF;
    RETURN NEW;
END;
$$ language 'plpgsql';

also https://www.postgresql.org/docs/current/static/sql-createtrigger.html

CREATE OR REPLACE TRIGGER 

will fail as it does not work with OR REPLACE - use just CREATE TRIGGER instead

also why not CHECK constraints? eg:

t=# create table q2(t timestamptz check (t < now() - '16 years'::interval));
CREATE TABLE
t=# insert into q2 select now();
ERROR:  new row for relation "q2" violates check constraint "q2_t_check"
DETAIL:  Failing row contains (2017-10-10 11:41:01.062535+00).
t=# insert into q2 select now() - '16 years'::interval;
ERROR:  new row for relation "q2" violates check constraint "q2_t_check"
DETAIL:  Failing row contains (2001-10-10 11:41:13.031769+00).
t=# insert into q2 select now() - '16 years'::interval -'1 second'::interval;
INSERT 0 1

update

In case of existing previous values that do not match check constraint - you can delay check with NOT VALID, eg:

t=# create table q2(t timestamptz);
CREATE TABLE
t=# insert into q2 select now();
INSERT 0 1
t=# alter table q2 add constraint q2c check (t < (now() - '16 years'::interval)) not valid;
ALTER TABLE
t=# insert into q2 select now();
ERROR:  new row for relation "q2" violates check constraint "q2c"
DETAIL:  Failing row contains (2017-10-10 11:56:02.705578+00).

Upvotes: 3

Dmitry Umarov
Dmitry Umarov

Reputation: 476

You missed semicolon at the end of the line.

RAISE EXCEPTION '% must be 16 years in past', NEW.pat_dob_dt;

Upvotes: 1

Related Questions