# What will happen if the sql cursor not be closed

**URL:** <https://discuss.elastic.co/t/what-will-happen-if-the-sql-cursor-not-be-closed/213137>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [December 27, 2019, 6:24am UTC](https://discuss.elastic.co/t/what-will-happen-if-the-sql-cursor-not-be-closed/213137 "2019-12-27T06:24:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwtony](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@gwtony](https://discuss.elastic.co/u/gwtony)\
**Post date:** [December 27, 2019, 6:24am UTC](https://discuss.elastic.co/t/what-will-happen-if-the-sql-cursor-not-be-closed/213137/1 "2019-12-27T06:24:36Z")

</div>

It wrote in Elasticsearch Reference that

Unlike scroll, receiving the last page is enough to guarantee that the Elasticsearch state is cleared.

I wonder that:

- Is there any memory leak or bad effection will happen if the sql cursor aways not be closed
- Is there any way to find how many cursors in use

Thank you

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [December 30, 2019, 10:55am UTC](https://discuss.elastic.co/t/what-will-happen-if-the-sql-cursor-not-be-closed/213137/2 "2019-12-30T10:55:13Z")

</div>

@gwtony the way this works in ES SQL is that when the user asks for the next page in the cursor, we look at the results size of this page and if there are no documents in it we, on purpose, call [clear scroll](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high-clear-scroll.html) before returning the response to the user.

If the last page is not reached, the cursor (or scroll context) will remain active for 45 seconds by default (`page_timeout` parameter from [https://www.elastic.co/guide/en/elasticsearch/reference/7.x/sql-rest-fields.html#sql-rest-fields](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/sql-rest-fields.html#sql-rest-fields)). There is a memory footprint for each scroll context kept alive in memory before the timeout runs out or before it's explicitly cleared, as explained in [https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-body.html#scroll-search-context](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-body.html#scroll-search-context). If you are using cursors and you want to manage the memory usage properly (recommended), you can clear the cursor with `/_sql/close` as explained [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/sql-pagination.html#sql-pagination).

If you want to find out the number of scroll contexts alive (a cursor is basically a scroll context), you can use: `GET /_nodes/stats/indices/search`. In the results, search for `scroll`.

---

<div class="post-metadata">

**Author:** ![gwtony](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@gwtony](https://discuss.elastic.co/u/gwtony)\
**Post date:** [January 10, 2020, 11:26am UTC](https://discuss.elastic.co/t/what-will-happen-if-the-sql-cursor-not-be-closed/213137/3 "2020-01-10T11:26:04Z")

</div>

Thank you for the answer.  
I have another question: it seems that the length of cursor id has relationship with field number, not a fixed length like scroll id. Is that a bug or designed for some reason

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 7, 2020, 11:26am UTC](https://discuss.elastic.co/t/what-will-happen-if-the-sql-cursor-not-be-closed/213137/4 "2020-02-07T11:26:06Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
