On 2 June 2016 at 10:13, konstantin knizhnik <k.knizh...@postgrespro.ru>
wrote:

>
> On Jun 1, 2016, at 4:37 PM, Thom Brown wrote:
>
> On 1 June 2016 at 14:20, Konstantin Knizhnik <k.knizh...@postgrespro.ru>
> wrote:
>
>> I wonder why domain types can not be used for specification of array
>> element:
>>
>> create domain objref as bigint;
>> create table foo(x objref[]);
>> ERROR:  type "objref[]" does not exist
>> create table foo(x bigint[]);
>> CREATE TABLE
>>
>> Is there some principle problem here or it is just not implemented?
>
>
> It's not implemented, but patches welcome.
>
> Thom
>
>
> The patch is trivial:  just use typbasetype in get_array_type if typtype
> is TYPTYPE_DOMAIN:
>
> diff --git a/src/backend/utils/cache/lsyscache.c
> b/src/backend/utils/cache/lsyscache.c
> index cb26d79..ecfbb20 100644
> --- a/src/backend/utils/cache/lsyscache.c
> +++ b/src/backend/utils/cache/lsyscache.c
> @@ -2486,7 +2486,18 @@ get_array_type(Oid typid)
>         if (HeapTupleIsValid(tp))
>         {
>                 result = ((Form_pg_type) GETSTRUCT(tp))->typarray;
> -               ReleaseSysCache(tp);
> +               if (result == InvalidOid && ((Form_pg_type)
> GETSTRUCT(tp))->typtype == TYPTYPE_DOMAIN) {
> +                       typid = ((Form_pg_type)
> GETSTRUCT(tp))->typbasetype;
> +                       ReleaseSysCache(tp);
> +                       tp = SearchSysCache1(TYPEOID,
> ObjectIdGetDatum(typid));
> +                       if (HeapTupleIsValid(tp))
> +                       {
> +                               result = ((Form_pg_type)
> GETSTRUCT(tp))->typarray;
> +                               ReleaseSysCache(tp);
> +                       }
> +               } else {
> +                       ReleaseSysCache(tp);
> +               }
>         }
>         return result;
>  }
>
>
> Any problems with it?
>
>
Yes, it doesn't work:

# CREATE DOMAIN teenager AS int CHECK (VALUE BETWEEN 13 AND 19);
CREATE DOMAIN

# SELECT 14::teenager;
 teenager
----------
       14
(1 row)

# SELECT 20::teenager;
ERROR:  value for domain teenager violates check constraint "teenager_check"

# SELECT '{14,20}'::teenager[];
 teenager
----------
 {14,20}
(1 row)

That last one should fail.

Thom

Reply via email to