Question

Declare cursors

Posted March 13, 2020 432 views
DigitalOcean Managed PostgreSQL Database

I’m trying to declare CURSOR on table with ~9M records

DECLARE "name" NO SCROLL CURSOR WITH HOLD FOR SELECT ....

and it fails with error: terminating connection due to administrator command. No logs, nothing.

DigitalOcean, any suggestions?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @periparts,

You could try these 2 approaches and see if that helps.

  1. Optimize the query to reduce the sorting which will in turn reduce the amount of memory been used, you can do an EXPLAIN ANALYZE on the query to see the optimizers path as well as see the disk sorting done by the query

  2. Adjust the workmem, the default is 2547 KB you can change it on a session-level first for your test “ SET workmem = ‘80MB’; then "show work_mem” you should see 80MB (Note: You can modify the 80MB to what you like) after which you can execute your query.

The below URL has some good information on declare cursor memory, please review

https://www.cybertec-postgresql.com/en/declare-cursor-in-postgresql-or-how-to-reduce-memory-consumption/
https://stackoverflow.com/questions/33635405/postgres-cursor-with-hold
https://bytes.com/topic/postgresql/answers/420717-cursors-transactions-why
https://www.postgresql-archive.org/Correct-use-of-cursors-for-very-large-result-sets-in-Postgres-td5944846.html

Regards,
Rajkishore