Create array of custom domain postgres

前端 未结 1 667
慢半拍i
慢半拍i 2021-01-12 02:02

Because of the inherit limitations of enum (you can\'t add values to the enum from within a function), I\'m switching to custom domains with a check constraint verifying the

相关标签:
1条回答
  • 2021-01-12 02:32

    Another possible workaround is:

    CREATE TYPE foo_tup AS (item foo);
    

    Domain types can wrapped in tuples like this and that gives you an array constructor. The downside is now you probably want to create casts:

    select array[row('foo')::foo_tup, row('bar')];
    

    For example you could create a function and a cast:

    create function foo_tup(foo) returns foo_tup language sql as $$
        select row($1)::foo_tup;
    $$ immutable;
    create function foo(foo_tup) returns foo language sql as $$
         select $1.item;
    $$;
    create cast (foo as foo_tup) with function foo_tup(foo);
    create cast (foo_tup as foo) with function foo(foo_tup);
    

    Then aggregation becomes easy:

    select array_agg(myfoo::foo_tup) from my_table; 
    

    though you get extra parentheses.

    0 讨论(0)
提交回复
热议问题