plpgsql: some 'DELETE' queries are returning ROWCOUNT = 0

From: Dragos Valentin Moinescu <dragos(dot)moinescu(at)gmail(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: plpgsql: some 'DELETE' queries are returning ROWCOUNT = 0
Date: 2010-11-01 12:45:31
Message-ID: AANLkTi=KXPn+squ9Rr1d1J_w_Ek9vNCuKTzKAmuibmeu@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hello,

I try to create some functions that will execute some queries stored
inside tables.
These 2 functions are:

[code]

CREATE FUNCTION __syncpg_execute1(sql text) RETURNS text AS $$
DECLARE
nraffected integer;
retcode text;
BEGIN
retcode := '';
EXECUTE sql;
GET DIAGNOSTICS nraffected = ROW_COUNT;
IF nraffected = 0 THEN
retcode = 'rows_affected = 0';
END IF;

RETURN retcode;
EXCEPTION
WHEN OTHERS THEN
RETURN SQLSTATE;
END;
$$ LANGUAGE plpgsql;

[/code]

and

[code]

CREATE FUNCTION __syncpg_execute() RETURNS integer AS $$
DECLARE
sqlrec text;
nrrows integer;
sqlins text;
errstr text;
BEGIN
nrrows := 0;

FOR sqlrec IN SELECT sql FROM __syncpg_toexecute LOOP
SELECT __syncpg_execute1(sqlrec) INTO errstr;
nrrows := nrrows + 1;
IF errstr <> '' THEN
sqlins = 'INSERT INTO __syncpg_toexecuteres VALUES (' ||
quote_literal(sqlrec) || ', ' ||
quote_literal(errstr) || ');';
EXECUTE sqlins;
END IF;
END LOOP;

TRUNCATE __syncpg_toexecute;

RETURN nrrows;
END;
$$ LANGUAGE plpgsql;

[/code]

The problem I have is that some DELETE FROM ... does not affect rows
even if those rows are there. I get the same issue with UPDATE ....

I haven't found issues with INSERT (there are lots of errors=23505 -
which is unique violation)

If I run that SQL by hand it runs successfully deleting/updating the
necessary rows.

Am I missing something?

Thank you

--
Best regards,
Dragos Moinescu

Browse pgsql-admin by date

  From Date Subject
Next Message A J 2010-11-01 16:08:18 Pagesize for large-objects (ONLY) database.
Previous Message Vibhor Kumar 2010-11-01 07:20:31 Re: Integrity Error