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 1ts0c7-00DBxB-8H for pgsql-general@arkaria.postgresql.org; Tue, 11 Mar 2025 14:29:47 +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 1ts0c5-00HE51-V3 for pgsql-general@arkaria.postgresql.org; Tue, 11 Mar 2025 14:29:45 +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 1ts0c5-00HE4t-K8 for pgsql-general@lists.postgresql.org; Tue, 11 Mar 2025 14:29:45 +0000 Received: from mail-il1-x129.google.com ([2607:f8b0:4864:20::129]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ts0c1-002FXU-37 for pgsql-general@lists.postgresql.org; Tue, 11 Mar 2025 14:29:45 +0000 Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-3d45503af24so22963455ab.2 for ; Tue, 11 Mar 2025 07:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741703381; x=1742308181; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=7lgpqY8ZnKAQwgCF70YmQIkJ+g4Laa5NlBZzWM5WaKU=; b=myZuNayaIQHiKykgBN/NUdPlZFvTIWMWl43O8xbkhCLX6OIz3cwpsrvSFJR1Y72v2e oFHWCEvRkA2HyuieqoQpk5fEGjxBzrUPuYoPqDnkN+Rz5XeREBFMN+m5qdTR5JxCyRRR /MSQMWyny/W2g7u5KrOkQmfHTJaxlNJdON0j8krP0V7b7UfJs5EiiDXgAjIexb9zTXEQ IBeJbAfEtKtv936cS1UB/lLI0V8FRrRNwUnbspPsiA5Cww0MCCJUWUYRWcjw+A3wjPVG EWo2mYRL9DW3pTlpcoB6iD58H2Mshxxv0VpgYq3O7dYVm4WurTrEWBH6grt3KsqJmBZ+ IG0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741703381; x=1742308181; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7lgpqY8ZnKAQwgCF70YmQIkJ+g4Laa5NlBZzWM5WaKU=; b=tGqM6g5h3X1bsjFrlOz8A4l+cwJyUv0M4LQbJtLjVvvsKV3JhttM9yYy9J7t9+YizN VqiCcBvt4Hk2y9iqT9m2Q63tg1Xao+SLb0h3H5AyMY5BT+WbhTU8OljvlUYhEFcLZ/Xc QbBJkugLn1QEVyBT60AxUM8slbZLc+sd41P/p13DjS2uOpUFCx6YUJ1D5okbTEOoWKVw VKhjKoSEsMdd6Xn8N/ZTPdnpTesl4weGFhwnYqpd0ABPMwhl70ikd/pp105vwnlvO3Q4 mZtEZhbcMgW1CzzImbjlCbiykTt4DMBJ3izJ2UX1kcuCJ+1or8qQMj8MxiwuEwLD+TJR l71A== X-Forwarded-Encrypted: i=1; AJvYcCXQnEQh2Csa0nsbB16PaOH3PNZmLIK2xgfoc5yrpnOgGXVUAamCOVijs6TbcnyYT9IxI44QgEPAX8eW2D2U@lists.postgresql.org X-Gm-Message-State: AOJu0YwBTBcNdwHa4b56eLDtmsBUlnSe9n2kdg4S2++Z9i75hycMtrwx PFd93zexcYFt+cwFUFWD3vj8eSWpi+NwXCUwU9xqCBjUR4wYzMBNiKRuikIKLP5WYQyECvN/3xn JAW4WYTX4YyHrlQrQmNuCCuE1D50= X-Gm-Gg: ASbGnctKte6RhUOiX9ST/UtICgm5ZiV4ldER3vL2dy+e297jZdcI9VKagox9nUyRAZz LbxZSO71s87g+Ms2ak68ZVUjdRQIILXRMfF/Q33Vxcg1qhm845dKmv9KE28DzHJ7bql6KtX9M7m t4j2+mDOYoTNv4gElaj74pGRqfqAyyhJU5E9UO8OkTCkdspPaRW6YuEbDX0CGN X-Google-Smtp-Source: AGHT+IHc5XPWY6owRkmYLKcGaRgfrg04/TgXAtFMF/887R4lJuG/Cbr4G9ldRhIc0K3LCTgL4H7iIEfaUgXumviWaME= X-Received: by 2002:a05:6e02:1d09:b0:3d4:2ea4:6b8a with SMTP id e9e14a558f8ab-3d4689a0861mr40171965ab.22.1741703381442; Tue, 11 Mar 2025 07:29:41 -0700 (PDT) MIME-Version: 1.0 References: <6f7fff13-fbc6-423b-b8cb-73762b0cc28b@cloud.gatewaynet.com> In-Reply-To: From: Greg Sabino Mullane Date: Tue, 11 Mar 2025 10:29:05 -0400 X-Gm-Features: AQ5f1JoM1Ub65Q_pfqVI6J3dEdgKaoG8l66At3ShSRCq8LIKAKC9QLSesGiIjwc Message-ID: Subject: Re: exclusion constraint question To: Achilleas Mantzios - cloud Cc: "Rhys A.D. Stewart" , pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000005aec23063011ebeb" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000005aec23063011ebeb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Mar 11, 2025 at 3:06=E2=80=AFAM Achilleas Mantzios asked: > is it still harder than the trigger ? > I think the trigger wins: no extension needed, arguably better error output, easier to understand at a glance, and can quickly change the business logic by adjusting the function. Pretty short too. Don't know what the op tried, but here's a version I came up with. Note that this trigger allows you to remove all the UNIQUE column specifiers and the CHECK clause from the original table. create table mugs ( mug_id bigint PRIMARY KEY, shelf bigint -- not needed for the trigger to work, but nice to have ); create or replace function add_a_mug() returns trigger language plpgsql as $$ begin if tg_op in ('UPDATE','DELETE' then delete from mugs where mug_id in (old.l_mug_id, old.c_mug_id, old.r_mug_id); end if; if tg_op in ('UPDATE','INSERT') then with mugs as (select unnest(array[new.l_mug_id,new.c_mug_id,new.r_mug_id]) as mug) insert into mugs(mug_id, shelf) select mug, new.shelf_id from mugs where mug is not null; end if; return null; end $$; create trigger take_one_down_pass_it_around after insert or update or delet= e on shelves for each row execute function add_a_mug(); Cheers, Greg -- Crunchy Data - https://www.crunchydata.com Enterprise Postgres Software Products & Tech Support --0000000000005aec23063011ebeb Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, Mar 11, 2025 at 3:06=E2=80=AFAM A= chilleas Mantzios asked:

      

is it still har= der than the trigger ?

I think the trigger wins: no extension ne= eded, arguably better error output, easier to understand at a glance, and c= an quickly change the business logic by adjusting the function. Pretty shor= t too. Don't know what the op tried, but here's a version I came up= with. Note that this trigger allows you to remove all the UNIQUE column sp= ecifiers and the CHECK clause from the original table.

=
create table mugs (
=C2=A0 mug_id bigint PRIMARY KEY,
=C2=A0 she= lf bigint -- not needed for the trigger to work, but nice to have
);

create or replace function add_a_mug() returns trigge= r
language plpgsql as $$
begin
=C2=A0 if tg_op in ('UPDATE'= ;,'DELETE' then
=C2=A0 =C2=A0 delete from mugs where mug_id in (= old.l_mug_id, old.c_mug_id, old.r_mug_id);
=C2=A0 end if;
=C2=A0 if t= g_op in ('UPDATE','INSERT') then
=C2=A0 =C2=A0 with mugs= as (select unnest(array[new.l_mug_id,new.c_mug_id,new.r_mug_id]) as mug)=C2=A0 =C2=A0 =C2=A0 insert into mugs(mug_id, shelf) select mug, new.shel= f_id
=C2=A0 =C2=A0 =C2=A0 =C2=A0 from mugs where mug is not null;
=C2= =A0 end if;
=C2=A0 return null;
end
$$;

c= reate trigger take_one_down_pass_it_around after insert or update or delete=
on shelves for each row execute function add_a_mug();

Cheers,
Greg

--
Enterprise Postgres Software Produc= ts & Tech Support

--0000000000005aec23063011ebeb--