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 1rhyLB-00ENHt-AH for pgsql-general@arkaria.postgresql.org; Wed, 06 Mar 2024 20:58:17 +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 1rhyL8-00EJil-R5 for pgsql-general@arkaria.postgresql.org; Wed, 06 Mar 2024 20:58:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rhyL8-00EJid-9t for pgsql-general@lists.postgresql.org; Wed, 06 Mar 2024 20:58:14 +0000 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rhyL4-003CQS-9m for pgsql-general@postgresql.org; Wed, 06 Mar 2024 20:58:13 +0000 Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-5cf2d73a183so954176a12.1 for ; Wed, 06 Mar 2024 12:58:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709758690; x=1710363490; darn=postgresql.org; h=mime-version:subject:references:in-reply-to:message-id:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=qG9ATvrKxtgkTy2qNdqWDUnXSuntPD8GxlqcZ78JnCc=; b=chYVF0MrchtypJ3pE5z8d6nGzM4gcLTr32pxD0NWROipw+QH7+YpmpvlkNJzpjO9qm +z33CLZ5mXVuCVdBnruLZXXajrqpW7DglNQ74kWF+IRyomI75nzm8gv52OmdOUQ4M7CY 3QXC3tlYYimD7CimZ6FjcqMggw9VWfjy+UPx+MIZ/pJL6K9/TZcSXM00BJc3X5xyRpLB D1pk5Z68KbqEU70UOeWsZPSwUJLJMEz8dIRB0jAIZL6p0TL/5jMPw2dZd7QwCtK5d25B BBuLpiqcSjrXAkUq4NPRsp67wUuTILhpRUNDIV0LtmVlUs2M713LBsOOTN3MagCkPdJM o5Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709758690; x=1710363490; h=mime-version:subject:references:in-reply-to:message-id:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qG9ATvrKxtgkTy2qNdqWDUnXSuntPD8GxlqcZ78JnCc=; b=g+Ix3Rk4IwOX7Vi0NDIKjgCwWgauNe14lphKqOFOZKKgMW1tvWixNVRXmqM88qYv7G 7rfu9eXzSnlOqgw0Y+IvZ4wR6ghKkCZdYuEetxgK4R5OBo1X4Xw0NIU20ZtQIlVK0CZE fRVBd+J2AlXRn9/kaKHGmCBy3vatN0ii4/lK9AQG4C9MNpXpWoqdrq/Rnq5tJOO6tFFF 0mOtlFrePriEiQcfQCv9vAy747C6DYtpe8bv2SFmtWw9U3cPxfkcyiK7XHd1C+yFbHvG c+9nGooKCAubkL6IXYuDmgs/vvFePU8iHn/OSn/U2FQfpb6ScZY/e79R2EXQvZoavile 3XZA== X-Gm-Message-State: AOJu0YxOriw1XyeQ4JirXMbZq65jf8DKSxQ7EI39NDJRIGBBEEnR3vdR 4Tv6B5IztoyB7e3+Q6ZYAeffPNC6oOx9rQoz7TX+9LU1RGKhAQ3uJnBOTBkl X-Google-Smtp-Source: AGHT+IGmVaIzZzRLKRzNvfNuXPjft5HX/IQa/j1edSfR5cpJcK6C9yzb+pBG8f7UzdLrjUs6uE0Ghw== X-Received: by 2002:a17:90a:e548:b0:29b:1bc5:cfda with SMTP id ei8-20020a17090ae54800b0029b1bc5cfdamr9225090pjb.16.1709758690202; Wed, 06 Mar 2024 12:58:10 -0800 (PST) Received: from [2600:8801:8600:631:3a:533:100:0] ([2600:8801:8600:631::e]) by smtp.gmail.com with ESMTPSA id gt17-20020a17090af2d100b00298cc4c56cdsm167482pjb.22.2024.03.06.12.58.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Mar 2024 12:58:09 -0800 (PST) Date: Wed, 6 Mar 2024 12:57:38 -0800 From: Guyren Howe To: Stephen Frost Cc: PG-General Mailing List Message-ID: In-Reply-To: References: <506dd5f4-65fe-4d34-8cfe-2ebc08b978e4@Spark> Subject: Re: Thoughts on user-defined types for talk at Postgres conference? X-Readdle-Message-ID: e7d2ee82-92f0-4def-8535-e778d8e9a9dd@Spark MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="65e8d8dd_6d9b8fae_12cfa" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --65e8d8dd_6d9b8fae_12cfa Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Thanks for that=21 It seems as though a Composite Type having only fixed-length fields shoul= d be able to be regarded as a fixed-length value. On 6 Mar 2024 at 09:45 -0800, Stephen =46rost , wro= te: > Greetings, > > * Guyren Howe (guyren=40gmail.com) wrote: > > But what *really* sets Postgres apart from comparable systems is user= defined types. I would like to carefully lay out how to define and use a= user-defined type (I don=E2=80=99t think I have time to dig into doing f= ancy stuff with C functions, so just the basic =E2=80=9Cuser defined sum = type=E2=80=9D), but also any gotchas. > > What I take it you're referring to here are most specifically called > =22Composite Types=22, as discussed here: > > https://www.postgresql.org/docs/current/sql-createtype.html > > > And I=E2=80=99d like to finish with some thoughts about when and how = to use user-defined types. My feeling is that this feature is greatly und= er-used, mostly because it=E2=80=99s so non-standard. But A=46AICT, user-= defined types are fine and other than some ugliness due to SQL (mainly ne= eding parentheses in some unexpected places), fields in a user defined ty= pe work perfectly well in Postgres=E2=80=99s SQL. I guess you=E2=80=99d n= eed to pull them apart for values returned to clients, but that isn=E2=80= =99t difficult. > > Yeah, it's a bit grotty when you're trying to reference them (as you > note with the comment about the extra parentheses) but it's also a bit > painful building them up to supply as input (though not impossible). > > > So, any gotchas with user defined types=3F Any thoughts about designi= ng with them=3F > > The biggest gotcha (which is a bit different from the usability issues,= > which we discuss above) from my perspective is the serialization of > composite types- we use up a lot more space to store a composite type > that looks like: > > CREATE TYPE twoint AS (a int, b int); > > then if we create a table as: > > CREATE TABLE t1 (a int, b int); > > Let's show this, follow the above two commands, do: > > CREATE TABLE t2 (c1 twoint); > > INSERT INTO t1 VALUES (1,2); > INSERT INTO t2 VALUES ('(1,2)'); > > =3D*=23 select pg=5Fcolumn=5Fsize(a) from t1; > pg=5Fcolumn=5Fsize > ---------------- > 4 > (1 row) > > =3D*=23 select pg=5Fcolumn=5Fsize(b) from t1; > pg=5Fcolumn=5Fsize > ---------------- > 4 > (1 row) > > =3D*=23 select pg=5Fcolumn=5Fsize(a) from t2; > pg=5Fcolumn=5Fsize > ---------------- > 29 > (1 row) > > We can see it for the whole row too: > > =3D*=23 select pg=5Fcolumn=5Fsize(t1.*) from t1; > pg=5Fcolumn=5Fsize > ---------------- > 32 > (1 row) > > =3D*=23 select pg=5Fcolumn=5Fsize(t2.*) from t2; > pg=5Fcolumn=5Fsize > ---------------- > 53 > (1 row) > > That's an additional 21 bytes, which is really quite a lot. What's > included in those 21 bytes are the length (since it's now a > variable-length column to PG, unlike the case with the individual int > columns in the table where we know from the table structure the length > of them), the type information (typmod if there is one and the OID of > the composite type), some other info, and then the actual data of the > two int values inside of the composite type. > > I've often wondered if there's a way to reduce this overhead, as I do > think that plus some improvements on the usability side would go a long= > way to making composite types more appealing to users. Still, they are > certainly handy in some instances, I just can't recommend heavy use of > them for large data sets where size is particularly important (such as > in heavy OLTP environments) due to their overhead. > > Thanks=21 > > Stephen --65e8d8dd_6d9b8fae_12cfa Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Thanks for that=21

It seems as though a Composite Type having only fixed-length fields shoul= d be able to be regarded as a fixed-length value.
On 6 Mar 2024 at 09:45 -0800, Steph= en =46rost <sfrost=40snowman.net>, wrote:
Greetings,

* Guyren Howe (guyren=40gmail.com) wrote:
But what *really* sets Postgres apart from = comparable systems is user defined types. I would like to carefully lay o= ut how to define and use a user-defined type (I don=E2=80=99t think I hav= e time to dig into doing fancy stuff with C functions, so just the basic = =E2=80=9Cuser defined sum type=E2=80=9D), but also any gotchas.

What I take it you're referring to here are most specifically called
=22Composite Types=22, as discussed here:

https://www.postgresql.org/docs/current/sql-createtype.html

And I=E2=80=99d like to finish with some th= oughts about when and how to use user-defined types. My feeling is that t= his feature is greatly under-used, mostly because it=E2=80=99s so non-sta= ndard. But A=46AICT, user-defined types are fine and other than some ugli= ness due to SQL (mainly needing parentheses in some unexpected places), f= ields in a user defined type work perfectly well in Postgres=E2=80=99s SQ= L. I guess you=E2=80=99d need to pull them apart for values returned to c= lients, but that isn=E2=80=99t difficult.

Yeah, it's a bit grotty when you're trying to reference them (as you
note with the comment about the extra parentheses) but it's also a bit painful building them up to supply as input (though not impossible).

So, any gotchas with user defined types=3F = Any thoughts about designing with them=3F

The biggest gotcha (which is a bit different from the usability issues, which we discuss above) from my perspective is the serialization of
= composite types- we use up a lot more space to store a composite type
that looks like:

CREATE TYPE twoint AS (a int, b int);

then if we create a table as:

CREATE TABLE t1 (a int, b int);

Let's show this, follow the above two commands, do:

CREATE TABLE t2 (c1 twoint);

INSERT INTO t1 VALUES (1,2);
INSERT INTO t2 VALUES ('(1,2)');

=3D*=23 select pg=5Fcolumn=5Fsize(a) from t1;
pg=5Fcolumn=5Fsize
----------------
4
(1 row)

=3D*=23 select pg=5Fcolumn=5Fsize(b) from t1;
pg=5Fcolumn=5Fsize
----------------
4
(1 row)

=3D*=23 select pg=5Fcolumn=5Fsize(a) from t2;
pg=5Fcolumn=5Fsize
----------------
29
(1 row)

We can see it for the whole row too:

=3D*=23 select pg=5Fcolumn=5Fsize(t1.*) from t1;
pg=5Fcolumn=5Fsize
----------------
32
(1 row)

=3D*=23 select pg=5Fcolumn=5Fsize(t2.*) from t2;
pg=5Fcolumn=5Fsize
----------------
53
(1 row)

That's an additional 21 bytes, which is really quite a lot. What's
included in those 21 bytes are the length (since it's now a
variable-length column to PG, unlike the case with the individual int
columns in the table where we know from the table structure the length of them), the type information (typmod if there is one and the OID of
the composite type), some other info, and then the actual data of the
two int values inside of the composite type.

I've often wondered if there's a way to reduce this overhead, as I do
think that plus some improvements on the usability side would go a long way to making composite types more appealing to users. Still, they are certainly handy in some instances, I just can't recommend heavy use of them for large data sets where size is particularly important (such as in heavy OLTP environments) due to their overhead.

Thanks=21

Stephen
--65e8d8dd_6d9b8fae_12cfa--