Re: C-Functions using SPI - Missing Magic Block - Mailing list pgsql-general

From Sam Mason
Subject Re: C-Functions using SPI - Missing Magic Block
Date
Msg-id 20100705104953.GM7584@samason.me.uk
Whole thread Raw
In response to Re: C-Functions using SPI - Missing Magic Block  (Saitenheini@web.de)
List pgsql-general
On Mon, Jul 05, 2010 at 10:20:30AM +0200, Saitenheini@web.de wrote:
> Datum count_person(PG_FUNCTION_ARGS) {
> SPI_connect();
> int ret = SPI_exec("SELECT count(*) FROM person", 0);
> SPI_finish();
> PG_RETURN_INT32(ret);
> }
>
> But I guess I still did something wrong, because no matter how many
> rows exist in my table "person" the result in always 5.

5 is actually the value you want to be getting back!

The problem is that you're treating the status value of SPI_exec as the
result of the query.  You need to check the result to see if there was
an error and only if it's OK can you call something like SPI_getvalue to
actually get the count out.  You could probably steal some code from:

  http://developer.postgresql.org/pgdocs/postgres/spi-examples.html

--
  Sam  http://samason.me.uk/

pgsql-general by date:

Previous
From: Saitenheini@web.de
Date:
Subject: Re: C-Functions using SPI - Missing Magic Block
Next
From:
Date:
Subject: Out of memory on update of a single column table containg just one row.