sql - Looping through relational table -
is possible loop through postgres while changing values of limit , offset. want loop through till end of relational table reached. reason want dynamically set limit , offset have little virtual memory of 1 gb due process getting killed.
currently approach taking manually set values of limit , offset.
select * my_relational_table limit 1000 offset 0; select * my_relational_table limit 1000 offset 1000; select * my_relational_table limit 1000 offset 2000; is possible automate process within postgres such loop stops when end of relational table reached.
yes, possible. can use cursor , command fetch.
- http://www.postgresql.org/docs/current/static/sql-declare.html
- http://www.postgresql.org/docs/current/static/sql-fetch.html
example:
postgres=# begin read only; begin postgres=# declare xx cursor select * generate_series(1,100); declare cursor postgres=# fetch 2 xx; generate_series ----------------- 1 2 (2 rows) postgres=# fetch 2 xx; generate_series ----------------- 3 4 (2 rows) postgres=# fetch 2 xx; generate_series ----------------- 5 6 (2 rows) postgres=# fetch 2 xx; generate_series ----------------- 7 8 (2 rows) postgres=# commit; commit
Comments
Post a Comment