Welcome back to our series on PostgreSQL Cursors. As we continue exploring PostgreSQL Cursors, including Scrollable and Non-Scrollable Cursors, we're now focusing on With-Hold Cursors in this article. With-Hold Cursors in PostgreSQL provide a crucial mechanism for managing data, enabling users to maintain query result sets even beyond transaction commitments.
Data Navigation with PostgreSQL Cursors.

DECLARE cursor_name CURSOR WITH HOLD FOR SELECT * FROM table_name;
Here:
cursor_name is the name you give to your cursor.WITH HOLD tells PostgreSQL to keep the data even after you finish.SELECT statement gets the data you want to work with.Imagine we have a list of employees in a table, and we want to work with their data using a With-Hold Cursor. Let us see how we can declare such a cursor and fetch data from the cursor.
-- Declare the With-Hold Cursor
DECLARE emp_cursor CURSOR WITH HOLD FOR SELECT * FROM employees;
-- Fetch data from the Cursor
FETCH NEXT FROM emp_cursor;
Note: We must close the WITH HOLD CURSOR because it won't automatically close.
CLOSE emp_cursor;
A quick Architecture Audit can save hours of troubleshooting later.
Try it Now!Let HexaRocket simplify it - migrate smarter, faster, and stress-free.
Contact us Today!HexaRocket performs end-to-end database migrations and replication seamlessly between Oracle, SQL Server, MySQL, MariaDB, and PostgreSQL.
Try Today!-- Transaction A
BEGIN;
-- Declare a With-Hold Cursor
DECLARE emp_cursor CURSOR WITH HOLD FOR SELECT * FROM employees;
-- Fetch data from the Cursor
FETCH NEXT FROM emp_cursor;
-- Transaction A ends
COMMIT;
Now, even after Transaction A has ended, we can still access the With-Hold Cursor from Transaction B, as long as it is from the same session as the Transaction A.
-- Transaction B
BEGIN;
-- Fetch data from the With-Hold Cursor declared in Transaction A
FETCH NEXT FROM emp_cursor;
-- Transaction B ends
COMMIT;
This demonstrates that With-Hold Cursors can be accessed within the same session, from any transaction, providing persistence across transaction boundaries.
Note: Please note that With-Hold Cursors in PostgreSQL cannot be accessed once the session is closed. It's essential to ensure that you have completed all necessary operations with the cursor before ending your session to avoid losing access to the cursor data.
That wraps up our series on PostgreSQL Cursors. We hope you found these articles on Scrollable, Non-Scrollable and With-Hold cursors helpful.
Seeking specialized PostgreSQL support or Database Migrations assistance ? Get experts advice for effective data management, query tuning, database migrations and database optimization. Click here for personalized expertise to elevate your database performance and meet your unique business needs.
Stay tuned for more interesting topics!
Subscribe to our Newsletters and Stay tuned for more interesting topics.

Jagadeesh Panuganti is a full-stack Software Engineer at HexaCluster, specializing in building and deploying scalable cloud-native applications. With expertise in React, Node.js, Go, and PostgreSQL, he focuses on creating robust solutions using microservices architecture on cloud platforms like Azure and AWS. He has a passion for designing secure, efficient systems and CI/CD pipelines.
Start your migration journey 🚀
start your migration journey with our expert team
Database & Application Migration Assessment Tool
End-to-End Database Migration & Modernization Tool
Database Code Object Conversion to PostgreSQL
MyBatis Mapper Conversion to PostgreSQL
Enterprise Data Replication & Live CDC
Oracle Compatibility Layer for PostgreSQL