Re: GREATEST/LEAST ignores comparison operator volatility in contain_volatile_functions_walker

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mats Rydberg <mats(at)planetscale(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: GREATEST/LEAST ignores comparison operator volatility in contain_volatile_functions_walker
Date: 2026-06-02 19:21:45
Message-ID: 2949986.1780428105@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Mats Rydberg <mats(at)planetscale(dot)com> 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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-06-02 19:26:12 Re: BUG #19504: Segmentation fault in plpython3u function (pg19)
Previous Message Maxim Boguk 2026-06-02 18:51:43 Re: BUG #19505: Some weird spikes postgresql processes in database (up to 200k sometime) without apparent reasons.