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 1trZBA-005bPF-03 for pgsql-general@arkaria.postgresql.org; Mon, 10 Mar 2025 09:12:08 +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 1trZB8-00775D-P9 for pgsql-general@arkaria.postgresql.org; Mon, 10 Mar 2025 09:12:06 +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 1tqzpD-00771F-4s for pgsql-general@lists.postgresql.org; Sat, 08 Mar 2025 19:27:07 +0000 Received: from p-west1-cluster5-host2-snip6-1.eps.apple.com ([2a01:b747:3003:202::e] helo=p00-icloudmta-asmtp-us-west-1a-100-percent-0.p00-icloudmta-asmtp-vip.icloud-mail-production.svc.kube.us-west-1a.k8s.cloud.apple.com) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tqzpA-001iuA-0Y for pgsql-general@lists.postgresql.org; Sat, 08 Mar 2025 19:27:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emanuelseemann.ch; s=sig1; bh=kBlhCgQ7MkQsHXNUr/iKpB4knMbrBem8TbtQEINETBQ=; h=Content-Type:Mime-Version:Subject:From:Date:Message-Id:To:x-icloud-hme; b=JAMkZ6egPvHsHBSiuQjBilU/ynn1LS5pO1QTV0lMx8fdyZ3HYPC8Sf8MYb/5BWOru Fm/ZLnZbPnplGa7ihDdjBQSSnisefmQOh2zsJyRPgKN5USXu9jaolucxcXgAI5hLHM Y2OEUimXyC3MTXeaPvWnVaYUIWDWsLRLnYF6JeH1Dkol7P8JGg2Xl1Iuf4Z9rg5srS axoiHFiYNjRvy7JtDDBC73j3RUiZ5ti5qeqamTbo34nir+g4vl/0BRweHT7m4uvBQU mAhm3AXRwU8OV8ZbpPp1WXkhz2x0skI14RIjoQuAUnCsyQEO1us20lOeWp7eW66udM 3alA38H49a+uA== Received: from smtpclient.apple (pv-asmtp-me-k8s.p00.prod.me.com [17.56.9.36]) by p00-icloudmta-asmtp-us-west-1a-100-percent-0.p00-icloudmta-asmtp-vip.icloud-mail-production.svc.kube.us-west-1a.k8s.cloud.apple.com (Postfix) with ESMTPSA id ABB3B1800919; Sat, 8 Mar 2025 19:26:58 +0000 (UTC) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: exclusion constraint question From: personal@emanuelseemann.ch In-Reply-To: Date: Sat, 8 Mar 2025 20:26:44 +0100 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <9D35A370-D623-4858-93CA-BBEE0F27AADF@emanuelseemann.ch> References: To: "Rhys A.D. Stewart" X-Mailer: Apple Mail (2.3774.600.62) X-Proofpoint-ORIG-GUID: LzufDGRqxzSPHBIX2KW_kvgIVdWKlvzV X-Proofpoint-GUID: LzufDGRqxzSPHBIX2KW_kvgIVdWKlvzV X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1093,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-03-08_08,2025-03-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 clxscore=1030 bulkscore=0 spamscore=0 phishscore=0 adultscore=0 malwarescore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2503080151 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hey Rhys, I think you might be better served doing something like CREATE TABLE shelve_items( id bigint PRIMARY KEY, shelf_id bigint, column_name VARCHAR, mug_id bigint UNIQUE ) and then putting a unique index on (shelf_id, column_name): CREATE UNIQUE INDEX shelve_items_id_c_name on shelve_items (shelf_id, = column_name) The first row of your shelf would then look like: INSERT INTO shelve_items VALUES (1, 1, left, 7) INSERT INTO shelve_items VALUES (2, 1, middle, 2) INSERT INTO shelve_items VALUES (3, 1, right, 1) This would also allow you to scale to shelves that are n items wide = (maybe at that point you could use an id for the columns instead of = names) Does this work for you? Best, Emanuel > On 8 Mar 2025, at 20:01, Rhys A.D. Stewart = wrote: >=20 > Greetings All, >=20 > I have the following table: >=20 > CREATE TABLE shelves( > shelf_id bigint PRIMARY KEY, > l_mug_id bigint UNIQUE, > c_mug_id bigint UNIQUE, > r_mug_id bigint UNIQUE, > CHECK (l_mug_id <> c_mug_id AND l_mug_id <> r_mug_id AND c_mug_id > <> r_mug_id), > EXCLUDE USING gist (l_mug_id WITH <>, c_mug_id WITH <>, r_mug_id > WITH <>) -- Not working as expected (or my expectations are wrong). > ); >=20 > And some data: >=20 > INSERT INTO shelves VALUES (1, 7, 2, 1); > INSERT INTO shelves VALUES (2, 3, null, null); > INSERT INTO shelves VALUES (3, null, 1, 4); > INSERT INTO shelves VALUES (4, 4, 5, null); >=20 > Mugs on shelves, fascinating. A mug_id can only appear once in the > entire table. The check constraint handles not having the same mug_id > in each row and the unique constraints does the same for the column. > But how do I get around checking multiple columns for the same mug_id. > I'm thinking an exclusion constraint, but (a) I do not know if I am > overthinking it and (b) the exclusion constraint I have does not work > as expected, or my expectations are way off. >=20 > Any suggestions would be appreciated. >=20 > Regards, >=20 > Rhys > Peace & Love | Live Long & Prosper >=20 >=20