public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tom Lane <[email protected]>
To: Mats Rydberg <[email protected]>
Cc: [email protected]
Subject: Re: GREATEST/LEAST ignores comparison operator volatility in contain_volatile_functions_walker
Date: Tue, 02 Jun 2026 15:21:45 -0400
Message-ID: <[email protected]> (raw)
In-Reply-To: <CABu=s9Uz29GRAu-xzHO142jFsWa4xpySUNRT6pxcyQpi3vjvkw@mail.gmail.com>
References: <CABu=s9Uz29GRAu-xzHO142jFsWa4xpySUNRT6pxcyQpi3vjvkw@mail.gmail.com>

Mats Rydberg <[email protected]> writes:
> GREATEST and LEAST (MinMaxExpr nodes) are missing from
> contain_volatile_functions_walker in src/backend/optimizer/util/clauses.c.
> OpExpr nodes are correctly handled -- the walker resolves the oprcode via
> set_opfuncid and checks func_volatile. But there is no MinMaxExpr case, so
> GREATEST/LEAST always appears function-free to the volatility checker
> regardless of the < or > operator's provolatile.

This is intentional, per the comment in
contain_mutable_functions_walker (which
contain_volatile_functions_walker refers to):

     * It should be safe to treat MinMaxExpr as immutable, because it will
     * depend on a non-cross-type btree comparison function, and those should
     * always be immutable.

A non-cross-type btree comparison function directly determines the
ordering of an index for its data type, so if it isn't immutable then
you can't rely on the index to be consistent.

> The practical consequence: a GREATEST/LEAST expression whose comparison
> operator is STABLE or VOLATILE is incorrectly treated as IMMUTABLE. The
> reproducer below demonstrates this via a generated column, which requires
> an IMMUTABLE expression. PostgreSQL accepts the column definition when it
> should reject it.

This reproducer depends on an invalid operator class.  The reason
why you can't make an operator class without superuser privilege
is that the system depends on them behaving per spec.  We'd try
to enforce that rather than just assume it, were it not for the
halting problem.

			regards, tom lane






view thread (2+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: GREATEST/LEAST ignores comparison operator volatility in contain_volatile_functions_walker
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox