Polymorphic function calls

From: knizhnik <knizhnik(at)garret(dot)ru>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Polymorphic function calls
Date: 2013-12-29 16:44:47
Message-ID: 52C0517F.2020706@garret.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Is there any chance to implement polymorphic calls in PostgreSQL?
Consider the following definitions:

create table base_table (x integer, y integer);
create table derived_table (z integer) inherits (base_table);
create function volume(r base_table) returns integer as $$ begin return
r.x*r.y; end; $$ language plpgsql strict stable;
create function volume(r derived_table) returns integer as $$ begin
return r.x*r.y*r.z; end; $$ language plpgsql strict stable;
insert into base_table values (1,2);
insert into derived_table values (3,4,5);

postgres=# select * from base_table;
x | y
---+---
1 | 2
3 | 4
(2 rows)

postgres=# select volume(r) from base_table r;
volume
--------
2
12
(2 rows)

postgres=# select volume(r) from only base_table r union all select
volume(r_1) from only derived_table r_1;
volume
--------
2
60
(2 rows)

Execution plans of first and second queries are very similar.
The difference is that type of r_1 in first query is "base_table".
It is obvious that query should return fixed set of columns, so

select * from base_table;

can not return "z" column.
But passing direved_table type instead of base_table type to volume()
function for record belonging to derived_table seems to be possible and
not contradicting something, isn't it?

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2013-12-29 17:09:30 Re: INSERT...ON DUPLICATE KEY LOCK FOR UPDATE
Previous Message Heikki Linnakangas 2013-12-29 16:18:46 Re: INSERT...ON DUPLICATE KEY LOCK FOR UPDATE