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 1uJwih-009uLd-HR for pgsql-general@arkaria.postgresql.org; Tue, 27 May 2025 16:00:03 +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 1uJwig-009LCT-8U for pgsql-general@arkaria.postgresql.org; Tue, 27 May 2025 16:00:02 +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 1uJvHQ-008ZLi-Nu for pgsql-general@lists.postgresql.org; Tue, 27 May 2025 14:27:49 +0000 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uJvHN-000Ngc-38 for pgsql-general@lists.postgresql.org; Tue, 27 May 2025 14:27:48 +0000 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-7080dd5fe92so26134117b3.3 for ; Tue, 27 May 2025 07:27:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1748356065; x=1748960865; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=iqvAIOqrdSw7sITgFTfIAMcMwKQIN4/jZHwWHRYbX0I=; b=HG5LykHqEDSjtJhs1wtU7o5NYjBOSs79f3L7pFPGHx8w1+YE1fuBKuUZX2HB6ZLfJD +AebsuOLf2m4pZd8REmsT9zotxF7P0ANe+jBjDGemeIRbmxYaivv5IQJmaMOQUj9The3 D+a0DlvnLS8+SxIKZhuB6JJIy28eMiZv3EenoE5kgpSe3AzwCsyqoKYpoKPMrX3/nZFf S55M8CbZF0BOud58A00mUzX7HpsYHI5IRKxLS/mdl/Yil5nChwm37aT79CeEHzG5ebSj +U4RlHE1TVPhKItJiG7culRx8HP3lbWEc3qa1C1TSIk9HpY4dfcKMiRstjVWN21RstRy tWcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748356065; x=1748960865; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=iqvAIOqrdSw7sITgFTfIAMcMwKQIN4/jZHwWHRYbX0I=; b=c53yOptPzRSbm9lfNMusqxSSi9FUtyfkknJzsct7Rkkb/t38PbWDau+DRdGY9sAnyt YYDadadzTh1WiRn0tzPMx6V+AhbYF7WCaXjr8Kq8QZCP3bCkIxa/srDpmUcz09mricBs dT9FwjKSW6K0U+go1hqxMnyuC5RvjU/Ez3kK6dSoea95mRo/a3SN4w4NNJ4b18YZNB1j ++6NbOlP4lzxo24dKjYWJu9xPFtxSBdr8uppobbmgG/7Gagnu59ta2guUPluIdwGVd2l dCrXR6HOcSYm6stAVUYml7ySbl067DNHoThFGUUMzJFDPgEiNBKfdVMgerudDQTkegZa pL+w== X-Gm-Message-State: AOJu0Ywr9H5ts1jam2RgDjcgemSh2qchbaOqn6mhCZXDl0X4anDHVNZd rcLwn94V0mk0fN69xgD5wdC+bNjWK2Rvhdj8sWefc79Dp8V+vbM3o3GqJWJKqZnjB58zdfBTUtz DyfGZovUWIjCDtJhO3DzH75BqKHvxRh/jhzZh X-Gm-Gg: ASbGncveW/Rac/2FXmp9fMr5+5dWoM4odORYwLvjxnO/7QskDkV4N9Yk10pLVlwvdkQ JoFFTY2Qhmytb8Rt6qmQqfVvEpS5F+CT+bekCdZKcOP0sWyi+cKVwj0+V5TjOWFJ5gnhjZbT6Tq 8Qf+UzA6WJ42F6NFMc++FlL912xtGVFpUWdjblAQ1drNo= X-Google-Smtp-Source: AGHT+IEKt7x/7EXVjX3EEhy2SUpYqZeOJTm4+HQQJOkMlwMKpfjepZaQuKhSkqpd7bpdtYhfyQolSgDXkMPa6eBLth4= X-Received: by 2002:a05:690c:7001:b0:70d:ed5d:b4dd with SMTP id 00721157ae682-70e2dace451mr135441167b3.25.1748356064641; Tue, 27 May 2025 07:27:44 -0700 (PDT) MIME-Version: 1.0 From: Richard Zetterberg Date: Tue, 27 May 2025 16:27:33 +0200 X-Gm-Features: AX0GCFu3pcJdH31Z57ez3ZwaDuYwDQn1qh1yshi7rN1F5YZOHccYon2soLt6Zx4 Message-ID: Subject: Changing a varchar(7) domain into text directly in pg_type To: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000002c8e0e06361ede99" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000002c8e0e06361ede99 Content-Type: text/plain; charset="UTF-8" Hello, I have a read-only table that contains a set of never changing categories. Each category has a unique alpha numerical ID and a description. The purpose of this table is so that other tables can reference the ID of this table, to make sure that they don't contain invalid/unknown categories and so that users can lookup the description of each category. This category table has the following type on the ID column: "varchar(7)" (yes, I should have used text). In order to avoid having to type "varchar(7)" in all the tables that references the category table, I created this domain that I used as type for all referencing columns: "CREATE DOMAIN cat.id AS varchar(7);". During some data archeology, I found a bunch of new categories that haven't been imported into the database yet, and they have IDs longer than 7. I've seen claims that varchar and text have the same representation on disk and that they are treated the same way "under the hood", except for the extra constraint checks on varchar. So, I thought that maybe I could just change the type of my domain to text, directly in pg_type and that should solve my problems. After some thinkering, and looking at how "CREATE DOMAIN cat.id AS varchar(7);" and "CREATE DOMAIN cat.id AS text;" appeared in pg_type and information_schema.domains, I came up with this query: ``` WITH text_oid AS ( SELECT typ.oid AS text_oid FROM pg_type AS typ INNER JOIN pg_namespace AS ns ON typ.typnamespace = ns.oid AND typ.typname = 'text' ), target_oid AS ( SELECT typ.oid AS target_oid FROM pg_type AS typ INNER JOIN pg_namespace AS ns ON typ.typnamespace = ns.oid AND ns.nspname = 'cat AND typ.typname = 'id' ), oids AS ( SELECT * FROM text_oid CROSS JOIN target_oid ) UPDATE pg_type AS styp SET typoutput = 'textout' , typsend = 'textsend' , typbasetype = o.text_oid , typtypmod = -1 FROM oids AS o WHERE oid = o.target_oid; ``` After running that query, my domain had the type text instead of varchar(7) and all the fkeys to my category table seems to be working. Could this be a viable option to solve my problem? Or will I face serious problems later down the line that I haven't discovered/considered yet? Thanks for any insight, Richard Zetterberg --0000000000002c8e0e06361ede99 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,

I have a read-only table that contains a set= of never changing categories. Each category has a unique alpha numerical I= D and a description. The purpose of this table is so that other tables can = reference the ID of this table, to make sure that they don't contain in= valid/unknown categories and so that users can lookup the description of ea= ch category.

This category table has the following type on the ID co= lumn: "varchar(7)" (yes, I should have used text). In order to av= oid having to type "varchar(7)" in all the tables that references= the category table, I created this domain that I used as type for all refe= rencing columns: "CREATE DOMAIN cat.id A= S varchar(7);".

During some data archeology, I found a bunch of= new categories that haven't been imported into the database yet, and t= hey have IDs longer than 7.

I've seen claims that varchar and te= xt have the same representation on disk and that they are treated the same = way "under the hood", except for the extra constraint checks on v= archar. So, I thought that maybe I could just change the type of my domain = to text, directly in pg_type and that should solve my problems.

Afte= r some thinkering, and looking at how "CREATE DOMAIN cat.id AS varchar(7);" and "CREATE DOMAIN cat.id AS text;" appeared in pg_type and informati= on_schema.domains, I came up with this query:

```
WITH text_oid A= S (
=C2=A0 SELECT typ.oid AS text_oid
=C2=A0 =C2=A0 FROM pg_type AS t= yp
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INNER JOIN pg_namespace AS ns
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ON typ.typnam= espace =3D ns.oid
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 AND typ.typname =C2=A0 =C2=A0 =C2=A0=3D 'text'
), target_oid= AS (
=C2=A0 SELECT typ.oid AS target_oid
=C2=A0 =C2=A0 FROM pg_type = AS typ
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INNER JOIN pg_namespace AS ns=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ON typ.typn= amespace =3D ns.oid
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 AND ns.nspname =C2=A0 =C2=A0 =C2=A0 =3D 'cat
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 AND typ.typname =C2=A0 =C2=A0 =C2=A0= =3D 'id'
), oids AS (
=C2=A0 SELECT *
=C2=A0 =C2=A0 FROM t= ext_oid
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CROSS JOIN target_oid
) UPD= ATE pg_type AS styp
=C2=A0 =C2=A0SET typoutput =C2=A0 =3D 'textout&#= 39;
=C2=A0 =C2=A0 =C2=A0, typsend =C2=A0 =C2=A0 =3D 'textsend'=C2=A0 =C2=A0 =C2=A0, typbasetype =3D o.text_oid
=C2=A0 =C2=A0 =C2=A0,= typtypmod =C2=A0 =3D -1
=C2=A0 FROM oids AS o
=C2=A0WHERE oid =3D o.= target_oid;
```

After running that query, my domain had the type = text instead of varchar(7) and all the
fkeys to my category table seems = to be working.

Could this be a viable option to solve my problem? Or= will I face serious problems later down the line that I haven't discov= ered/considered yet?

Thanks for any insight,
Richard Zetterberg<= br>
--0000000000002c8e0e06361ede99--