Re: Possible bug (or at least unexpected behavior)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Adam Mackler <adam(at)mackler(dot)email>
Cc: "pgsql-sql(at)lists(dot)postgresql(dot)org" <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: Possible bug (or at least unexpected behavior)
Date: 2022-08-09 21:10:57
Message-ID: 84934.1660079457@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I wrote:
> ... It looks like memory management
> in SQL functions is not coping well with expanded arrays, but I'm
> not quite sure where it's going off the rails.

It doesn't seem to be a memory management problem, but rather that
SQL functions are being careless with arguments that are read/write
expanded Datums. A function that is passed such an argument is
allowed to modify it in-place, and array_append does so to reduce
the expense of repeatedly appending to an array value. If you
have two array_append's referencing the same parameter in a SQL
function, and that parameter is passed in as a R/W datum, you
get the wrong answer: the second array_append will see the effects
of the first one. Also, if the SQL function does array_append
first and array_cardinality second, array_cardinality reports the
wrong result.

Now, it doesn't look like your example function does either of those
things, but it turns out that it does after function inlining. The
planner effectively flattens out one level of the recursion, creating
a plan in which we do have these hazards.

The only practical fix I can see is to force SQL function parameter
values to read-only. We could do better if we knew which parameters
are actually multiply referenced in the function, but we don't have
the infrastructure needed to detect that. I'm not convinced that
it'd be appropriate to expend a lot of effort here --- non-inlined
execution of a SQL function is a pretty low-performance situation in
any case. So I've just gone for the simplest possible fix in the
attached.

regards, tom lane

Attachment Content-Type Size
fix-sql-function-rw-parameters.patch text/x-diff 4.3 KB

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message John Ericson 2022-08-09 21:31:23 Alternative to "AT TIME ZONE" that is less of a foot-gun?
Previous Message Carl Sopchak 2022-08-08 16:30:50 Re: select items based on 2 columns