CLOSE(7)
π 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
COMMITorROLLBACK. noCLOSEneeded, 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
CLOSEis 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
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms