[PATCH] Prevent replacement of a function if it's used in an index expression and is not IMMUTABLE

From: sundayjiang(蒋浩天) <sundayjiang(at)tencent(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: [PATCH] Prevent replacement of a function if it's used in an index expression and is not IMMUTABLE
Date: 2025-06-27 07:32:55
Message-ID: tencent_50E8D54549948E7B30830464@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

The purpose of this patch is to prevent replacing a function via `CREATE OR REPLACE FUNCTION` with a new definition that is not marked as `IMMUTABLE`, if the existing function is referenced by an index expression.

Replacing such functions may lead to index corruption or runtime semantic inconsistencies, especially when the function’s output is not stable for the same input.

This patch is motivated by our analysis of both recent and earlier PostgreSQL security vulnerabilities, notably:

- CVE-2020-25695: A privilege escalation issue caused by non-IMMUTABLE expressions.
- CVE-2024-1713: A similar privilege escalation issue related to the `plv8` extension, akin to CVE-2020-25695.

Although these CVE vulnerabilities have been fixed, we believe this patch enforces a stricter rule that further enhances PostgreSQL’s robustness:

If a function is used in an index, it can only be replaced if it is declared as `IMMUTABLE`.

This strategy aligns with PostgreSQL’s established assumption that `IMMUTABLE` functions are safe to use in indexes and their behavior should remain consistent after updates.

We hope this contribution benefits the community, and we welcome your valuable feedback.

Sincerely,&nbsp;
xiaojiluo (Tencent Yunding Lab)&nbsp;

Attachment Content-Type Size
0001-Prevent-replacement-of-a-function-if-it-s-used-in-an.patch application/octet-stream 3.4 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Zane Duffield 2025-06-27 07:42:41 Check for existing replication slot in pg_createsubscriber
Previous Message Yugo Nagata 2025-06-27 07:19:59 Re: JIT works only partially with meson build?