Referential Integrity

Поиск
Список
Период
Сортировка
Искать

Referential Integrity

От:
"Alexander Stanier" <alexander.stanier@egsgroup.com>
Дата:

Dear pgsql-sql,

(Third time of trying to post this message - apologies if any previous attempts subsequently succeed!! Ignore them this one states the case more clearly)

I have the following scenario in pg7.3.1.:

CREATE TABLE alex (aid varchar(40) NOT NULL,
                   bid varchar(100) NOT NULL,
                   itemdesc varchar(100),
        CONSTRAINT alex_pkey PRIMARY KEY (aid, bid));

CREATE TABLE alexette (aid varchar(40) NOT NULL,
                       bid varchar(100) NOT NULL,
                       vcode varchar(1),
            CONSTRAINT alexette_pkey PRIMARY KEY (aid, bid));

ALTER TABLE alexette
ADD CONSTRAINT alexette_fk_1 FOREIGN KEY (aid,bid)
    REFERENCES alex (aid,bid)
    ON DELETE CASCADE;

INSERT INTO alex VALUES ('1','1','OneOne');

INSERT INTO alexette VALUES ('1','1','V');

CREATE FUNCTION test() RETURNS INT4 AS '
DECLARE
    v_return INTEGER;
BEGIN
  DELETE FROM alex
        WHERE aid = ''1''
          AND bid = ''1'';

  INSERT INTO alex (aid,bid,itemdesc)
            VALUES (''1'',''1'',''OneOne'');

  INSERT INTO alexette (aid,bid,vcode)
                VALUES (''1'',''1'',''V'');

  RETURN 0;
END;
' LANGUAGE 'plpgsql';

Please could someone tell me why running the function test() causes the following error:

"Cannot insert a duplicate key into unique index alexette_pkey"

The foreign key with "on delete cascade" should ensure that the delete statement within this function should delete the records from both alex and alexette. Indeed a simple "delete from alex" demonstrates that this does indeed work. However, it appears that within the function this is not happening or that the insert statements cannot see the full extent of the changes made by the delete statement. Whichever, surely this is wrong? Or am I missing something?

Regards,

Alexander Stanier.
mailto:alex@egsgroup.com

Re: Referential Integrity

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Alexander Stanier"  writes:
> [ expected ON DELETE CASCADE doesn't seem to happen in this context: ]

> CREATE FUNCTION test() RETURNS INT4 AS '
> DECLARE
>     v_return INTEGER;
> BEGIN
>   DELETE FROM alex
>         WHERE aid =3D ''1''
>           AND bid =3D ''1'';

>   INSERT INTO alex (aid,bid,itemdesc)
>             VALUES (''1'',''1'',''OneOne'');

>   INSERT INTO alexette (aid,bid,vcode)
>                 VALUES (''1'',''1'',''V'');

>   RETURN 0;
> END;
> ' LANGUAGE 'plpgsql';

The cascaded deletes will be implemented at end of statement --- which
I believe is always taken to be the end of the current interactive
statement, ie, the "SELECT test()" you typed.  So they haven't happened
yet at the time the function tries to do the inserts.

This is probably a bug, but IIRC, it's not entirely obvious what to do
instead. I seem to recall some inconclusive discussions in pgsql-hackers
about designing a better rule for when to fire the RI actions.  Check
the list archives for details.
		regards, tom lane

Re: Referential Integrity

От:
Stephan Szabo <sszabo@megazone23.bigpanda.com>
Дата:
On Mon, 10 Feb 2003, Tom Lane wrote:

> "Alexander Stanier"  writes:
> > [ expected ON DELETE CASCADE doesn't seem to happen in this context: ]
>
> > CREATE FUNCTION test() RETURNS INT4 AS '
> > DECLARE
> >     v_return INTEGER;
> > BEGIN
> >   DELETE FROM alex
> >         WHERE aid =3D ''1''
> >           AND bid =3D ''1'';
>
> >   INSERT INTO alex (aid,bid,itemdesc)
> >             VALUES (''1'',''1'',''OneOne'');
>
> >   INSERT INTO alexette (aid,bid,vcode)
> >                 VALUES (''1'',''1'',''V'');
>
> >   RETURN 0;
> > END;
> > ' LANGUAGE 'plpgsql';
>
> The cascaded deletes will be implemented at end of statement --- which
> I believe is always taken to be the end of the current interactive
> statement, ie, the "SELECT test()" you typed.  So they haven't happened
> yet at the time the function tries to do the inserts.
>
> This is probably a bug, but IIRC, it's not entirely obvious what to do
> instead. I seem to recall some inconclusive discussions in pgsql-hackers
> about designing a better rule for when to fire the RI actions.  Check
> the list archives for details.

Yeah, I don't think we came to a conclusion.  Looking at SQL99, the
"correct"  behavior seems to be that the actions occur as part of the
actual delete or update action so that effectively at the "same" point in
time the dependant rows are changed, using (I think) 14.14 and 14.20 as a
guide.  This is significantly different from what we do AFAICT though. :(
(For example, I'm not sure if 14.14 implies that after triggers from the
statement inside the function to occur as part of that statement rather
than after the function ends)


Re: Referential Integrity

От:
"Alexander Stanier" <alexander.stanier@egsgroup.com>
Дата:

Tom,

Thanks for your reply. I did find some correspondance between Stephan Szabo mailto:sszabo@megazone23.bigpanda.com and Jan Wieck mailto:janwieck@Yahoo.com entitled "Subject: Re: Questions on RI spec (poss. bugs)".

I can see that if the delete statement called a function which deleted the same record then it might cause problems. It does however seem to me that if a constraint is not deferred then the statement executor should see results of that command, otherwise the executor has lost consistency in the transaction. The docs do say "A constraint that is not deferrable will be checked immediately after every command." But, I guess it is a question of, having checked the constraint - when to fire the "on cascade delete" trigger? I am not sure exactly what SQL99 says, but the way it works seems wrong to me.

I guess I could rewrite the code to update the relevant records instead of deleting and re-inserting - but then this becomes more complicated. If you want to replace a whole lot of related records it is much easier to do one delete and then reinsert them.

Had a look through the FAQs and TODO, but can't see anything related to this problem. I'll see what replies I get from this before reporting it as a bug.

Regards,

Alex.

-----Original Message-----
From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: 10 February 2003 15:10
To: Alexander Stanier
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Referential Integrity

"Alexander Stanier" <alexander.stanier@egsgroup.com> writes:
> [ expected ON DELETE CASCADE doesn't seem to happen in this context: ]

> CREATE FUNCTION test() RETURNS INT4 AS '
> DECLARE
>     v_return INTEGER;
> BEGIN
>   DELETE FROM alex
>         WHERE aid =3D ''1''
>           AND bid =3D ''1'';

>   INSERT INTO alex (aid,bid,itemdesc)
>             VALUES (''1'',''1'',''OneOne'');

>   INSERT INTO alexette (aid,bid,vcode)
>                 VALUES (''1'',''1'',''V'');

>   RETURN 0;
> END;
> ' LANGUAGE 'plpgsql';

The cascaded deletes will be implemented at end of statement --- which
I believe is always taken to be the end of the current interactive
statement, ie, the "SELECT test()" you typed.  So they haven't happened
yet at the time the function tries to do the inserts.

This is probably a bug, but IIRC, it's not entirely obvious what to do
instead. I seem to recall some inconclusive discussions in pgsql-hackers
about designing a better rule for when to fire the RI actions.  Check
the list archives for details.

                        regards, tom lane

FAQ