Thread: plpgsql: return multiple result sets
Hi all, I'm running Postgres 7.2.1 and I need to return multiple row sets from plpgsql function. I'm new in plpgsql but according documentation and everything I could find in the mailing list I need to switch to 7.3 to get at least SETOF rows as a result. I can't really upgrade Postgres now. Is there is any a workaround idea to retrieve multiple rowsets? I have up to 50 tables in database to join and pass this data to the another application I had an idea to build a view and retrieve cursor on this view (if I stay with 7.2) or generate custom type based on the columns of all 50 tables and retrieve a SETOF custom type (if I use 7.3) Can anybody give me any suggestion? Thank you in advance, Oksana
--- Oksana Yasynska <oksana@athabascau.ca> wrote: > Hi all, > > I'm running Postgres 7.2.1 and I need to return > multiple row sets from plpgsql > function. I'm new in plpgsql but according > documentation and everything I > could find in the mailing list I need to switch to > 7.3 to get at least SETOF > rows as a result. > > I can't really upgrade Postgres now. Is there is any > a workaround idea to > retrieve multiple rowsets? > > I have up to 50 tables in database to join and pass > this data to the another > application > I had an idea to build a view and retrieve cursor on > this view (if I stay with > 7.2) or generate custom type based on the columns of > all 50 tables and > retrieve a SETOF custom type (if I use 7.3) > > Can anybody give me any suggestion? You can return a cursor from your function, which you can then use in your application. Sort of like: create function my_cursor_test(refcursor, integer) returns refcursor as 'begin open $1 as cursor for select * from mytable where id = $2; return $1; end;' language 'plpgsql'; Then call it like: begin; select my_cursor_test(mycursor, 1); select * from mycursor; (output comes here) end; Note the need to wrap the statements in an explicit transaction. With statements being autocommitted, the cursor would be closed immediately following the function call. Better check the syntax too, I just dashed that off (hey, it's Saturday). It's all there in the 7.2 docs under "procedural languages". __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com
Jeff, Sorry for the delay response. We have problems with our mail server and my first mail didn't get to the list. Thank you for the idea about cursor unfortunately it's not a right solution in my situation. I think I didn't explain properly what my problem is. It's obvious that Eng is not my native language:) I need to write a plpgsql function which returns information selected from the 50 tables (in the following example: title, descriptions and locations) to the other application. By the way, data has a tree structure. I have an idea to use function to build a temp table. Is it feasible to return temporary table as a plpgsql function result ? For example, 3 tables: CREATE TABLE "lom" ( "id" int4 DEFAULT nextval('"lom_id_seq"'::text) NOT NULL, "title" varchar(1000)); CREATE TABLE "description" ( "id" int4 DEFAULT nextval('"description_id_seq"'::text) NOT NULL, "lom_id" int4, "description" varchar(2000)); CREATE TABLE "location" ( "id" int4 DEFAULT nextval('"location_id_seq"'::text) NOT NULL, "lom_id" int4, "uri" varchar(1000)); With the following information: INSERT INTO "lom" ("id", "title") VALUES(948, 'title'); INSERT INTO "description" ("id", "lom_id", "description") VALUES(564, 948, 'description1'); INSERT INTO "description" ("id", "lom_id", "description") VALUES(565, 948, 'description2'); INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1258, 948, 'http://yahoo.ca - location1'); INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1259, 948, 'http://google.ca - location2'); Oksana
Jeff, Thank you for the idea about cursor unfortunately it's not a right solution in my situation. I think I didn't explain properly what my problem is. It's obvious that Eng is not my native language:) I need to write a plpgsql function which returns information selected from the 50 tables (in the following example: title, descriptions and locations) to the other application. By the way, data has a tree structure. I have an idea to use function to build a temp table. Is it feasible to return temporary table as a plpgsql function result ? For example, 3 tables: CREATE TABLE "lom" ( "id" int4 DEFAULT nextval('"lom_id_seq"'::text) NOT NULL, "title" varchar(1000)); CREATE TABLE "description" ( "id" int4 DEFAULT nextval('"description_id_seq"'::text) NOT NULL, "lom_id" int4, "description" varchar(2000)); CREATE TABLE "location" ( "id" int4 DEFAULT nextval('"location_id_seq"'::text) NOT NULL, "lom_id" int4, "uri" varchar(1000)); With the following information: INSERT INTO "lom" ("id", "title") VALUES(948, 'title'); INSERT INTO "description" ("id", "lom_id", "description") VALUES(564, 948, 'description1'); INSERT INTO "description" ("id", "lom_id", "description") VALUES(565, 948, 'description2'); INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1258, 948, 'http://yahoo.ca - location1'); INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1259, 948, 'http://google.ca - location2'); Oksana
--- Oksana Yasynska <oksana@athabascau.ca> wrote: > I need to write a plpgsql function which returns > information selected from the > 50 tables (in the following example: title, > descriptions and locations) to > the other application. By the way, data has a tree > structure. > > I have an idea to use function to build a temp > table. > Is it feasible to return temporary table as a > plpgsql function result ? Yes. This was a commonly used workaround before version 7.2.x. A couple of points to be aware of: * A temporary table persists only for (and is only available to) the current user session. * Better to use "EXECUTE" to create the table, as there are some potential gotchas with tables created by a function. Check the archives for plenty of examples. > > > For example, 3 tables: > CREATE TABLE "lom" ( > "id" int4 DEFAULT nextval('"lom_id_seq"'::text) > NOT NULL, > "title" varchar(1000)); > > CREATE TABLE "description" ( > "id" int4 DEFAULT > nextval('"description_id_seq"'::text) NOT NULL, > "lom_id" int4, > "description" varchar(2000)); > > CREATE TABLE "location" ( > "id" int4 DEFAULT > nextval('"location_id_seq"'::text) NOT NULL, > "lom_id" int4, > "uri" varchar(1000)); > > With the following information: > > INSERT INTO "lom" ("id", "title") VALUES(948, > 'title'); > > INSERT INTO "description" ("id", "lom_id", > "description") VALUES(564, 948, > 'description1'); > INSERT INTO "description" ("id", "lom_id", > "description") VALUES(565, 948, > 'description2'); > > INSERT INTO "location" ("id", "lom_id", "uri") > VALUES(1258, 948, > 'http://yahoo.ca - location1'); > INSERT INTO "location" ("id", "lom_id", "uri") > VALUES(1259, 948, > 'http://google.ca - location2'); > > > > Oksana > __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com
Jeff, thank you for the time and suggestion. I'm also trying to use SETOF custom_type as a solution Oksana