http://qs321.pair.com?node_id=1215456


in reply to Returning multiple rows from pgplsql to DBI

I discovered a postgres bug in rowtype

If you can explain what the bug is I'll have it fixed. (Please mention what platform and software versions are used)

Replies are listed 'Best First'.
Re^2: Returning multiple rows from pgplsql to DBI
by anonymized user 468275 (Curate) on May 30, 2018 at 13:52 UTC
    In the below code, the FOR doc IN SELECT ... loop was returning a NULL in doc_typeid - impossible because it has a foreign key constraint on it. By luck I found that modifying it to FOR doc in SELECT doc_id, doc_typeid magically forced the correct value into doc_typeid (luck because I didn't need to select doc_id). When I changed the declaration of doc to record instead of a rowtype and removed the kludgey doc_id from the SELECT, it also worked correctly. These different tests led me to conclude that something other than the list of selected columns in the SELECT was determining the result set - perhaps the columns as ordered in the doc table at the last creation or alter table event are driving the population of the rowtype variable instead. So perhaps because record does not depend on any tables, my workaround to replace the rowtype declaration with record worked first time.
    CREATE OR REPLACE FUNCTION public."doct_GetActMask"( parm_holid bigint, parm_holtype character varying) RETURNS integer AS $BODY$DECLARE doc document%ROWTYPE;; actmask integer := 0; doctpid integer := NULL; approved integer := NULL; verified integer := NULL; logok integer := NULL; BEGIN SELECT docs_id FROM public."DocumentStatus" WHERE docs_shortname = 'Approved' INTO approved; SELECT docs_id FROM public."DocumentStatus" WHERE docs_shortname = 'Verified' INTO verified; FOR doc IN SELECT doc_type_id FROM document WHERE doc_hol_id = parm_holid AND (doc_docsid = approved OR doc_docsid = verified) LOOP SELECT public."doct_GetRoot"(doc.doc_type_id) INTO doctpid; IF doctpid < 0 THEN RETURN doctpid; END IF; actmask := actmask | (1<<(doctpid-1)); END LOOP; RETURN actmask; END$BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public."doct_GetActMask"(bigint, character varying) OWNER TO mydbo;

    One world, one people