Unix Technical Forum

stored function - array parameter - how many element in array ?

This is a discussion on stored function - array parameter - how many element in array ? within the Pgsql General forums, part of the PostgreSQL category; --> hello, the question is very clear .. when we write stored function, we can use array parameter as a ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 11:36 AM
=?UTF-8?Q?=C3=96zg=C3=BCr_Tu=C4=9Frul?=
 
Posts: n/a
Default stored function - array parameter - how many element in array ?

hello,

the question is very clear .. when we write stored function, we can use
array parameter as a variable .. but, in the function, how do i know how
many element in that array ?

i want to perform some dml operations about each one like (delete, update or
delete)

can anyone show me the example or tell me the function name i should use ..

regards
---------------------------------------------------------------------------------------------------------------
There is no e-mail anymore .. There is Gmail

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 11:36 AM
Merlin Moncure
 
Posts: n/a
Default Re: stored function - array parameter - how many element in array ?

On 7/13/06, Özgür Tuðrul <ozgur.tugrul@gmail.com> wrote:
>
> hello,
>
> the question is very clear .. when we write stored function, we can use
> array parameter as a variable .. but, in the function, how do i know how
> many element in that array ?
>
> i want to perform some dml operations about each one like (delete, update or
> delete)
>
> can anyone show me the example or tell me the function name i should use ..
>



have you looked at array_upper/array_dims? you can see an example of
array_upper here:
http://people.planetpostgresql.org/m...th-arrays.html

(shameless plug)

merlin

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

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 11:37 AM
Claire McLister
 
Posts: n/a
Default Re: stored function - array parameter - how many element in array ?

Have you considered using a set instead? We had a similar need and
were using an array as a parameter. That turned out to be taking too
long. Recently we have changed it to a set and it seems to work
faster, although I must admit I haven't timed it yet. In the first
case you call it by "select deleteEntries(1, '{1, 2, 3}'));" and in
the second, "select deleteEntries(1, '(1, 2, 3)');"

The first one was defined as:

CREATE OR REPLACE FUNCTION deleteEntries(int, integer[])
RETURNS Void
AS
'
DECLARE
G ALIAS FOR $1;
Entries ALIAS FOR $2;
ThisEntryId Integer;
BEGIN
IF array_lower(Entries, 1) is NULL THEN
RETURN ''True'';
END IF;
FOR I IN array_lower(Entries, 1)..array_upper(Entries, 1) LOOP
ThisEntryId := Entries[i];
DELETE FROM Details WHERE id = ThisEntryId;
END LOOP;
RETURN;
END;
'
LANGUAGE plpgsql
STABLE
RETURNS NULL ON NULL INPUT;


The new one is defined as:

CREATE OR REPLACE FUNCTION deleteEntries(int, varchar)
RETURNS Void
AS
'
DECLARE
G ALIAS FOR $1;
Entries ALIAS FOR $2;
BEGIN
EXECUTE ''DELETE FROM Details WHERE id IN '' || Entries;
RETURN;
END;
'
LANGUAGE plpgsql
STABLE
RETURNS NULL ON NULL INPUT;

On Jul 13, 2006, at 5:38 AM, Özgür Tuğrul wrote:

> hello,
>
> the question is very clear .. when we write stored function, we can
> use array parameter as a variable .. but, in the function, how do
> i know how many element in that array ?
>
> i want to perform some dml operations about each one like (delete,
> update or delete)
>
> can anyone show me the example or tell me the function name i
> should use ..
>
> regards
> ----------------------------------------------------------------------
> -----------------------------------------
> There is no e-mail anymore .. There is Gmail


--
Claire McLister mclister@zeesource.net
1684 Nightingale Avenue Suite 201
Sunnyvale, CA 94087 408-733-2737(fax)

http://www.zeemaps.com



---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 11:37 AM
Volkan YAZICI
 
Posts: n/a
Default Re: stored function - array parameter - how many element in array ?

On Jul 13 08:28, Claire McLister wrote:
> Have you considered using a set instead? We had a similar need and
> were using an array as a parameter. That turned out to be taking too
> long. Recently we have changed it to a set and it seems to work
> faster, although I must admit I haven't timed it yet.


Did you experience same results when you use an indexable (integer)
array type supplied by intarray contrib module? Furthermore, there're
lots of useful procedures and operators which supplies any kind of
functionality you'll ever need with arrays. Also it's obviouse that
there's no need to tell that these libraries are written in C with quite
optimized algorithms.


Regards.

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:23 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com