Re: Segment fault when excuting SPI function On PG with commit 41c6a5be - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Segment fault when excuting SPI function On PG with commit 41c6a5be
Date
Msg-id 1003166.1627657587@sss.pgh.pa.us
Whole thread Raw
In response to Re: Segment fault when excuting SPI function On PG with commit 41c6a5be  (Michael Paquier <michael@paquier.xyz>)
Responses Re: Segment fault when excuting SPI function On PG with commit 41c6a5be  (Daniel Gustafsson <daniel@yesql.se>)
List pgsql-hackers
Michael Paquier <michael@paquier.xyz> writes:
> On Fri, Jul 30, 2021 at 08:57:35AM +0000, liuhuailing@fujitsu.com wrote:
>> When I used SPI_execute_plan function on PG12 which commit 41c6a5be is used,
>> Segment fault occurred.

> I see nothing wrong in this commit, FWIW.
> But I see an issue with your code.  It seems to me that you should
> push a snapshot before doing SPI_prepare() and SPI_execute_plan(),
> as of:
> PushActiveSnapshot(GetTransactionSnapshot());

Yes.  What that commit did was to formalize the requirement that
a snapshot exist *before* entering SPI.  Before that, you might
have gotten away without one, depending on what you were trying
to do (in particular, detoasting a toasted output Datum would
fail if you lack an external snapshot).

This isn't the first complaint we've seen from somebody whose
code used to work and now fails there, however.  I wonder if
we should convert the Assert into an actual test-and-elog, say

    /* Otherwise, we'd better have an active Portal */
    portal = ActivePortal;
-    Assert(portal != NULL);
+    if (unlikely(portal == NULL))
+        elog(ERROR, "must have an outer snapshot or portal");
    Assert(portal->portalSnapshot == NULL);

Perhaps that would help people to realize that the bug is theirs
not EnsurePortalSnapshotExists's.

I gather BTW that the OP is trying to test C code in a non-assert
build.  Not a great approach.

            regards, tom lane



pgsql-hackers by date:

Previous
From: John Naylor
Date:
Subject: Re: A qsort template
Next
From: Daniel Gustafsson
Date:
Subject: Re: Segment fault when excuting SPI function On PG with commit 41c6a5be