Thread: cursor in plpgsql

cursor in plpgsql

From
"Grigoriy G. Vovk"
Date:
Sorry for my bad english!

Can I use "DECLARE CURSOR" in plpgsql function?
I have :
    create table ipacc(ip_from inet,
            sourse_ports int2,
            ip_to inet,
            dist_port,
            bytes int4,
            packets int4,
            when_time datetime)

I want to build report (I'm planing step):
    1. CURSOR select distinct ip_from from ipacc;
    2. FETCH from cursor into variable;
    3. select sum(bytes) from ipacc where ip_from = variable;
    4. Do while FETCH return row;
I do:
CREATE FUNCTION report() RETURNS text AS '
BEGIN WORK;
    DECLARE
        host_cursor CURSOR FOR SELECT DISTINCT ip_from FROM ipacc;
        host record;
        host_sum int4;
    BEGIN
        WHILE (FETCH FROM host_cursor INTO :host) LOOP;
        SELECT sum(bytes) from ipacc where ip_from=host;
        END LOOP;
    END;
psql report - CREATE
I do: select report();
psql report - NOTICE: plpgsql: ERROR during compile of report near line
3
ERROR: parse error at or near "CURSOR"

Where I do mistake?
--
---------
Grigoriy G. Vovk

Re: cursor in plpgsql

From
wieck@debis.com (Jan Wieck)
Date:
[Charset koi8-r unsupported, filtering to ASCII...]
> Sorry for my bad english!
>
> Can I use "DECLARE CURSOR" in plpgsql function?

    No,  it's  impossible and yes, that's bad because the backend
    has to load the entire result set into memory.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#========================================= wieck@debis.com (Jan Wieck) #