CLOSE(7)

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

πŸ’… CLOSE(7) β€” bestie, it's literally in the PostgreSQL 13.23 docs

okay so first things first: this is the CLOSE page from the PostgreSQL 13.23 documentation. that's the whole vibe. let's get into it 🫢


✨ NAME ✨

CLOSE β€” close a cursor. that's it. that's the function. closing. a cursor. slay.


🌸 SYNOPSIS 🌸

CLOSE { name | ALL }

πŸ¦‹ DESCRIPTION πŸ¦‹

so CLOSE frees the resources associated with an open cursor. like, it's the cursor's ✨release era✨. and once the cursor is closed? no subsequent operations are allowed on it. none. zero. the door is shut, the lights are off, nobody's home πŸ’€. so yeah β€” a cursor should be closed when it's no longer needed. iconic cleanup behaviour, honestly.

now here's the part where the transactions get a little messy, iykyk:

  • Every non-holdable open cursor is implicitly closed when a transaction is terminated by COMMIT or ROLLBACK. no CLOSE needed, the universe handles it for you 🧚
  • A holdable cursor is implicitly closed if the transaction that created it aborts via ROLLBACK. okay king, tidy of it.
  • but if the creating transaction successfully commits, the holdable cursor remains open until an explicit CLOSE is executed, or the client disconnects. so bestie it just… stays. lurking. main character energy honestly πŸ‘‘
lowkey important: holdable cursors don't clean themselves up after a successful commit. that's YOUR job. you have to actually CLOSE it or disconnect. no cap.

πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name of an open cursor to close. one cursor, hand-picked, very deliberate, we love a targeted approach πŸ’–
  • ALL β€” close all open cursors. every single one. mass closure event. it's giving spring cleaning 🌸

πŸ”₯ NOTES πŸ”₯

okay random but crucial: PostgreSQL does not have an explicit OPEN cursor statement. i know. i know. 😭 a cursor is considered open when it is declared. that's the whole trick. so use the DECLARE(7) statement to declare a cursor β€” that's where the opening happens, bestie.

wanna see all your available cursors? πŸ‘€ just query the pg_cursors system view. free real estate. go look. it's giving transparency.

and here's the spicy caveat 🫦 β€” if a cursor is closed after a savepoint which is later rolled back, the CLOSE is not rolled back. meaning: the cursor remains closed. periodt. the rollback does NOT resurrect it. do not @ me.


πŸ’œ EXAMPLES πŸ’œ

close the cursor liahona:

CLOSE liahona;

and that's the whole example. one line. ate. πŸ”₯


🌈 COMPATIBILITY 🌈

CLOSE is fully conforming with the SQL standard ⭐. however β€” CLOSE ALL is a PostgreSQL extension. so that one's us. we invented that. you're welcome πŸ’…


πŸ“ SEE ALSO πŸ“

go read those too, bestie, they're the whole cursor friend group 🫢


and that's a wrap!! πŸŽ‰ huge thanks to the PostgreSQL 13.23 documentation, dated 2025 β€” that's who wrote this, that's who we're thanking, mwah πŸ’– bye bestie, close those cursors πŸŽ€βœ¨

πŸ“¦ where this page comes from