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.96) (envelope-from ) id 1wB4je-000iBx-1K for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 05:48:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wB4ja-009QfV-0n for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 05:48:51 +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.96) (envelope-from ) id 1wB4jZ-009QfM-2p for pgsql-hackers@lists.postgresql.org; Fri, 10 Apr 2026 05:48:50 +0000 Received: from mail-oi1-x229.google.com ([2607:f8b0:4864:20::229]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wB4jY-00000000Ij3-224o for pgsql-hackers@lists.postgresql.org; Fri, 10 Apr 2026 05:48:50 +0000 Received: by mail-oi1-x229.google.com with SMTP id 5614622812f47-46aa216a65aso937810b6e.1 for ; Thu, 09 Apr 2026 22:48:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775800126; cv=none; d=google.com; s=arc-20240605; b=JXTV0Wc/TkM+cBSXLddJZ48oudokprS/4O3FUIL0Js3DkxhtrM+VAJnzZt7TG0IM1Z vZX/rLpdrHViUH3aJCXdJZypkeHbtdn/VPIwUneiI4V1R4Ka/avb8QWTEpHmmem0fx3w USXY09D1tDOPp7/uDyd3igv5p3no/BhCj/z2Q1EKMxKYgbwK75o0YKssUISHe1r6Gu6s Ujr2iF+If81om1/Avc6n2X3l+s9v4g52MFnWyrhcQWh4HzIMVoFFOuUk9GWnWtCcSHez wWRmmF6R9pFX66Cfh9B0lqQ+/hn9rZiLl8ZzILKtNP+6SSWN+/3oi3MTQ8iWHSGxaNrF abig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=ZospMsQ1Nu+z9ujDY1fwh9PINTom6fjNYq/baXynDQg=; fh=Ps3AfkWHPuYzcMare4W7RBMqe6UE+f1/ZX1ZmISZCXQ=; b=Vbb1Law8IT6rKZ/ONY++DPhuqr6D1NeWD7rfeEYoI84c16tobUaKGCuXhnYDMY2qW3 zfp95iKuswLM57Z1Np+Jof2Zubtpys4zUdMcNODLq06HhbZWDlwS8og+/MmQkwS43cWu fKGveGY9nqpW7UsOqbIoeUGrviY5V2ABn3yufSDZll1lZlS4BMp5nJviXJoxm2r0UWW8 lc05OeEPYbL5dPW+hKXANCpeXnDw9ZGeOWSxdbizNt2oFDS63joajZNN6+iS48VvLZZm rj2g6Pd0Jz5TII8BJyfo1trJXav8kdrpeZShouq5xX8MhKkZwgwE7W8yKSJXA17DFKBn 8p7g==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775800126; x=1776404926; 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=ZospMsQ1Nu+z9ujDY1fwh9PINTom6fjNYq/baXynDQg=; b=dCP8palMHjy7CU8P55lEEpSFqFy6GxcUPkdhCqBfLw0OtLNWQwKLMu+2bl0+rgDb2R HqJDPG+9mVijdIhn62c0h6sRWhuTzSMGK8FOJSaT6oBdOWb9prxtLKRw2APwzJckJxdk MT2AYlO4YleaT67HALq21PfpOB3hKK1+m5BdiSnUxMP9S+gxSIG1+k42ulbEKQjg7wlV sh6FIKJ8tu2rBHPP9w0lfT+YgHn1RWsLihKtPNFr7FHcJ6YF91aulRDDiodSiaiozPpD h6pwwj9wAX5OoVRUXtieZh+/AGwKAAZInf9mHbUZpSvgT6f9VJAiVivx1qZP14D4X45s 4Shg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775800126; x=1776404926; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ZospMsQ1Nu+z9ujDY1fwh9PINTom6fjNYq/baXynDQg=; b=q0psc0pHWQTDnlUtnasKG09VNrIzS2lTT1NY+Ajfc4YpqRRN1Z5Qn+0CGolYrDmHpn EixqDGG4f4i2+hiIF13FyT1E8OPqu3aXhtSEr9rxohEUPiI+04945LXlVS2cjSlBWE6P tb8YmqWWlhCM0hzGvybbfKPbQ1rR4jzDYQMIwEjSzfL3T2VXSNGISMnwg9ZuIbkWy8vK iVsFo8YICqmg6VMOlN0dIlf8fsNOZrfOWAzGzbL2PTvdCxZuQ1i5VvBaPM7DrPJF0nOU VI02zJhRKjreTQnlH1I12ZXjravaB8DncvqeCTr98Toz2NkXxZ8+ryeTI8mQ3pfJGX0L gPEw== X-Forwarded-Encrypted: i=1; AJvYcCWncLIj6M3bpTlHf13CC99vmzoQlUbhCmj727cwuwQlFc89e9y2NLu4nfqZgLUPIsEDtG1m3JCvZ0ImdS0X@lists.postgresql.org X-Gm-Message-State: AOJu0Yxhrof9Hgl+4enUv93MSZ2wTKo6Du5IpO0ck6392Ep0Z6SMzU2V sNY4k0ZLnb5dzxPHVDu+RBZBqw3TGdXYwbOKRKhDOQGqZlC53f2yGhfSLNwcQhGVnyhmJgUMT/W jwLDAPZHzVwVQpDwd9zSfwYfcnONNyW8= X-Gm-Gg: AeBDietfodXHAC4j4C9i27HnzGpgdR1i5Ulf7kq/HyXajpwbQpBHhqvqySHndUxDgis PGK901qZO3SBB9V6fXXqz7Lk9fQkEl7bjIbfAo8bQOQ5eHCuGf4oJvyduCqrOOIxmr9h1OEyE2p Vb58nDn9tBxg7z80y26HbAhNd9mq9L3tXDGdXzf/QPKxhfy825PJ8SAkDGOkMIlJSrx8TjzMhAb 1fEny/YoWFjQJ+ixaTl5XnIJDygpr+ICVQ4kpa2B5weeotjNdHwFbA8GZ+97qm+7cadgi7zHoJG pOTixFAlsfFIgwKbJak3 X-Received: by 2002:a05:6808:191a:b0:45a:6cfe:b00 with SMTP id 5614622812f47-478a080b963mr999380b6e.43.1775800126316; Thu, 09 Apr 2026 22:48:46 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Guo Date: Fri, 10 Apr 2026 14:48:34 +0900 X-Gm-Features: AQROBzAReCjZMlybOfR_1BtHiP4cAO1esCEPQowItq_Ftg5vT_Vugmg0OgbdSos Message-ID: Subject: Re: Bug: var_is_nonnullable() gives wrong results for old/new in RETURNING To: Tender Wang Cc: SATYANARAYANA NARLAPURAM , PostgreSQL Hackers 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 Fri, Apr 10, 2026 at 10:30=E2=80=AFAM Tender Wang w= rote: > SATYANARAYANA NARLAPURAM =E4=BA=8E2026=E5=B9= =B44=E6=9C=8810=E6=97=A5=E5=91=A8=E4=BA=94 02:43=E5=86=99=E9=81=93=EF=BC=9A > > It appears the optimizer incorrectly simplifies old. IS NULL to FA= LSE in RETURNING clauses when the underlying column has a NOT NULL constrai= nt. > > > > The issue is that var_is_nonnullable() in clauses.c doesn't check Var.v= arreturningtype. It sees a NOT NULL column and concludes the Var can never = be NULL. > > But this assumption is wrong for old.* and new.* references. Because th= e old tuple doesn't exist on INSERT, and the new tuple doesn't exist on DEL= ETE Nice catch. > Yes, the current var_is_nonnullable() ignores this case. The > attached patch seems ok to me. The patch also LGTM. I also checked if has_notnull_forced_var() has the same issue, but it doesn't: Vars with non-default returning type only appear in the RETURNING clause, so they never show up in WHERE/ON clauses. - Richard