By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,930 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,930 IT Pros & Developers. It's quick & easy.

plpgsql: return multiple result sets

P: n/a
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
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
--- Oksana Yasynska <ok****@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

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2

P: n/a
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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #3

P: n/a
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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #4

P: n/a
--- Oksana Yasynska <ok****@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

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #5

P: n/a
Jeff,

thank you for the time and suggestion.
I'm also trying to use SETOF custom_type as a solution

Oksana

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.