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 1vGIWu-00ByHG-2E for pgsql-general@arkaria.postgresql.org; Tue, 04 Nov 2025 15:01:03 +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 1vGIWs-00EVJG-LH for pgsql-general@arkaria.postgresql.org; Tue, 04 Nov 2025 15:01:01 +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 1vGIWs-00EVJ7-AU for pgsql-general@lists.postgresql.org; Tue, 04 Nov 2025 15:01:01 +0000 Received: from mail-oi1-x235.google.com ([2607:f8b0:4864:20::235]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vGIWo-005wxu-2f for pgsql-general@lists.postgresql.org; Tue, 04 Nov 2025 15:01:00 +0000 Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-4441ccde23dso2805724b6e.3 for ; Tue, 04 Nov 2025 07:00:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762268456; x=1762873256; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=xJKvyAw8XGatTQddBdkvG4UVh7ZW3BL3Blm41FRaDyo=; b=MT8rt/NVIZ8Wx6wBe9dQKnPpGllIcrSEFn9AaiKkpaAZ4cfZZCYhOuuGCg4gnpTO4x hDSWjzazFhWxWBZRqvxR9cigieQvBKJU+JAJ8W/GJJ3qPnhpmKato0RR1elH3VpylPkY vG24YVrsgkcemD6UU0jFwyfpxuO08WajQM9AQ53+07wh5ZrL2yAPm/Q8/bj/adOjdNhV 3O3qpX5v+FXZd/A5jAZJ78m/cLYri+ZBNpUSnPVW5OEycHPNvBcNC02G4awRuWFsc5hD D/gg8eXM+8hNAMriIW0sCzRKwUqw+h5UGDWMLSz8u0hWHHXoK5iYxtzrME8oyO4IX0TO /CDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762268456; x=1762873256; h=content-transfer-encoding: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=xJKvyAw8XGatTQddBdkvG4UVh7ZW3BL3Blm41FRaDyo=; b=aOK8m1M3/MqpNBIIPesVWjWbKZEMazoemM5rKEbEnxV2hMEadc/IvJq1jMTBA9adjC XQ/JRSY6begxMiE8jQvcTf8q6aJMObUJxhMlpruyjj8MfnffyWX/Kw+f87mf5um7IKCa FZjiC21A90RfXQ+fLpx5b0qateJ+QBaUlNb3swIjuy8Yo+pQTPDzd4idzrvOEXPFmatG 3pb3vcWYbI+z9HA5TcGYxJV5u5TLjRVW/TqikzJSb1GWrhWIhidKLTFtTRlna+Q2R6A6 9+6wTexb8erER+Az63A/GuEx18Om/5CZDt9xxXpgX/mZShi/Bfg8alTbGaTKr/qYx71f V5iA== X-Gm-Message-State: AOJu0YxUrfay4jQKXFtUpi+NpbTOraM0TpcgcM+O6SxBdhpFbRfgbLDZ XmSVCySpqrp5sJ2yYX/ieXN7Q+LO6yQ0R8IA8zuv8GPhp2LuCQMjcHfH1W2+xVQwkjm+WyR0XFh JcuiJkjUv8M9CiEs1QCYfLa5504oF0MPLZp5w X-Gm-Gg: ASbGncve4eUt139DyHf6vswb4T1Z/sVjgdUgcZCSQAOnR7fT0s6YiGO+zB6kY0+FTZI w7AGk475y5elfGNA1wmaXjFEeVudTmDTv8ItTeijCT5adHd8nqx/yczs7CF5ZJQ2ULKeIOwBrb1 +JdgY2cWZ3tjKsbqaXAVZIlAl11beFMCYaPYRQQlgzwCY6OuAlXJGticOPU9XseLrTCyn0yvvRu BN3kACcZOAoYcR0vbha2nEuBmvYr4Oifo7kzWDB8cwZfwkr6Gah0CbTaLfqpcDGTozQCdFY2w== X-Google-Smtp-Source: AGHT+IHLMH9dXd6T2m7RdTVYvb6pElmqlLP/TztoJrSOizw4oYiAUKQN82dG5dPtigxIKIeyG2EhTnhggta6bLJWruU= X-Received: by 2002:a05:6808:2912:b0:44f:e61d:188d with SMTP id 5614622812f47-44fe61d2629mr407879b6e.6.1762268454402; Tue, 04 Nov 2025 07:00:54 -0800 (PST) MIME-Version: 1.0 References: <616435641.255753519.1762260528389.JavaMail.zimbra@meteo.fr> In-Reply-To: <616435641.255753519.1762260528389.JavaMail.zimbra@meteo.fr> From: Dominique Devienne Date: Tue, 4 Nov 2025 16:00:42 +0100 X-Gm-Features: AWmQ_bkWiSADw5l3p5DS8QLD08mcWdgjnpI8wHVpx68_UgOQgmBz0J_ceQUrTHM Message-ID: Subject: Re: PostgreSQL trigger how to detect a column value explicitely modified To: PALAYRET Jacques Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Nov 4, 2025 at 1:49=E2=80=AFPM PALAYRET Jacques wrote: > In a trigger body, is there a simple way to know if a column value has be= en explicitely modified ? Using pg_trigger_depth(), you can know whether the trigger is called from "outer SQL" directly, or from SQL done within another trigger (because the depth will be larger). I didn't quite follow your description, to be honest, but I suspect the above is what you want (maybe :)). --DD PS: To illustrate, we have this trigger to enforce some of our tables are "trigger managed", and no DMLs should be done "directly" on them (only from triggers). FWIW. -= -DD PPS: pg_trigger_depth() is 0 if the trigger function is called directly (unusual). 1 if directly called from an "outer SQL" statement (from a proc/func or n= ot). 2 or more if triggered from SQL done by another (possibly the same) "triggered" trigger. CREATE FUNCTION trigger_managed_tf() RETURNS TRIGGER AS $$ BEGIN IF pg_trigger_depth() < 2 THEN RAISE EXCEPTION 'Direct insert/update/delete are not allowed on the % table.', TG_TABLE_NAME; END IF; RETURN COALESCE (NEW, OLD); END $$ LANGUAGE plpgsql