Re: rules / triggers on insert. why after?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ahti Legonkov <lego(at)127(dot)0(dot)0(dot)1>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: rules / triggers on insert. why after?
Date: 2002-07-12 15:07:29
Message-ID: 9792.1026486449@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Ahti Legonkov <lego(at)127(dot)0(dot)0(dot)1> writes:
> CREATE TABLE reo (
> "REO_ID" int4 DEFAULT nextval('"REO_ID_seq"'::text) NOT NULL,
> "TYPE" varchar(64) NOT NULL,
> CONSTRAINT "REO_ID_reo_ukey" UNIQUE ("REO_ID"),
> CONSTRAINT reo_pkey PRIMARY KEY ("REO_ID")
> );

> CREATE TABLE lreo (
> "CITY" varchar(64),
> "STREET" varchar(64),
> "PRICE" int4,
> "REO_ID" int4 DEFAULT currval('"REO_ID_seq"'::text),
> CONSTRAINT "REO_ID_land_reo_ukey" UNIQUE ("REO_ID"),
> CONSTRAINT "fk_LREO_REO_ID" FOREIGN KEY ("REO_ID") REFERENCES reo
> ("REO_ID") ON DELETE NO ACTION ON UPDATE NO ACTION NOT DEFERRABLE
> INITIALLY IMMEDIATE
> );

That default for reo_id is too fragile to consider using in any case.
You are making way too many assumptions about when defaults will be
evaluated relative to other actions (such as rule/trigger firings).

I'd suggest that you have no default for column reo_id, and instead
have a BEFORE INSERT trigger for lreo that (a) inserts a row into reo
and (b) sets new.reo_id to the inserted key (which you could get from
currval at that point).

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jie Liang 2002-07-12 15:29:55 Re: [SQL] Please, HELP! Why is the query plan so wrong???
Previous Message Dmitry Tkach 2002-07-12 14:34:27 Re: [SQL] Please, HELP! Why is the query plan so wrong???