Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qibal-00797A-QH for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Sep 2023 14:20:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qibaj-00AGjC-El for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Sep 2023 14:20:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qibaj-00AGiy-1b for pgsql-hackers@lists.postgresql.org; Tue, 19 Sep 2023 14:20:41 +0000 Received: from mail-yw1-x112e.google.com ([2607:f8b0:4864:20::112e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qibaf-0067fV-Ks for pgsql-hackers@postgresql.org; Tue, 19 Sep 2023 14:20:40 +0000 Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-59bebd5bdadso59078417b3.0 for ; Tue, 19 Sep 2023 07:20:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=supabase.io; s=google; t=1695133235; x=1695738035; darn=postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=+pcq0CQt0VAYlA0JpP6GnNsa4rpicbdrADXBAMMSs6M=; b=dFKETyrw+DrsbVGeLjVLVM2VvnjRHWTWLQsNNxUvdJyrZ0jr10cUhas9qdIRbaOv8e 7hiYX17sIn3D+L6YxdxHMIMqAoWfn3PC9AFU0/FIcPXavwWYuFJ/aqYKqxkoSYDsjUgw JCjkde6wuB2U38h5M7Jnc8p+yBL2FK9G/QuIF2z+ztNuwFiNMJ6WJI0ZLbth26oWOGip s5qHS0gH+eXBABIbttaabm2y1mPC4YbBP/daHJvdBhXJt/v3GJI/Y1X/Cz7YO4qy96ye TLmx3uSJn/HwRSX728l1kO+f59LyUGM2BhFuRDWpa31FCZjRpoZpZ7U3b3EjdOpW7K6L y7LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695133235; x=1695738035; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+pcq0CQt0VAYlA0JpP6GnNsa4rpicbdrADXBAMMSs6M=; b=CwbADv+3AWNDDy0vvX9drzklOiCquy7OYSeLq6i/oprpQoGt5QZxXgBgGpZmB/syKG oxKOg1IWbzxFgxpbfpIX0RkAz2p94I6f+EtwRPox6RgyUQ4yqdaSGHrfiyG/uZCWatZ6 HgDr6NWez5wbx5hnr2TtswCnKZlolFhiACeNkpohzAehI1P5aPgCvH+e1t5Xwn+ip4wx 8i1j7cWGecmqMNfQ4GN1WS6CLDq6wEwCyzWkcGEqabU2ZMJU6MgZzMXcPzYMbWRLpIic EjNxYnrVDVPec38dAkX6BjC+WODsxNqORAAKX3YGLUXs+BtmoYPj5YGsYzJ5QJpjiWIU vrFA== X-Gm-Message-State: AOJu0YxkafQUU4E+xxcBsctTKRJ/2yY3Ff9OEoWki+tQESze0YHkvMk+ LZM6LmGZqRHFtBb67+IAXBOYhbzHBWehW4QdRlcKyOt/mq+35+Yr76spJA== X-Google-Smtp-Source: AGHT+IFEUmPdalH/S1/Su9hyzim7XyzcY4dzQInLfRVYjcYOYhi52LxaqI77DpI36JJ25wY9/byMhGjufn5LwloOjNQ= X-Received: by 2002:a81:6cc1:0:b0:59b:eab8:7ac6 with SMTP id h184-20020a816cc1000000b0059beab87ac6mr12733977ywc.42.1695133235395; Tue, 19 Sep 2023 07:20:35 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Steve Chavez Date: Tue, 19 Sep 2023 11:20:24 -0300 Message-ID: Subject: Re: Add pg_basetype() function to obtain a DOMAIN base type To: PostgreSQL-development Content-Type: multipart/alternative; boundary="00000000000057e6790605b6f633" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000057e6790605b6f633 Content-Type: text/plain; charset="UTF-8" Just to give a data point for the need of this function: https://dba.stackexchange.com/questions/231879/how-to-get-the-basetype-of-a-domain-in-pg-type This is also a common use case for services/extensions that require postgres metadata for their correct functioning, like postgREST or pg_graphql. Here's a query for getting domain base types, taken from the postgREST codebase: https://github.com/PostgREST/postgrest/blob/531a183b44b36614224fda432335cdaa356b4a0a/src/PostgREST/SchemaCache.hs#L342-L364 So having `pg_basetype` would be really helpful in those cases. Looking forward to hearing any feedback. Or if this would be a bad idea. Best regards, Steve Chavez On Sat, 9 Sept 2023 at 01:17, Steve Chavez wrote: > Hello hackers, > > Currently obtaining the base type of a domain involves a somewhat long > recursive query. Consider: > > ``` > create domain mytext as text; > create domain mytext_child_1 as mytext; > create domain mytext_child_2 as mytext_child_1; > ``` > > To get `mytext_child_2` base type we can do: > > ``` > WITH RECURSIVE > recurse AS ( > SELECT > oid, > typbasetype, > COALESCE(NULLIF(typbasetype, 0), oid) AS base > FROM pg_type > UNION > SELECT > t.oid, > b.typbasetype, > COALESCE(NULLIF(b.typbasetype, 0), b.oid) AS base > FROM recurse t > JOIN pg_type b ON t.typbasetype = b.oid > ) > SELECT > oid::regtype, > base::regtype > FROM recurse > WHERE typbasetype = 0 and oid = 'mytext_child_2'::regtype; > > oid | base > ----------------+------ > mytext_child_2 | text > ``` > > Core has the `getBaseType` function, which already gets a domain base type > recursively. > > I've attached a patch that exposes a `pg_basetype` SQL function that uses > `getBaseType`, so the long query above just becomes: > > ``` > select pg_basetype('mytext_child_2'::regtype); > pg_basetype > ------------- > text > (1 row) > ``` > > Tests and docs are added. > > Best regards, > Steve Chavez > --00000000000057e6790605b6f633 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Just to give a data point for the need of this functi= on:


This is also a common use case for ser= vices/extensions that require postgres metadata for their correct functioni= ng, like postgREST or pg_graphql.

Here's a que= ry for getting domain base types, taken from the postgREST codebase:
<= div>https://gi= thub.com/PostgREST/postgrest/blob/531a183b44b36614224fda432335cdaa356b4a0a/= src/PostgREST/SchemaCache.hs#L342-L364

So havi= ng `pg_basetype` would be really helpful in those cases.

Looking forward to hearing any feedback. Or if this would be a bad i= dea.

Best regards,
Steve Chavez

On Sat, 9 Sept= 2023 at 01:17, Steve Chavez <steve= @supabase.io> wrote:
Hello hackers,

Currently ob= taining the base type of a domain involves a somewhat long recursive query.= Consider:

```
create domain mytext as t= ext;
create domain mytext_child_1 as mytext;
create domain mytext_chi= ld_2 as mytext_child_1;
```

To get `= mytext_child_2` base type we can do:

```
WITH RECURSIVE
recurse AS (
=C2=A0 SELECT
=C2=A0 =C2=A0 oid,
= =C2=A0 =C2=A0 typbasetype,
=C2=A0 =C2=A0 COALESCE(NULLIF(typbasetype, 0)= , oid) AS base
=C2=A0 FROM pg_type
=C2=A0 UNION
=C2=A0 SELECT
= =C2=A0 =C2=A0 t.oid,
=C2=A0 =C2=A0 b.typbasetype,
=C2=A0 =C2=A0 COALE= SCE(NULLIF(b.typbasetype, 0), b.oid) AS base
=C2=A0 FROM recurse t
= =C2=A0 JOIN pg_type b ON t.typbasetype =3D b.oid
)
SELECT
=C2=A0 o= id::regtype,
=C2=A0 base::regtype
FROM recurse
WHERE typbasetype = =3D 0 and oid =3D 'mytext_child_2'::regtype;

=C2= =A0 =C2=A0 =C2=A0 oid =C2=A0 =C2=A0 =C2=A0 | base
----------------+----= --
=C2=A0mytext_child_2 | text
```

Core has the `getBaseType` function, which already gets a domain base typ= e recursively.

I've attached a patch that expo= ses a `pg_basetype` SQL function that uses `getBaseType`, so the long query= above just becomes:

```
select pg_baset= ype('mytext_child_2'::regtype);
=C2=A0pg_basetype
----------= ---
=C2=A0text
(1 row)
```

Tes= ts and docs are added.

Best regards,
Ste= ve Chavez
--00000000000057e6790605b6f633--