If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Fetch large result set from table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-11, 06:59
waltari2001 waltari2001 is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
Fetch large result set from table

Hi,

I've got a table that contains 20+ millions records and I'm trying to dump its data to a file. I've tried to use the cursor approach because of the memory issues.

How do you keep looping on the cursor until it reaches the end of the result set?

Here's my code

CREATE OR REPLACE FUNCTION gelargedata()
RETURNS SETOF mytable AS $$
DECLARE
cur refcursor;
BEGIN
OPEN cur FOR SELECT * FROM mytable;
LOOP
FETCH FORWARD 1000 FROM cur;
IF not found THEN
exit;
ELSE
RETURN NEXT;
END IF;
END LOOP;
CLOSE cur;
END;
$$ LANGUAGE 'PLPGSQL';

I get this error:

ERROR: expected FROM or IN at or near "1000"....

Can one uses FETCH in a loop?

Any help if very much appreciated.


Eric
Reply With Quote
  #2 (permalink)  
Old 06-27-11, 07:06
waltari2001 waltari2001 is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
Anything guys?
Reply With Quote
  #3 (permalink)  
Old 06-27-11, 19:52
someidiot someidiot is offline
Registered User
 
Join Date: Mar 2011
Posts: 27
Not very familiar with code like that, but can I ask why you don't just use pg_dump or psql to create a flat file?
Reply With Quote
  #4 (permalink)  
Old 06-28-11, 07:20
waltari2001 waltari2001 is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
I did try using pg_dump:

pg_dump -Oa -F p -U myuser -t mytable mydb
But I will need to extract my data between the two lines matching "COPY mytable ..." & "\."

I'd love to see how it's done using cursors.

Eric
Reply With Quote
  #5 (permalink)  
Old 06-30-11, 10:57
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
Quote:
Originally Posted by waltari2001 View Post
I've got a table that contains 20+ millions records and I'm trying to dump its data to a file. I've tried to use the cursor approach because of the memory issues.
Quote:
Originally Posted by waltari2001 View Post
But I will need to extract my data between the two lines matching "COPY mytable ..." & "\."
It's difficult to figure out exactly what you're trying to do.

If you're attempting to dump the database, pg_dump is the tool to use. The documentation provides guidance on how to deal with large databases. Dumping the database to a compressed or custom archive give you lots of options for restoring the database using pg_restore.

If you're attempting to "export" a specific table's data to a csv file, then you can write a copy script:
Code:
copy table mytable to stdout with csv header;
You can then pipe the output to a file with psql using the strategies outlined above.

Anything with a cursor or custom function, I'm guessing, would be orders of magnitude slower.
Reply With Quote
  #6 (permalink)  
Old 06-30-11, 13:26
waltari2001 waltari2001 is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
Thanks for the feedback futurity!.

I'm still very interested in figuring out a way to navigate a large table using cursors.

Last edited by waltari2001; 07-01-11 at 06:56.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On