October 16th, 2010 by depesz | Tags: , , , , , , | 2 comments »
Did it help? If yes - maybe you can help me?

On 26th of September, Peter Eisentraut committed patch which adds:

Add ALTER TYPE ... ADD/DROP/ALTER/RENAME ATTRIBUTE
 
Like with tables, this also requires allowing the existence of
composite types with zero attributes.
 
reviewed by KaiGai Kohei

Custom types, do not get much coverage, but I personally like them. I much prefer to use them for SRF functions (than multiple OUT parameters).

The problem was that changing these types was never easy. you had to drop the type, and create it again.

Now, it's no longer the case – you can “easily" modify them. Why “easily" and not easily? Well, changing this datatype still required function recompilation, so it's not solving all problems. But it's a step in right direction. And besides – if you are changing definition of recordset returned from function – you're likely changing the function itself anyway.

So, let's see how it works:

$ create type test as (
    x int4,
    y int4
);
 
$ \d test
Composite type "public.test"
 Column |  Type
--------+---------
 x      | integer
 y      | integer

Now, we can:

$ ALTER TYPE test ALTER ATTRIBUTE x TYPE int8;
ALTER TYPE
 
$ ALTER TYPE test DROP ATTRIBUTE y;
ALTER TYPE
 
$ ALTER TYPE test ADD ATTRIBUTE z INT8;
ALTER TYPE
 
$ ALTER TYPE test RENAME ATTRIBUTE x TO new_x;
ALTER TYPE
 
\d test
Composite type "public.test"
 Column |  Type
--------+--------
 new_x  | bigint
 z      | bigint

So, how does it influence the functions?

Let's test.

$ create function testit() returns setof test as $$
declare
    reply test;
begin
    reply.new_x := 123;
    reply.z := 456;
    return next reply;
    return;
end;
$$ language plpgsql;
CREATE FUNCTION
 
$ select * from testit();
 new_x |  z
-------+-----
   123 | 456
(1 row)

So far so good. So, let's change the type definition, in a way that (at least theoretically) shouldn't matter for the function:

$ ALTER TYPE test add column new_column int4;
ALTER TYPE
 
$ select * from testit();
ERROR:  wrong record type supplied in RETURN NEXT
CONTEXT:  PL/pgSQL function "testit" line 7 at RETURN NEXT

Oops. But – this happens only because this particular db connection already used the function. If I disconnect, and reconnect again (without any more changes):

$ select * from testit();
 new_x |  z  | new_column 
-------+-----+------------
   123 | 456 |     [null]
(1 row)

Alternatively, I could simply reload the same function body with CREATE OR REPLACE:

$ ALTER TYPE test add attribute new_column2 int4;
ALTER TYPE
 
$ select * from testit();
ERROR:  wrong record type supplied in RETURN NEXT
CONTEXT:  PL/pgSQL function "testit" line 7 at RETURN NEXT
 
$ select * from testit();
ERROR:  wrong record type supplied in RETURN NEXT
CONTEXT:  PL/pgSQL function "testit" line 7 at RETURN NEXT
 
$ create or replace function testit() returns setof test as $$ 
declare 
    reply test; 
begin 
    reply.new_x := 123; 
    reply.z := 456; 
    return next reply; 
    return; 
end; 
$$ language plpgsql;
CREATE FUNCTION
 
$ select * from testit();
 new_x |  z  | new_column | new_column2 
-------+-----+------------+-------------
   123 | 456 |     [null] |      [null]
(1 row)

Sweet. I think that if it altering datatype would actually invalidate all previously compiled plans/functions – it would be even better, but as I wrote earlier – when you're changing datatype you're likely to be changing function body as well, so it's not a big deal.

  1. 2 comments

  2. # Thom Brown
    Oct 16, 2010

    It might be also worth mentioning that if any table is using it as a column type, the type cannot be modified.

  3. Oct 16, 2010

    Custom types, do not get much coverage, but I personally like them. I much prefer to use them for SRF functions (than multiple OUT parameters).

    Note that you can also use

    RETURNS TABLE

    . I used this a bunch in PGXN::Manager. See for example

    by_tag_json

    . I like how this keeps the implicit custom type associated with the function without requiring

    OUT

    parameters.

    —Theory

Leave a comment