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 1tqzQa-00DDgd-2O for pgsql-general@arkaria.postgresql.org; Sat, 08 Mar 2025 19:01:40 +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 1tqzQW-006YCA-Kw for pgsql-general@arkaria.postgresql.org; Sat, 08 Mar 2025 19:01:36 +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 1tqzQW-006YBz-7j for pgsql-general@lists.postgresql.org; Sat, 08 Mar 2025 19:01:36 +0000 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tqzQS-001j7q-1k for pgsql-general@lists.postgresql.org; Sat, 08 Mar 2025 19:01:35 +0000 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2ff5f2c5924so806108a91.2 for ; Sat, 08 Mar 2025 11:01:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741460491; x=1742065291; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=/ZdZwn3Or8KvOSS7SLPLq7reybd6V9IC6o26iZd/nTk=; b=Fz9mtGUARXiXGMwFREUYnzYIV9FxD9Fs1zLalHGyvTlxpPMhfZoGgC1QIxkefJoNwT 4ngjFDuXosOutFnO2EsbrDziB2e/cvfG9PuS70bt92ZiR4Ft03uNSbg85Ot9gu99VlOd ibekFm3uXOadCeF0MJryoGW+bo6DNjG1xyFWBF+EZYctDFBetftdyUoNOuLoGHF/AS/4 qd01H/Wi93fuBxjKal5Opj10vYHJGIU5upFftlXD3QMUX4C8IpbtMODo2kBYdQlH69Pu DFEX0RyGMKE/dzLQYfdZSxZwlSVE2NhtVWEhu1jrBc3BFx7CmVbmh4gurpZjdDW9HVFz 9sRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741460491; x=1742065291; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=/ZdZwn3Or8KvOSS7SLPLq7reybd6V9IC6o26iZd/nTk=; b=tSZDdXpGDovIrF2NawO38c7X7IeNWSh268587s6T9rOCmAkrzlQYziztFrOkD4WMiU uIfiJOZ3Zggvg16tUs3QQN3aLkaqqOOdu8/hcc0FeY4tsHn6xnoiEBDLeB5ciGGnA/eo CRg3U8fWPQw4HtXQCgXoxY4kIKVanh1riJsSUvptemycRviDfJDLSW7ooKdV+/y1TNdh CjCmd1vXqWM84kmVU/l+M7smbvLhM7Wzbad4zYOBCl8yxEbuFcBwqVuCNMTafGOrK/QU V4xi8DfMVj7O0sdMGlzNL95AKh4nk/oW4dLUvLK/fT/UNlaghPUilNS4ZOolxUhVqQvc vUAA== X-Gm-Message-State: AOJu0YxrlE16PtTNdYvhhnbGXEFoW0uVJ3Qac4GKg0Kf6zo5iXPQb9R2 HIHnIsApNERUIUYDSBVwTLDYCxN0FenefssghmtCZ20XjGApZuwNhgT1V045xTuZlYn2kOG88Gq 9zdzrCAox0Gt0FTG/OUwzpb4hgD/ZpTe1fwU= X-Gm-Gg: ASbGncuv3MZX/lH7EnjBXkfNnyx8mclutl+IqvsIVFv+PBWbD0aVQo3FHbRcKy9bM9k mNfresl92cpBzCD22AID2/xT56QlKjIc5L3eQxuDnWlsZgbuvf1eSs0oiR4YBIt+eKFsxRXusfo EtIm/yjDUaCyJoZ+1MEXNUV+4fB1olhWhEj8wDihE= X-Google-Smtp-Source: AGHT+IGwLAmlYzm7wpGSSc8KmUFeDTTxbXNcrSEKLCccMQwtLL4LTkBmXasq0Fi6mr+qEQDMTpD0M/1894gbS1PEjyg= X-Received: by 2002:a17:90b:1a91:b0:2ee:6563:20b5 with SMTP id 98e67ed59e1d1-300a2891aeamr2198033a91.0.1741460490643; Sat, 08 Mar 2025 11:01:30 -0800 (PST) MIME-Version: 1.0 From: "Rhys A.D. Stewart" Date: Sat, 8 Mar 2025 14:01:04 -0500 X-Gm-Features: AQ5f1JompoCaF0Sgrj6ghWm46toLIhQyqG6jrhzBb0k5FIsmKH2DRCMLOSvHdlM Message-ID: Subject: exclusion constraint question To: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Greetings All, I have the following table: 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). ); And some data: 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); 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. Any suggestions would be appreciated. Regards, Rhys Peace & Love | Live Long & Prosper