Avoid evaluating FOR PORTION OF bounds more than once

From: Paul A Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>
Subject: Avoid evaluating FOR PORTION OF bounds more than once
Date: 2026-09-04 20:21:33
Message-ID: CA+renyU4yDpiG-drzF40GfVxiMpmByDQEDZwpxODYa5ztwP6Jw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Hackers,

Here is a fix for finding D9 from [0].

Currently we can evaluate the expressions in FOR PORTION OF more than
once. If a function is declared STABLE but isn't really, that can
cause inconsistent results. Another way to reach the problem is by
using current_setting, and then calling set_config (for instance from
a trigger). I can't find any other avenues besides those two. I also
tried EvalPlanQual and a STABLE function reading from a table that
gets modified mid-statement by a trigger.

Arguably this is not really a bug, and the fix is somewhat involved
(using a PARAM_EXEC slot to pass the value around). But I wanted to
share a patch in case others think it needs to be fixed. It might
still be worth doing (though not in v19 IMO), since it saves an expr
evaluation every row. I skipped `Backpatch-through: 19` on this patch,
but I'll add it to future versions if we want it in this release.

[0] https://www.postgresql.org/message-id/CA%2BrenyV6QLOJYmLo3gbsg1Y%2BCrho8NqME1jJXgPbO_NgxfBaKQ%40mail.gmail.com

Yours,

--
Paul ~{:-)
pj(at)illuminatedcomputing(dot)com

Attachment Content-Type Size
v1-0001-Evaluate-the-FOR-PORTION-OF-target-only-once.patch text/x-patch 12.6 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Kirk Wolak 2026-09-04 20:22:49 Re: [PATCH v1 0/7] Wait event timing and tracing instrumentation
Previous Message Bharath Rupireddy 2026-09-04 19:59:18 Re: REPACK (CONCURRENTLY) rewrites tables marked with user_catalog_table