Skip site navigation (1) Skip section navigation (2)

FW: table inheritance and polymorphic functions

From: David(dot)Ventimiglia(at)wellsfargo(dot)com
To: pgsql-novice(at)postgresql(dot)org
Subject: FW: table inheritance and polymorphic functions
Date: 2004-07-12 16:11:18
Message-ID: FE4FB40DD8E71B438560737A5C58FB7904AFEAFD@msgsw55cacah26.wellsfargo.com (view raw or flat)
Thread:
Lists: pgsql-novice
Creating a "toy" example to test it out, I have these tables:
  create table cities (
    name          text,
    population    float,
    altitude      int
  )

  create table capitals (
    state         char(2)
  ) inherits (cities)

with this data:
  insert into cities(name, population, altitude) values('Detroit', 10, 10);
  insert into cities(name, population, altitude) values('Boston', 20, 20);
  insert into cities(name, population, altitude) values('San Francisco', 30,
30);
  insert into capitals(name, population, altitude, state) values('Denver',
40, 40, 'CO');
  insert into capitals(name, population, altitude, state)
values('Sacramento', 50, 50, 'CA');

and these functions:
  create function change_population(cities)
  returns float
  as '
    select $1.population * 2 as population;
  '
  language sql;

  create function change_population(capitals)
  returns float
  as '
    select $1.population * -2 as population;
  '
  language sql;

I want to call the function in some way so that the correct implementation
is invoked depending on the type of the row, either CITIES or CAPITALS.
Something like this:
  select name, change_population(cities.*)
  from cities;

I want to get results like this:
  Detroit          20
  Boston           40
  San Francisco    60
  Denver          -80
  Sacramento     -100

Instead I get results like this:
  Detroit          20
  Boston           40
  San Francisco    60
  Denver           80
  Sacramento      100

So the problem I'm having is that PostgreSQL seems to be dispatching to the
implementation of the function based on the declared type of the argument
passed in during invocation (e.g., "cities"), rather than on the actual
types of the various rows (i.e., some rows are "cities" while others are of
a derived type, "capitals").  It's likely I'm using it incorrectly.  Any
advice on how to use it correctly?  Thanks!

Regards,
David

-----Original Message-----
From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
Sent: Friday, July 09, 2004 8:16 PM
To: David(dot)Ventimiglia(at)wellsfargo(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: [NOVICE] table inheritance and polymorphic functions 


David(dot)Ventimiglia(at)wellsfargo(dot)com writes:
> Is there any way to set up overloaded functions whose parameters are
> composite types (i.e., row types) within a table inheritance hierarchy, so
> that postgresql dispatches to the correct function depending on the actual
> type of its argument, in an "object-oriented" fashion?

AFAIK this has always worked: functions on parent tables can be called
on rows of child tables.  What problem are you hitting exactly (and
which PG version are you trying it in)?

			regards, tom lane

pgsql-novice by date

Next:From: Andy HarrisonDate: 2004-07-12 17:28:40
Subject: using 'count' to show number of dupes
Previous:From: Bruno Wolff IIIDate: 2004-07-12 14:37:33
Subject: Re: Weird join...

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group