I created a PL/pgSQL function in PostgreSQL 10.4 using pgAdmin. It returns a query and updates entries in the same table. Called in pgAdmin, it runs as expected. When run from external code, the table is returned, but the updates never run. Wondering if this has to do with how I wrote the function?
CREATE OR REPLACE FUNCTION report()
RETURNS TABLE(id text, t1 text, t2 text)
LANGUAGE 'plpsql'
AS $BODY$
DECLARE
rec RECORD;
BEGIN
RETURN QUERY
SELECT id,
name AS t1,
data AS t2
FROM table1
WHERE status IS NULL;
IF FOUND THEN
FOR rec IN SELECT id
FROM table1
WHERE status IS NULL
LOOP
UPDATE table1 SET val=val+1
WHERE id=rec.id;
END IF;
RETURN;
END
$BODY$;
EDIT:
Thanks for the replies. Don't have anyone else to help take a look, so this was very helpful with troubleshooting what was going on.
Was so fixated on my PL/pgSQL function as having the issue, I overlooked my external program running the function. User error on my part. After moving my UPDATE above my SELECT, I noticed my program was seeing the UPDATE. I was forgetting to COMMIT the updates back to the database in my external program, so the table was never updating. Added a commit and good to.