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 1rJIXC-00CZr2-Or for pgsql-admin@arkaria.postgresql.org; Fri, 29 Dec 2023 19:28:42 +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 1rJIXB-00BTdS-CG for pgsql-admin@arkaria.postgresql.org; Fri, 29 Dec 2023 19:28: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 1rJIXB-00BTd9-16 for pgsql-admin@lists.postgresql.org; Fri, 29 Dec 2023 19:28:41 +0000 Received: from jakobs.com ([85.214.83.89] helo=rs.plausibolo.de) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rJIX4-00EW6h-Jy for pgsql-admin@postgresql.org; Fri, 29 Dec 2023 19:28:40 +0000 Received: from localhost (localhost [127.0.0.1]) by rs.plausibolo.de (Postfix) with ESMTP id E1AED380485; Fri, 29 Dec 2023 20:28:32 +0100 (CET) Received: from rs.plausibolo.de ([127.0.0.1]) by localhost (h2367442.stratoserver.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id r-hhiNUX0vTx; Fri, 29 Dec 2023 20:28:32 +0100 (CET) Received: from [192.168.178.86] (xdsl-84-44-129-210.nc.de [84.44.129.210]) by rs.plausibolo.de (Postfix) with ESMTPSA id 7466138046D; Fri, 29 Dec 2023 20:28:32 +0100 (CET) Message-ID: Date: Fri, 29 Dec 2023 20:28:31 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Content-Language: de-DE To: "[PGSQL-ADMIN-LIST]" From: Holger Jakobs Subject: Domain checks not always working when used in compound type Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello All, Today I was experimenting with domain and compound types. -- Creating a domain type with NOT NULL constraint and at least 2 characters. CREATE DOMAIN domain_a AS VARCHAR(10) NOT NULL   CHECK (LENGTH(TRIM(value)) > 2); -- Using the domain within a compound type. The NOT NULL constraint should be -- inherited from the domain, therefore not repeated here for column a. CREATE TYPE compound_ab AS (   a    domain_a,   b    varchar(10) ); -- Creating a table using the compound type. CREATE TABLE test1 (   name  VARCHAR(25) NOT NULL UNIQUE,   ab    compound_ab,   id    INTEGER GENERATED ALWAYS AS IDENTITY ); -- Insert into table without providing a value for ab, thus (ab).a is NULL, which shouldn't be allowed. INSERT INTO test1 VALUES ('person1'); INSERT 0 1 -- Select proves that (ab).a is NULL - violating the NOT NULL constraint. SELECT *, (ab).a FROM test1;   name   | a ---------+---  person1 | ¤ ¤ is set as a marker for NULL values in order to make it distinguishable from an empty string. -- Directly inserting a NULL gives an error, so the constraint is in place. INSERT INTO test1 (name, ab.a, ab.b)   VALUES ('person2', NULL, 'something'); ERROR 23502: domain domain_a doesn't allow NULL values INSERT INTO test1   VALUES ('person3', '(,something)'); ERROR 23502: domain domain_a doesn't allow NULL values -- But it can be circumvented by only feeding ab.b, but not ab.a. INSERT INTO test1 (name, ab.b)   VALUES ('person3', 'something'); SELECT *, (ab).a FROM test1;   name   |      ab      | id | a ---------+--------------+----+---  person1 | ¤            |  1 | ¤  person3 | (,something) |  2 | ¤ The tests have been carried out with PostgreSQL 16.1. Is the behaviour as expected? I think it isn't. Regards, Holger -- Holger Jakobs, Bergisch Gladbach, Germany, Tel. +49-178-9759012 Hint: Only my mail server is entitled to send mails from my mail address. The SPF regulation is strict. Mailing lists must not use the original from address when forwarding!