Report this

What is the reason for this report?

Declare cursors

Posted on March 13, 2020

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?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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 work_mem, the default is 2547 KB you can change it on a session-level first for your test " SET work_mem = ‘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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.