dbTalk Databases Forums  

Simple function question

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Simple function question in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sean Davis
 
Posts: n/a

Default Simple function question - 10-26-2004 , 07:12 AM






I would like to create a function that accepts an array of IDs
(integers) and loops over them, returning the the rows (setof
rowtype...?) that match them. I have tried to no avail to form such a
function. As a final extension, I would like to be able to return rows
formed by a join across a few tables (return a setof RECORD type?). I
suppose I can just create the view I like then use a similar function
to the single-table version....

Thanks,
Sean



---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org


Reply With Quote
  #2  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: Simple function question - 10-26-2004 , 08:29 AM






On Tue, Oct 26, 2004 at 07:12:36AM -0400, Sean Davis wrote:
Quote:
I would like to create a function that accepts an array of IDs
(integers) and loops over them, returning the the rows (setof
rowtype...?) that match them.
I'm not certain what you're asking for, but perhaps this example
will be useful:

CREATE TABLE product (
prodid SERIAL PRIMARY KEY,
prodname VARCHAR(64) NOT NULL
);

CREATE FUNCTION prodlist(INTEGER[]) RETURNS SETOF product AS '
SELECT * FROM product WHERE prodid = ANY($1)
' LANGUAGE SQL;

INSERT INTO PRODUCT (prodname) VALUES ('Widget');
INSERT INTO PRODUCT (prodname) VALUES ('Gizmo');
INSERT INTO PRODUCT (prodname) VALUES ('Gadget');
INSERT INTO PRODUCT (prodname) VALUES ('Dohickey');
INSERT INTO PRODUCT (prodname) VALUES ('Thingamajig');
INSERT INTO PRODUCT (prodname) VALUES ('Whatsit');

SELECT * FROM prodlist(ARRAY[2,4,6]);

Quote:
As a final extension, I would like to be able to return rows
formed by a join across a few tables (return a setof RECORD type?). I
suppose I can just create the view I like then use a similar function
to the single-table version....
You could return SETOF RECORD but then your queries will need to
provide a column definition list. Another way would be to create
a custom type that describes a result record and return SETOF that
type. But before you do any of this, perhaps you should think about
whether you really need a function at all, or whether you can use
views and WHERE clauses.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
  #3  
Old   
Sean Davis
 
Posts: n/a

Default Re: Simple function question - 10-26-2004 , 09:49 AM



Quote:
You could return SETOF RECORD but then your queries will need to
provide a column definition list. Another way would be to create
a custom type that describes a result record and return SETOF that
type. But before you do any of this, perhaps you should think about
whether you really need a function at all, or whether you can use
views and WHERE clauses.

Michael,

Thanks for the reply. The reason all of this comes up is that I have
an application in which the user can create "sets" of IDs. I then want
to be able to do logical operations on the sets of IDs and then return
the database objects based on the ID's in those sets. I tried the
function version using "ANY" and the simple query using "IN" and found
an order of magnitude difference in speed (IN is faster). So, it
appears that using views and where clauses is the way to go here. One
last question--is there a limit to the length of a SQL query (in terms
of characters), as some of these sets could be very large (up to 40000
integers)?

Thanks again.

Sean


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #4  
Old   
Sean Davis
 
Posts: n/a

Default Re: Simple function question - 10-26-2004 , 10:13 AM




On Oct 26, 2004, at 9:49 AM, Sean Davis wrote:

Quote:
You could return SETOF RECORD but then your queries will need to
provide a column definition list. Another way would be to create
a custom type that describes a result record and return SETOF that
type. But before you do any of this, perhaps you should think about
whether you really need a function at all, or whether you can use
views and WHERE clauses.


Michael,

Thanks for the reply. The reason all of this comes up is that I have
an application in which the user can create "sets" of IDs. I then
want to be able to do logical operations on the sets of IDs and then
return the database objects based on the ID's in those sets. I tried
the function version using "ANY" and the simple query using "IN" and
found an order of magnitude difference in speed (IN is faster). So,
it appears that using views and where clauses is the way to go here.
One last question--is there a limit to the length of a SQL query (in
terms of characters), as some of these sets could be very large (up to
40000 integers)?

Sorry. Answered (partially) my own question. max_expr_depth is set to
10000 as default on my MacOS installation (7.4.3).

Sean


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #5  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: Simple function question - 10-27-2004 , 09:25 AM



On Tue, Oct 26, 2004 at 09:49:43 -0400,
Sean Davis <sdavis2 (AT) mail (DOT) nih.gov> wrote:
Quote:
Thanks for the reply. The reason all of this comes up is that I have
an application in which the user can create "sets" of IDs. I then want
to be able to do logical operations on the sets of IDs and then return
the database objects based on the ID's in those sets. I tried the
function version using "ANY" and the simple query using "IN" and found
an order of magnitude difference in speed (IN is faster). So, it
appears that using views and where clauses is the way to go here. One
last question--is there a limit to the length of a SQL query (in terms
of characters), as some of these sets could be very large (up to 40000
integers)?
For this kind of task you probably don't want to use arrays. There should
be a table matching sets to the ids that are in them. Then you would be able
to get the information you want using normal queries.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.