Re: Re : overriding default value in inherited column (+ set_value function)

From: "Matt Magoffin" <mmagoffin(at)proxicom(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Re : overriding default value in inherited column (+ set_value function)
Date: 2001-03-20 20:26:23
Message-ID: 998e9p$1ku4$1@news.tht.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Is there any way to make use of the tableoid either as an argument to the
function or as a reference within the function (in plpgsql)? For example,
I'd like to either

CREATE TRIGGER set_default_value BEFORE INSERT
ON foo FOR EACH ROW EXECUTE PROCEDURE set_value('tableoid');

and within the function set_value():

SELECT p.relname::text FROM pg_class p WHERE p.oid = TG_ARGV[0]::oid );

- or -

CREATE TRIGGER set_default_value BEFORE INSERT
ON foo FOR EACH ROW EXECUTE PROCEDURE set_value();

and within the function set_value():

SELECT p.relname::text FROM pg_class p WHERE p.oid = NEW.tableoid;

The former produces the error

ERROR: text_oid: error in "tableoid": can't parse "tableoid"

and the later produces the error:

ERROR: record new has no field tableoid

I gather the former method is passing the string "tableoid" into the
set_value() function. I just want to be able to write one function that uses
the tableoid value to produce different results instead of unique functions
for each table I create.

-- m@

"Nico" <nicod(at)tiscalinet(dot)it> wrote in message
news:01032014024502(dot)01280(at)localhost(dot)localdomain(dot)(dot)(dot)
> Actually not possible with the "DEFAULT" sintax inside the CREATE TABLE.
> Instead, you could use explicit triggers, for example:
>
> CREATE table foo (
> "type" int2
> );
>
> CREATE table bar (
> "type" int2
> ) INHERITS (foo);
>
> CREATE TRIGGER set_default_value BEFORE INSERT
> ON foo FOR EACH ROW
> EXECUTE PROCEDURE set_value("type", 0);
>
> CREATE TRIGGER set_default_value BEFORE INSERT
> ON bar FOR EACH ROW
> EXECUTE PROCEDURE set_value("type", 1);
>
> The function set_value has to be written in C language (plpgsql lang
doesn't
> allow parameter passing for trigger functions).
>
> Has someone already written that function?
>
> regards, nico
>
>
> > From: "Matt Magoffin" <mmagoffin(at)proxicom(dot)com>
> > X-Newsgroups: comp.databases.postgresql.general
> > Subject: overriding default value in inherited column
> > Date: Mon, 19 Mar 2001 18:39:27 -0800
> >
> > Is there an easy way to override the defined default value of a column
in
> > an inherited table? For example:
> >
> > CREATE table foo (
> > "type" int2 DEFAULT 0
> > );
> >
> > CREATE table bar (
> > "type" int2 DEFAULT 1
> > ) INHERITS (foo);
> >
> > This gives the error:
> >
> > ERROR: CREATE TABLE: attribute "type" already exists in inherited schema
> >
> > which is understandable. In essence what I want to do is have each table
> > schema default to a different value.
> >
> > -- m@
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2001-03-20 20:50:20 Re: Re : overriding default value in inherited column (+ set_value function)
Previous Message chris markiewicz 2001-03-20 20:16:05 finding and removing a constraint...