Re: Domains vs data types

From: Dominique Devienne <ddevienne(at)gmail(dot)com>
To: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: Ertan Küçükoglu <ertan(dot)kucukoglu(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Domains vs data types
Date: 2025-08-21 08:11:08
Message-ID: CAFCRh-9n3Hf+D0fNKX4UQ34WxFSCz+SUUiB8T5EGyj9206yCMQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Aug 20, 2025 at 7:37 PM Florents Tselai
<florents(dot)tselai(at)gmail(dot)com> wrote:
> > On 20 Aug 2025, at 7:47 AM, Ertan Küçükoglu <ertan(dot)kucukoglu(at)gmail(dot)com> wrote:
> > I would like to learn if there is any benefit of using domains over data types for table column definitions in terms of performance gain/loss.

> I know that this doesn’t answer your question, but before exploring custom types / domains,
> and based on experience, I’d strongly recommend exploring jsonb instead as an alternative.

I stayed out of that thread, but this makes me step out and react.
domains are typically out constraining the value space of a type.
I.e. a type with a CHECK constraint. W/o one, it's pretty useless IMHO.

While json/jsonb is about denormalizing and stuffing unconstrained data,
of arbitrary (and often evolving) content. No need for complex relational
modeling and its associated constraints and "rigidity". I.e. easy evolution
of the data tier, at the cost of applications having to deal with the complexity
themselves of constrained data (must expect anything). Your SQL also becomes
less expressive or more complex, although PostgreSQL has excellent JSON support.

> Also note that using custom types can lead to some confusion initially for basic stuff
> you can’t do things like SELECT mytype.f1 and instead you have to do (mytype).f1

I guess you're talking about composite types? Not sure OP had that in mind.
But that's still a good point. Thanks for sharing.

Yes, using a custom type/domain is more metadata, since a new row in pg_type.
But that's mostly negligeable.

There's always an (integral) OID associated to columns, so only its value
changes if you start using a custom type, be it i memory or on disk. So no,
using a custom type is unlikely to make things bigger or slower.

That said, in my case, because I use the BINARY mode of LIBPQ and COPY,
it does matter, as my code knows about built-in OIDs, but not of custom OIDs
for custom types. But few people care about such things.

FWIW, --DD

In response to

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2025-08-21 09:50:53 Re: Streaming replica hangs periodically for ~ 1 second - how to diagnose/debug
Previous Message Justin Pryzby 2025-08-21 01:51:16 Re: analyze-in-stages post upgrade questions