Postgres / plpgsql equivalent to python's getattr() ?

From: James Robinson <jlrobins(at)socialserve(dot)com>
To: Hackers Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Postgres / plpgsql equivalent to python's getattr() ?
Date: 2011-08-03 21:19:15
Message-ID: C0122DA7-AEDD-47D3-9A36-97F4ADDB072C@socialserve.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hackers,

Python's getattr() allows for dynamic lookup of attributes on an
object, as in:

inst = MyClass(x=12, y=24)
v = getattr(inst, 'x')
assert v == 12

Oftentimes in writing data validating trigger functions, it'd be real
handy to be able to do a similar thing in plpgsql against column
values in a row or record type, such as making use of a trigger
argument for hint as what column to consider in this table's case. Oh,
to be able to do something like (toy example known to be equivalent to
a check):

CREATE OR REPLACE FUNCTION must_be_positive() RETURNS TRIGGER AS
$$
begin
if getattr(NEW, TG_ARGV[0]) <= 0
then
raise exception(TG_ARGV[0] || ' must be positive');
end if;

-- after trigger
return null;
end;
$$ LANGUAGE PLPGSQL;

A function which takes a row + a text column name, and / or a peer
function taking row + index within row would really open up plpgsql's
expressivity in cases where you're writing mainly SQL stuff, not
really wanting to go over to plpythonu or whatnot (whose description
of rows are as dicts).

Is there something in the internals which inherently prevent this? Or
am I fool and it already exists?

Not having to defer to EXECUTE would be attractive.

----
James Robinson
Socialserve.com

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2011-08-03 21:26:58 Re: cataloguing NOT NULL constraints
Previous Message Pavan Deolasee 2011-08-03 21:10:53 Re: [GENERAL] Odd VACUUM behavior when it is expected to truncate last empty pages