CLOSE(7)

πŸ“¦ version 17.11, in opensuse-leap 16.0

πŸ’… CLOSE(7) β€” the "bye bestie" of cursors

Okay so real talk: this is the CLOSE page from the PostgreSQL 17.11 Documentation (yes, she's a 7 manual page, we love a section-seven moment), and it's literally just about hanging up on a cursor when you're done with it. Short page, immaculate vibes, zero wasted words. Let's get into it 🫢


✨ NAME ✨

CLOSE β€” close a cursor, bestie. That's it. That's the whole job.


✨ SYNOPSIS ✨

CLOSE { name | ALL }

Two ways to do it: name your cursor, or go full chaos mode with ALL. Choose your fighter πŸ’…


🌸 DESCRIPTION 🌸

CLOSE frees the resources associated with an open cursor. Once the cursor is closed, no subsequent operations are allowed on it β€” like, the door is shut, the lights are off, we don't live there anymore. So the move is: close it when you no longer need it. A cursor should be closed when it is no longer needed. (Said twice because it matters, okay? 😭)

Now here's the automatic-cleanup tea β˜• β€” some cursors close themselves and some need you to actually parent them:

  • Every non-holdable open cursor is implicitly closed when a transaction is terminated by COMMIT or ROLLBACK. Poof. Gone. You didn't even have to ask πŸ’€
  • A holdable cursor is implicitly closed if the transaction that created it aborts via ROLLBACK. So if it all goes sideways, she's out too.
  • But if the creating transaction successfully commits, the holdable cursor remains open until an explicit CLOSE is executed, or the client disconnects. That one is YOUR responsibility, main character. She's holdable, she's holding on, she's staying πŸŽ€

🌈 PARAMETERS 🌈

  • name β€” the name of an open cursor to close. Just the one. Your fave.
  • ALL β€” close all open cursors. Everyone out of the pool πŸŠβ€β™€οΈ

πŸ¦‹ NOTES πŸ¦‹

  • PostgreSQL does not have an explicit OPEN cursor statement. A cursor is considered open when it is declared. Use the DECLARE statement to declare a cursor. So there's no OPEN β€” she's open the moment she exists, iconic behaviour honestly ✨
  • You can see all available cursors by querying the pg_cursors system view. Peek behind the curtain, iykyk πŸ‘€
  • ⚠️ Okay this one's important: if a cursor is closed after a savepoint which is later rolled back, the CLOSE is not rolled back β€” that is, the cursor remains closed. The rollback does not resurrect her. No take-backs. πŸ’€
Not me warning you again: savepoint rollbacks do NOT reopen a cursor you already closed. She's closed closed. πŸ™ˆ

πŸ’– EXAMPLES πŸ’–

Close the cursor liahona:

CLOSE liahona;

That's it. One line. Slay. πŸ’…


πŸ‘‘ COMPATIBILITY πŸ‘‘

CLOSE is fully conforming with the SQL standard. πŸŽ€

CLOSE ALL is a PostgreSQL extension β€” so if you're bouncing between databases, know that this one's a homegrown flex, not a standard thing.


πŸ”₯ SEE ALSO πŸ”₯

Go read DECLARE(7) next if you wanna know how the cursor got here in the first place, bestie 🫢


Anyway, that's the page β€” PostgreSQL 17.11, dated 2026, CLOSE(7) in the PostgreSQL 17.11 Documentation. Closing this out the only way I know how: CLOSE ALL; and disconnect πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from