pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Error from trigger
5+ messages / 2 participants
[nested] [flat]

* Error from trigger
@ 2005-12-07 23:06 Leif B. Kristensen <leif@solumslekt.org>
  2005-12-07 23:23 ` Re: Error from trigger Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Leif B. Kristensen @ 2005-12-07 23:06 UTC (permalink / raw)
  To: pgsql-sql

Hello,
I have a trigger that will delete records referring to an "events" table 
upon deletion. I have used it without problems for a number of times:

CREATE OR REPLACE FUNCTION delete_event_cascade() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM event_citations WHERE event_fk = OLD.event_id;
    DELETE FROM participants WHERE event_fk = OLD.event_id;
    RETURN OLD;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER delete_event_cascade
BEFORE DELETE ON events
    FOR EACH ROW EXECUTE PROCEDURE delete_event_cascade();

The event_citations table is a cross-reference between events and 
sources, and goes like this:

CREATE TABLE event_citations (
    event_fk            INTEGER REFERENCES events (event_id),
    source_fk           INTEGER REFERENCES sources (source_id),
    PRIMARY KEY (event_fk, source_fk)
);

I'm doing a little cleanup, and perform this query:

pgslekt=> select * from event_citations where source_fk=553;
 event_fk | source_fk
----------+-----------
     2600 |       553
     2592 |       553
     2817 |       553
    19919 |       553
    19920 |       553
(5 rader)

Then I do an adjustment in the "sources" table:

pgslekt=> update sources set 
parent_id=553,small_text='',large_text='23.04.1745 ved Solum: \"Abraham 
Nielsøn Aafos 49 aar 5 dage\"' where source_id=554;
UPDATE 1

And then, as I try to delete event #2600, I get the following message:

pgslekt=> delete from events where event_id=2600;
ERROR: relation with OID 1141502 does not exist
CONTEXT: SQL statement "DELETE FROM event_citations WHERE event_fk= $1 "
PL/pgSQL function "delete_event_cascade" line 2 at SQL statement

I run a quick check:

pgslekt=> select * from event_citations where event_fk=2600;
 event_fk | source_fk
----------+-----------
(0 rader)

The record seems to have disappeared into thin air. There has not been 
performed any inserts or updates in the database between the 'update' 
and the 'delete' above. And the event won't go away. 

This is hardly a practical problem, because an event that isn't linked 
to a "person" through the "participants" table will never print 
anywhere, and the referring "participant" disappeared. But I don't like 
it anyway.

I'm running PostgreSQL 8.0.4.
-- 
Leif Biberg Kristensen | Registered Linux User #338009
http://solumslekt.org/ | Cruising with Gentoo/KDE



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error from trigger
  2005-12-07 23:06 Error from trigger Leif B. Kristensen <leif@solumslekt.org>
@ 2005-12-07 23:23 ` Tom Lane <tgl@sss.pgh.pa.us>
  2005-12-07 23:43   ` Re: Error from trigger Leif B. Kristensen <leif@solumslekt.org>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2005-12-07 23:23 UTC (permalink / raw)
  To: Leif B. Kristensen <leif@solumslekt.org>; +Cc: pgsql-sql

"Leif B. Kristensen" <leif@solumslekt.org> writes:
> I have a trigger that will delete records referring to an "events" table 
> upon deletion. I have used it without problems for a number of times:

> CREATE OR REPLACE FUNCTION delete_event_cascade() RETURNS TRIGGER AS $$
> BEGIN
>     DELETE FROM event_citations WHERE event_fk = OLD.event_id;
>     DELETE FROM participants WHERE event_fk = OLD.event_id;
>     RETURN OLD;
> END;
> $$ LANGUAGE plpgsql;

> CREATE TRIGGER delete_event_cascade
> BEFORE DELETE ON events
>     FOR EACH ROW EXECUTE PROCEDURE delete_event_cascade();

> The event_citations table is a cross-reference between events and 
> sources, and goes like this:

> CREATE TABLE event_citations (
>     event_fk            INTEGER REFERENCES events (event_id),
>     source_fk           INTEGER REFERENCES sources (source_id),
>     PRIMARY KEY (event_fk, source_fk)
> );

Is there a reason you don't just mark the FK reference as ON DELETE
CASCADE, rather than using a handwritten trigger?

> And then, as I try to delete event #2600, I get the following message:

> pgslekt=> delete from events where event_id=2600;
> ERROR: relation with OID 1141502 does not exist
> CONTEXT: SQL statement "DELETE FROM event_citations WHERE event_fk= $1 "
> PL/pgSQL function "delete_event_cascade" line 2 at SQL statement

Offhand this looks like you might have dropped and recreated the
event_citations table?  If so it's just the known problem that
plpgsql caches plans and doesn't throw them away when the referenced
objects change.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error from trigger
  2005-12-07 23:06 Error from trigger Leif B. Kristensen <leif@solumslekt.org>
  2005-12-07 23:23 ` Re: Error from trigger Tom Lane <tgl@sss.pgh.pa.us>
@ 2005-12-07 23:43   ` Leif B. Kristensen <leif@solumslekt.org>
  2005-12-08 04:11     ` Re: Error from trigger Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Leif B. Kristensen @ 2005-12-07 23:43 UTC (permalink / raw)
  To: pgsql-sql

On Thursday 08 December 2005 00:23, Tom Lane wrote:
>Is there a reason you don't just mark the FK reference as ON DELETE
>CASCADE, rather than using a handwritten trigger?

I could have done that, of course. I'm still a little shaky on "best 
practice" with these things. Besides, I haven't found out yet how to 
alter the table to make the reference cascading. And I wanted to 
experiment with simple triggers.

>Offhand this looks like you might have dropped and recreated the
>event_citations table?  If so it's just the known problem that
>plpgsql caches plans and doesn't throw them away when the referenced
>objects change.

Right on target. Thank you. A few days ago, as I have already related on 
this list, I did the following to fix a bad design with inherited 
tables:

pgslekt=> create table event_cits (
pgslekt(> event_fk integer references events (event_id),
pgslekt(> source_fk integer references sources (source_id),
pgslekt(> PRIMARY KEY (event_fk, source_fk)
pgslekt(> );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"event_cits_pkey" for table "event_cits"
CREATE TABLE
pgslekt=> insert into event_cits (select event_fk, source_fk from 
event_citations);
INSERT 0 29139
pgslekt=> drop table event_citations cascade;
NOTICE:  drop cascades to rule _RETURN on view event_notes
NOTICE:  drop cascades to view event_notes
DROP TABLE
pgslekt=> drop table citations;
DROP TABLE
pgslekt=> alter table event_cits rename to event_citations;
ALTER TABLE
pgslekt=> \i views_and_functions.sql

I've run a VACUUM FULL ANALYZE, but that doesn't seem to cure the 
situation. Is there a way to fix it, short of a full dump, drop, and  
reload?
-- 
Leif Biberg Kristensen | Registered Linux User #338009
http://solumslekt.org/ | Cruising with Gentoo/KDE



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error from trigger
  2005-12-07 23:06 Error from trigger Leif B. Kristensen <leif@solumslekt.org>
  2005-12-07 23:23 ` Re: Error from trigger Tom Lane <tgl@sss.pgh.pa.us>
  2005-12-07 23:43   ` Re: Error from trigger Leif B. Kristensen <leif@solumslekt.org>
@ 2005-12-08 04:11     ` Tom Lane <tgl@sss.pgh.pa.us>
  2005-12-10 10:07       ` Re: Error from trigger Leif B. Kristensen <leif@solumslekt.org>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2005-12-08 04:11 UTC (permalink / raw)
  To: Leif B. Kristensen <leif@solumslekt.org>; +Cc: pgsql-sql

"Leif B. Kristensen" <leif@solumslekt.org> writes:
> On Thursday 08 December 2005 00:23, Tom Lane wrote:
>> Offhand this looks like you might have dropped and recreated the
>> event_citations table?  If so it's just the known problem that
>> plpgsql caches plans and doesn't throw them away when the referenced
>> objects change.

> Right on target. Thank you. A few days ago, as I have already related on 
> this list, I did the following to fix a bad design with inherited 
> tables:
> ...
> I've run a VACUUM FULL ANALYZE, but that doesn't seem to cure the 
> situation. Is there a way to fix it, short of a full dump, drop, and  
> reload?

Just starting a fresh session should make the problem go away, or if
that's not practical update the function definition using ALTER FUNCTION
or CREATE OR REPLACE FUNCTION.  (You don't need to actually *change*
anything about the function, just issue a command that could change it.)

If that doesn't make the error go away then we need to look more
closely at what's causing it.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error from trigger
  2005-12-07 23:06 Error from trigger Leif B. Kristensen <leif@solumslekt.org>
  2005-12-07 23:23 ` Re: Error from trigger Tom Lane <tgl@sss.pgh.pa.us>
  2005-12-07 23:43   ` Re: Error from trigger Leif B. Kristensen <leif@solumslekt.org>
  2005-12-08 04:11     ` Re: Error from trigger Tom Lane <tgl@sss.pgh.pa.us>
@ 2005-12-10 10:07       ` Leif B. Kristensen <leif@solumslekt.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Leif B. Kristensen @ 2005-12-10 10:07 UTC (permalink / raw)
  To: pgsql-sql

On Thursday 08 December 2005 05:11, Tom Lane wrote:
>Just starting a fresh session should make the problem go away, or if
>that's not practical update the function definition using ALTER
> FUNCTION or CREATE OR REPLACE FUNCTION.  (You don't need to actually
> *change* anything about the function, just issue a command that could
> change it.)
>
>If that doesn't make the error go away then we need to look more
>closely at what's causing it.

I did an "/etc/init.d/postgresql restart" and the problem went away.

Thanks again.
-- 
Leif Biberg Kristensen | Registered Linux User #338009
http://solumslekt.org/ | Cruising with Gentoo/KDE



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2005-12-10 10:07 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-12-07 23:06 Error from trigger Leif B. Kristensen <leif@solumslekt.org>
2005-12-07 23:23 ` Tom Lane <tgl@sss.pgh.pa.us>
2005-12-07 23:43   ` Leif B. Kristensen <leif@solumslekt.org>
2005-12-08 04:11     ` Tom Lane <tgl@sss.pgh.pa.us>
2005-12-10 10:07       ` Leif B. Kristensen <leif@solumslekt.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox