Re: Record as a parameter to a function

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Carlos Correia <carlos(at)m16e(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Record as a parameter to a function
Date: 2005-04-22 02:18:11
Message-ID: 20050422021811.GA44742@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Apr 22, 2005 at 01:44:24AM +0100, Carlos Correia wrote:
>
> carlos=# insert into test (c1) values( 'test');
> ERROR: NEW used in query that is not in a rule
> CONTEXT: PL/pgSQL function "tftest" line 2 at perform

PostgreSQL 8.0 and later have improved support for composite types.
Here's the example you posted, slightly rewritten:

CREATE TABLE test (c1 text);

CREATE FUNCTION test1(InRec test) RETURNS boolean AS $$
BEGIN
RAISE NOTICE '%', InRec.c1;
RETURN TRUE;
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION tftest() RETURNS trigger AS $$
BEGIN
PERFORM test1(NEW);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER TriggerTest AFTER INSERT ON test
FOR EACH ROW EXECUTE PROCEDURE tftest();

If I execute the above statements in an 8.0.2 database, I get the
following when I insert a new record:

test=> INSERT INTO test (c1) VALUES ('Test');
NOTICE: Test
CONTEXT: SQL statement "SELECT test1( $1 )"
PL/pgSQL function "tftest" line 2 at perform
INSERT 0 1

In psql you can change the verbosity so you don't see the context
messages:

test=> \set VERBOSITY terse
test=> INSERT INTO test (c1) VALUES ('Test');
NOTICE: Test
INSERT 0 1

Hope this helps.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John Browne 2005-04-22 02:24:49 Table modifications with dependent views - best practices?
Previous Message Steve - DND 2005-04-22 00:56:41 Re: timezone() with timeofday() converts the wrong direction?