PROPOSAL: Typmods for functions

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: PROPOSAL: Typmods for functions
Date: 2009-10-08 08:02:15
Message-ID: 162867790910080102m5e641d16h4a916eb6ed440d4c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello,

this proposal is related to discuss about enhancing "any" type and
using this type in other PL than C. Result from this discuss was:
* add synonym for type "any" ~ ANY TYPE
* add possibility select type from other referenced type ~ "TYPE AS
variable (This syntax is impossible, but we can use similar "WITH TYPE
AS variable" .. this isn't area of this proposal).

For implementation referenced types we need a storage. We could to use
typmodes. What more, typmodes should be nice feature mainly for
NUMERIC like types.

==steps==
1. add columns int prorettypmod and int[] proargtypmodes to pg_proc
2. enhance casting arguments to target types - add coerce_type_typmod
call to make_fn_arguments (based on proargtypmodes). There are two
possible behave: like explicit or implicit cast. In this case I prefer
behave like implicit cast - it's protection under silent cut strings,
...

CREATE OR REPLACE FUNCTION foo(a varchar(3))
RETURNS varchar AS $$
SELECT $1;
$$ LANGUAGE sql;

SELECT foo('abcd')
ERROR: value too long for type character varying(3)

I don't expect compatibility problems - every custom functions created
in older versions has typmod -1 for every parameter.

3. PL handler is responsible for correct cast to result typmod. I am
not sure, if we have to use implicit or explicit cast on output. I
incline to the opinion, that implicit cast is better here.

CREATE OR REPLACE FUNCTION foo(a varchar)
RETURNS varchar(2) AS $$
SELECT $1;
$$ LANGUAGE sql;

SELECT foo('abcde');
foo
-----
ab
(1 row)

Compatibility problems are not expected too.

3. support for dumps, ...

Because typmod conversions are fixed in plan, isn't possible alter typmod later.

Comments?

Regards
Pavel Stehule

Browse pgsql-hackers by date

  From Date Subject
Next Message Dimitri Fontaine 2009-10-08 08:28:53 Re: Concurrency testing
Previous Message Simon Riggs 2009-10-08 07:04:35 Re: Hot Standby 0.2.1