Re: pl/pgsql RECORD data type, how to access to the values

From: "Guillaume Bog" <guibog(at)gmail(dot)com>
To: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pl/pgsql RECORD data type, how to access to the values
Date: 2008-04-05 13:28:26
Message-ID: bc5951d00804050628q75676547i4bb695b854c44eb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Apr 5, 2008 at 4:05 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:

> Hello
>
>
>
>
> On 05/04/2008, Guillaume Bog <guibog(at)gmail(dot)com> wrote:
> > Hello,
> >
> > I'm writing a trigger in pl/pgSQL and I'd like to pass one column name as
> > argument to the trigger function.
> >
> > Provided my table has only one column named 'id', I can do easilly
> >
> > CREATE FUNCTION ft() RETURNS trigger AS $$
> > BEGIN
> > RAISE NOTICE 'It works:%', OLD.id;
> > END
> > $$ LANGUAGE plpgsql;
> >
> > But I'd like to do
> >
> > CREATE FUNCTION ft() RETURNS trigger AS $$
> > DECLARE
> > col VARCHAR;
> > BEGIN
> > col = TG_ARGV[0]
> > RAISE NOTICE 'This does not works:%', OLD.col
> > RAISE NOTICE 'This also does not works:%', OLD[col]
> > END
> > $$ LANGUAGE plpgsql;
> >
> > I tried OLD.(col) and other tricks, like "SELECT INTO" or "EXECUTE", and I
> > checked the docs.
>
> It's not possible in plpgsql. You have to use plperl, pltcl or plpython.

Ok, thanks. I may keep my code in the previous state instead because I
only have a little bit of duplication that currently still fits on one
screen, and it seems preferable to use pl/pgsql in my case.

Another question that is puzzling me:

I want a table to be "read-only", so I raise exceptions with a before
trigger on update, insert and delete. It works well.

This read-only table is modified (delete + insert) by a trigger
function set on another table, but this second trigger calls the first
and I can modify my read-only table. I'd like my "read-only" trigger
to be aware that the modification call on the read-only table comes
from the second trigger. Is it possible?

I have read in some places that I should use a rule instead, but I
never used them and it seems complex. I would prefer not to set up
complex access rules with GRANT and REVOKE because my access rules in
simple and works now. The best solution I can think of so far is to
have the client application work with a view, but having this behavior
fully managed through triggers would be more natural and I fear I
missed something in the docs.

> Regards
> Pavel Stehule
>
> >
> >
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jan Wieck 2008-04-05 14:00:04 Re: [Slony1-general] ERROR: XX000: cache lookup failed for relation
Previous Message Rusty Conover 2008-04-05 13:11:38 Re: pg_dump and schemas