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 1qnJgV-00AZyr-LT for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Oct 2023 14:14:07 +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 1qnJgU-00FUJQ-15 for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Oct 2023 14:14: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 1qnJgT-00FUIt-Lw for pgsql-hackers@lists.postgresql.org; Mon, 02 Oct 2023 14:14:05 +0000 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qnJgQ-007nT0-P5 for pgsql-hackers@postgresql.org; Mon, 02 Oct 2023 14:14:04 +0000 Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-323168869daso13823457f8f.2 for ; Mon, 02 Oct 2023 07:14:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1696255921; x=1696860721; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=83g/CC92oHu/6mOwRJiePquTOxQ5ioW5iZbw3jYjokY=; b=UHZPhIXq9JcyKaziTo603NiIQdDjlLsGMz0fcjh/4Jw6oljdCpQHC0ROrZApPwRmPd bkX9xyXbIK00hZ3JAoPWSNUq6q0w1hhQR+5x5iYFBj9eDSqs1nZ+7Sea7XIrCq+Nlx6q SVs+7q/DGRq/7jmUDDiRWlKq2egpbBiet/SOPWsymCzPqdrl+JY3k23Rk1Xk5IMWixvl X3GuCdO7hKE1EZzaute3AWMYu+kSm/D5cYHfmjsgKRtnL5WxPyKuaf9nv7xLTZWYN8t9 k14ODQ8qyn+J2g8qti/SF3JXBCj7wvXAyuclklTy3s4RVyw10FV8z4JyP4AgHy7xZisZ kPmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696255921; x=1696860721; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=83g/CC92oHu/6mOwRJiePquTOxQ5ioW5iZbw3jYjokY=; b=nGMZcDq0/A1y9jqmFFqmRlaREY7wRxBSfEZxodA8fdN18og2nvSvjNbyoftcnnfTkd IfIVYQrZrpHqtoyLiB2hKfNYbD+BQHtWQuXfVzhwWzhSdxQAnMDvAweQgPcdHoC8hOhg kNOrLasKKu65tClL3V0H8MH4o16BgsFxnYo1VBA/rchOCp/B+33scCN4s9bCUmPeRKqv JTHu7HWsMyKCcGSABUR0qsyeRsVTh38Z/slVZIRNHiPu4Tse9h5PAYzZUfY/eQzvLuM+ g8bltWQxumpxB/z3mrcESv9pLRg4xFalmYC1/6nXBAfXITWJDXWI9Uao1MH4r4rz9btK pKow== X-Gm-Message-State: AOJu0YwS6omXyUAMQWmhNFCSc3lK/GNhBvjRIT+LgJhXbMxpd+IAoIhU HFkUrJf3mfOYzj75S6Zcn0vGZIiDfv3RrJITbdA= X-Google-Smtp-Source: AGHT+IGzI/36JzCAAEXj+pp1l5pQ2UrTsCdmsehByP4aV1WM6haGQmFNXoBbbfxCtvLhDI4H1447CQ== X-Received: by 2002:a5d:4049:0:b0:31f:a503:c05c with SMTP id w9-20020a5d4049000000b0031fa503c05cmr11472615wrp.38.1696255920245; Mon, 02 Oct 2023 07:12:00 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:6cf4:1982:ecd7:8449:ce90]) by smtp.gmail.com with ESMTPSA id v20-20020a50a454000000b005346bebc2a5sm9612664edb.86.2023.10.02.07.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 07:11:59 -0700 (PDT) Message-ID: Subject: Re: Trigger violates foreign key constraint From: Laurenz Albe To: Tom Lane Cc: pgsql-hackers@postgresql.org Date: Mon, 02 Oct 2023 16:11:59 +0200 In-Reply-To: <2014740.1696254593@sss.pgh.pa.us> References: <2014740.1696254593@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.48.4 (3.48.4-1.fc38) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2023-10-02 at 09:49 -0400, Tom Lane wrote: > Laurenz Albe writes: > > CREATE FUNCTION silly() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RETU= RN NULL; END;'; > > CREATE TRIGGER silly BEFORE DELETE ON child FOR EACH ROW EXECUTE FUNCTI= ON silly(); >=20 > > The trigger function cancels the cascaded delete on "child", and we are= left with > > a row in "child" that references no row in "parent". >=20 > Yes.=C2=A0 This is by design: triggers operate at a lower level than > foreign keys, so an ill-conceived trigger can break an FK constraint. > That's documented somewhere, though maybe not visibly enough. >=20 > There are good reasons to want triggers to be able to see and > react to FK-driven updates, so it's unlikely that we'd want to > revisit that design decision, even if it hadn't already stood > for decades. Thanks for the clarification. I keep learning. I didn't find anything about that in the documentation or the READMEs in the source, but perhaps I didn't look well enough. Yours, Laurenz Albe