Search Postgresql Archives

Dumping rows into an array?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi

I'm new to Postgres, but I've been having fun with it.  In our
application we want to be able to store a variable number of
keywords for a record.

I first thought that an array column would be the way to go, but
after reading caveats on performance, I implemented they keywords
as a separate table.  This works great, but now I have another
array question.

I want to pass the keywords for the record as an array to a stored 
procedure (PL/pqsql), for inserting, and also return them as an array.

I got the input part working like this

   SELECT INTO len array_upper(kw, 1);
   FOR idx IN 1 .. len LOOP
      INSERT INTO keywords
         VALUES(DEFAULT, lid, kw[idx]);
   END LOOP;

kw is a TEXT[] parameter to the fuction.

I've been unable to come up with the counterpart to select the
keywords and populate an array that I can return..  

If I do

   DECLARE kwlist TEXT[];
...

   SELECT ARRAY[keyword] INTO keywordlist 


I get one keyword, cast as an array.

I've tried 

   DECLARE kwlist TEXT[];
   DECLARE kw RECORD;
...


    FOR kw IN SELECT keyword FROM keywords as kk
        WHERE lesson.id = kk.id
        ORDER BY id
        LOOP
          kwary[idx] := kw;
          idx := idx + 1;
        END LOOP;

But only one word gets returned in kwary[].  At least that's what I
get after doing

    lesson.keywordlist := kwary

Where lesson is the record I return from the function, and
keywordlist is a member of the record of type TEXT[].

Any ideas on how I might accomplish this?  I"m open to any
suggestions, including using a different way of passing the
keywords.  The only requirement I have is that I must be able to
pass in a variable length list of words, which I seem to have
solved, and that I need to be able to return a variable length list
fo words, which is driving me crazy.

Thanks for any pointers, 

-karl

PS, if it makes a difference, the application is  using Perl and 
DBD::Pg to query the database.  I see the same results when I call
the functions from psql.



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux