Re: trigger errors - Mailing list pgsql-general

From Ron Peterson
Subject Re: trigger errors
Date
Msg-id 3948F724.7459DB56@yellowbank.com
Whole thread Raw
In response to trigger errors  (Marc Britten <mbritten@cybernet-usa.com>)
List pgsql-general
Marc Britten wrote:
>
> hi again,
>
> i created a trigger on a table for insert or delete, and a function that
> gets created fine.
>
> however when i go to insert something into the table i get
>
> NOTICE:  plpgsql: ERROR during compile of create_count_cache near line 2
> ERROR:  parse error at or near ";"

Hi Marc,

Try this function instead:

CREATE FUNCTION create_count_cache()
RETURNS opaque AS '
    BEGIN
        DELETE FROM LangCount;
        INSERT INTO LangCount
            SELECT LangID as ID, COUNT(LangID) AS CNT
            FROM snippet
            GROUP BY LangID;
        DELETE FROM CatCount;
        INSERT INTO CatCount
            SELECT LangID as ID, COUNT(LangID) AS CNT
            FROM snippet
            GROUP BY LangID;
        RETURN NEW;
    END;'
LANGUAGE 'plpgsql';

There are two changes from what you have.  You cannot use BEGIN/COMMIT
(i.e. transactions) within a function or trigger.  In plpgsql, use BEGIN
and END to delimit your function statements.  There is more information
about this in the PL/pgSQL portion of the user's guide.  The function
also needs to return something.  A return type of 'opaque' doesn't mean
the function returns nothing, it means the function doesn't return a
defined SQL datatype.

________________________
Ron Peterson
rpeterson@yellowbank.com

pgsql-general by date:

Previous
From: "Patrick FICHE"
Date:
Subject: Performance for indexes on functions
Next
From:
Date:
Subject: libpgtcl.so missing from pgaccess on FreeBSD4.0?