Re: C-Functions using SPI - Missing Magic Block

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: C-Functions using SPI - Missing Magic Block
Date: 2010-07-05 10:49:53
Message-ID: 20100705104953.GM7584@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Jul 05, 2010 at 10:20:30AM +0200, Saitenheini(at)web(dot)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/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thom Brown 2010-07-05 11:40:22 Re: Out of memory on update of a single column table containg just one row.
Previous Message Zeeshan.Ghalib 2010-07-05 10:47:31 Out of memory on update of a single column table containg just one row.