pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jasen Betts <jasen@xnet.co.nz>
To: pgsql-sql@postgresql.org
Subject: Re: [Trigger] Help needed with NEW.* and TG_TABLE_NAME
Date: 13 May 2010 10:51:22 GMT
Message-ID: <hsglja$ofu$1@reversiblemaps.ath.cx> (raw)
References: <hsbiuo$rbe$1@news.eternal-september.org>

On 2010-05-11, Torsten Zühlsdorff <foo@meisterderspiele.de> wrote:
> Hello,
>
> i have a problem with a trigger written in pl/pgsql.
>
> It looks like this:
>
> CREATE OR REPLACE FUNCTION versionize()
> RETURNS TRIGGER
> AS $$
> BEGIN
>
>    NEW.revision := addContentRevision (OLD.content_id, OLD.revision);
>
>    /* not working line, just a stub:
>    EXECUTE 'INSERT INTO ' || TG_TABLE_NAME  || ' SELECT $1 ' USING NEW;
>    */
>
>    RETURN NULL;
>
> END;
> $$ LANGUAGE 'plpgsql' VOLATILE;
>
> The function should be used at different tables and is invoked before
> UPDATEs. Everything what happens is the function call of
> addContentRevision. After this call all data (with the updated revision
> column) should be stored in the table as a new row.

What many people have missed is that you want to INSERT when the DML
comnabd UPDATE is used.

for things like that usually a rule is used instead, but I can see where
that may be unsuitable for your needs.  I found the following 
to work on a simple test case.


The problem is that INSERT in PLPGSQL needs a fixed table-name, and
that "EXECUTE" can't use variable-names, and further that quote_literal
doesn't convert ROW variables into something that can be used in a
VALUES clause.

so, Here's what I did.

 CREATE OR REPLACE FUNCTION versionize()
 RETURNS TRIGGER
 AS $$
 BEGIN

    -- Not havign a definition for addContentRevision
    -- I had this line commented out during testing.
    NEW.revision := addContentRevision (OLD.content_id, OLD.revision);
    
    EXECUTE 'INSERT INTO '||TG_TABLE_NAME||' SELECT (' || 
       QUOTE_LITERAL(NEW) || '::' || TG_TABLE_NAME ||').*' ;

    RETURN NULL;

 END;
 $$ LANGUAGE PLPGSQL VOLATILE;

I take NEW, convert it to a quoted literal so I can use it in EXECUTE, cast it 
to the apreopreiate row type and split it into columns using SELECT
and .*. That gets inserted.

you should probably use QUOTE_IDENT on the TG_TABLE_NAME and possibly
also use similarly quoted TG_SCHEMA_NAME 




view thread (9+ messages)  latest in thread

Message-ID: <hsglja$ofu$1@reversiblemaps.ath.cx>
Permalink:  ../hsglja$ofu$1@reversiblemaps.ath.cx/
Also on:    postgresql.org/message-id/hsglja$ofu$1@reversiblemaps.ath.cx

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jasen@xnet.co.nz
  Subject: Re: [Trigger] Help needed with NEW.* and TG_TABLE_NAME
  In-Reply-To: <hsglja$ofu$1@reversiblemaps.ath.cx>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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