Re: array of domain types

From: Thom Brown <thom(at)linux(dot)com>
To: konstantin knizhnik <k(dot)knizhnik(at)postgrespro(dot)ru>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array of domain types
Date: 2016-06-02 09:29:28
Message-ID: CAA-aLv6dQ1S+Wze3AZsbstW0TiP8O9q2AAg0nM22vekOHziktw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2 June 2016 at 10:13, konstantin knizhnik <k(dot)knizhnik(at)postgrespro(dot)ru>
wrote:

>
> On Jun 1, 2016, at 4:37 PM, Thom Brown wrote:
>
> On 1 June 2016 at 14:20, Konstantin Knizhnik <k(dot)knizhnik(at)postgrespro(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message konstantin knizhnik 2016-06-02 10:40:45 Re: array of domain types
Previous Message konstantin knizhnik 2016-06-02 09:13:51 Re: array of domain types