Re: Getting ROW_COUNT from MOVE in 8.3

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Reuven M(dot) Lerner <reuven(at)lerner(dot)co(dot)il>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Getting ROW_COUNT from MOVE in 8.3
Date: 2010-10-25 07:29:34
Message-ID: 5682B8BD-4114-4EF0-B5DC-9497B7330BF5@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 24 Oct 2010, at 15:41, Reuven M. Lerner wrote:

> I've managed to improve things quite a bit by using cursors, but I've been stumped in trying to find a replacement for the COUNT(*). I wrote a function that works great on 9.0:
>
> CREATE OR REPLACE FUNCTION count_the_rows() RETURNS INTEGER AS $$
> DECLARE
> mycursor CURSOR FOR SELECT * FROM test_table;
> number_of_rows integer := 0;
> BEGIN
> OPEN mycursor;
> MOVE ALL IN mycursor;
> GET DIAGNOSTICS number_of_rows := ROW_COUNT;
> RETURN number_of_rows;
> END;
> $$ LANGUAGE 'plpgsql';
>
> Unfortunately, the project is using 8.3, and the function refuses to even compile, due to the "MOVE ALL". When we change it to something else (such as MOVE 20000000, which returns the actual number of rows skipped to psql), or ABSOLUTE -1, we get 0 back from ROW_COUNT.

I recall movable cursors weren't supported in pl/pgsql until 8.4.

You can use (movable) cursors in SQL though. That probably requires some client-side code, as things like GET DIAGNOSTICS aren't available in sql functions, but it may be of use to you. I've done this in a PHP web-application a couple of times.

Alban Hertroys

--
Screwing up is an excellent way to attach something to the ceiling.

!DSPAM:737,4cc531ef10289398819850!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Fredric Fredricson 2010-10-25 07:49:02 Re: What is "return code" for WAL send command
Previous Message Devrim GÜNDÜZ 2010-10-25 05:41:47 Re: about RPM build options