Skip site navigation (1) Skip section navigation (2)

Re: SRF's + SPI

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Eric B(dot)Ridge" <ebr(at)tcdi(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: SRF's + SPI
Date: 2005-04-01 20:55:47
Message-ID: 28484.1112388947@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-hackers
"Eric B.Ridge" <ebr(at)tcdi(dot)com> writes:
> Like I said, everything 
> usually works without problems, but from time to time it crashes.  

If you rebuild with --enable-cassert, does the crash get more
reproducible?  (Personally I would never consider developing any
backend C code without that turned on.)  It sounds like a use-of-
already-freed-memory bug, and if so --enable-cassert will help you
find it by clobbering memory blocks immediately during pfree.

> Below is the basic outline of my code.

I think you've forgotten that SPI_connect() and SPI_finish change
memory contexts.  The stuff you allocated in your startup step
goes away as soon as you SPI_finish, leaving dangling pointers that
SRF_RETURN_DONE tries to clean up.

I'm also pretty uncomfortable with the fact that you're returning out
of your function while still connected to SPI.  That would certainly
cause problems for anything else trying to use SPI in the same query.

You would probably be better off using the return-a-tuplestore mechanism
that plpgsql uses for set-valued results.  That way you could connect
to SPI, issue your query, push the tuples into the tuplestore,
disconnect from SPI, and be done with only one pass through the
function.  This would be a tad inefficient with a huge number of tuples
of course ... but you could convert the operation to use a SPI cursor
and thereby avoid duplicate storage of the tuples.

We didn't have tuplestores at the time the SPI API was designed.  I
wonder whether we shouldn't extend SPI to have an option to return
tuples into a caller-provided tuplestore, instead of a SPI_tuptable.

			regards, tom lane

In response to

Responses

pgsql-hackers by date

Next:From: Eric B.RidgeDate: 2005-04-01 21:29:31
Subject: Re: SRF's + SPI
Previous:From: Mark WoodwardDate: 2005-04-01 18:50:35
Subject: Re: ARC patent

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group