Subject: Domains vs data types

From: Ertan Küçükoglu <[email-masked]>
Date: Wed, 20 Aug 2025 07:47:43 +0300

Hello,

I am using PostgreSQL 17.6.
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.

For example I might have table defined as below
create table test (
  a integer,
  b integer,
  c integer,
  d varchar(5)
);

I might also have ame table defined as below

create domain aint integer;
create domain s5 varchar(5);

create table test_domain (
  a aint,
  b aint,
  c aint,
  d s5
);

Does the second table have any technical advantage/disadvantage over plain
data type definition?
Less metadata in memory? High metadata in memory? Less/increased disk space?

Thanks & Regards,
Ertan

---

From: Greg Sabino Mullane <[email-masked]>
Date: Wed, 20 Aug 2025 12:12:25 -0400

> Less metadata in memory? High metadata in memory? Less/increased disk

Same disk space. No disadvantage other than confusing your users, and any
performance differences will be so minor as to be unmeasurable. (my two
cents: domains are best when the data type is complex AND shared across
multiple tables. Even then I tend to avoid them.)

Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support

---

From: Ron Clarke <[email-masked]>
Date: Wed, 20 Aug 2025 18:56:47 +0200

Opinion: domains are useful if you give them names that are full of
meaning. For example if you have the same type of data accross tables
"item_number" or "account" etc so that you can use them to describe what
you want stored in them and ensure the same defaults, nulls etc are applied
accross tables. Having randomly named or encoded lists just makes life more
complicated.

On Wed, 20 Aug 2025, 18:13 Greg Sabino Mullane, <[email-masked]> wrote:

> On Wed, Aug 20, 2025 at 12:48 AM Ertan Küçükoglu <
> [email-masked]> wrote:
>
>> Does the second table have any technical advantage/disadvantage over
>> plain data type definition?

---

From: "David G. Johnston" <[email-masked]>
Date: Wed, 20 Aug 2025 10:01:30 -0700

On Wed, Aug 20, 2025 at 9:57 AM Ron Clarke <[email-masked]> wrote:

> ...and ensure the same defaults, nulls etc are applied accross tables.
>

Just as a warning - a deviation we have from the SQL Standard regarding
domains and their NOT NULL constraints makes specifying one on a domain a
bit of a potential trap.  Rely on column NOT NULL.

David J.