HexaCluster LogoHexaCluster Logo
  • Services
  • Products
  • HexaRocket
  • Blog
  • Resources
  • Company
  • Contact Us
Schedule a Demo
Stay Updated

Subscribe to Newsletters

Be the first to know! Stay updated with the latest insights, database migration benchmarks, and technical updates from HexaCluster.

HexaCluster LogoHexaCluster Logo

Enterprise-grade Database migration, modernization, and tooling for teams moving off legacy databases.

  • One Dundas Street West, Suite 2500, Toronto, Ontario, M5G 1Z3, Canada
  • HexaCluster DMCC, Plot No: JLT-PH2-RET-R6 Jumeirah Lakes Towers, Dubai, UAE
connect@hexacluster.ai+1 (902) 221-5976

Security & Compliance

SOC 2 Type 1 reportSOC 2 Type 2 report, monitored by Comp AIGDPR compliantISO 27001AICPA SOC for Service Organizations

Products

  • DMAT
  • HexaRocket
  • HexaBridge
  • HexaTranspile
  • MyBatis2Pg
  • HexaReplicate
  • Download Products

HexaRocket

  • Supported Database Migrations
  • Migrate to Yugabyte
  • About HexaRocket
  • Migrate to Oracle
  • Migrate to PostgreSQL
  • Migrate to MariaDB

Services

  • Database Migration to PostgreSQL
  • Application Migration and Modernization
  • AI/ML and MLOps
  • Architectural Health Audit
  • Managed DBA Services
  • Performance Tuning
  • PostgreSQL Development
  • Training for DBAs & Developers
  • 24/7 Support
  • Supported Tools and Extensions

Company

  • Blog
  • Case Studies
  • Webinars
  • Announcements
  • About Us
  • Referral Program
  • Events
  • Contact Us

© HexaCluster 2026. All rights reserved. Privacy PolicyThis site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

HEXACLUSTERHEXACLUSTERHEXACLUSTER

Data Navigation with PostgreSQL Cursors – Part III

Jagadeesh Panuganti
Mar 11, 2024
postgresqlCursors#PostgreSQL#Cursors#Postgres Cursors

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.


  • Scrollable Cursors
  • Non-Scrollable Cursors
  • With-Hold Cursors

What are With-Hold Cursors ?

With-Hold Cursors in PostgreSQL are cursor types that enable users to retain the query result set even after the transaction in which they were declared has been committed. Unlike other cursor types, which are automatically closed at the end of a transaction, With-Hold Cursors persist beyond transaction boundaries. This persistence allows for further processing or retrieval until explicitly closed or the session terminates. Consequently, users can maintain the cursor's data for extended periods, facilitating iterative or interactive data manipulation tasks across transactions.

image


How to Use a With-Hold Cursor ?

Using a With-Hold Cursor is similar to using other types of cursors in PostgreSQL. To illustrate the practical use of With-Hold Cursors, let's consider an example
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.

Using a With-Hold Cursor

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;


🧠 Fine-tune Your PostgreSQL

A quick Architecture Audit can save hours of troubleshooting later.

Try it Now!

🚀 Planning a Database Migration?

Let HexaRocket simplify it - migrate smarter, faster, and stress-free.

Contact us Today!

🚀 Try HexaRocket

HexaRocket performs end-to-end database migrations and replication seamlessly between Oracle, SQL Server, MySQL, MariaDB, and PostgreSQL.

Try Today!

With-Hold Cursors Across Transactions

Now, let's explore how With-Hold Cursors maintain their accessibility across different transactions within the same session.
Let us consider a case where we have a Transaction A. We declare a With-Hold Cursor to fetch data from the `employees` table inside this transaction.

-- 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.

Conclusion

In conclusion, With-Hold Cursors offer significant advantages for data management in PostgreSQL. With-Hold Cursors are useful in PostgreSQL for keeping your data accessible throughout all transactions of a session. By understanding how to use them, you can make your work more efficient and flexible.

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.



Authors

Jagadeesh Panuganti

Jagadeesh Panuganti

Senior Developer

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

Products

DMAT

Database & Application Migration Assessment Tool

HexaRocket

End-to-End Database Migration & Modernization Tool

HexaTranspile

Database Code Object Conversion to PostgreSQL

MyBatis2Pg

MyBatis Mapper Conversion to PostgreSQL

HexaReplicate

Enterprise Data Replication & Live CDC

HexaBridge

Oracle Compatibility Layer for PostgreSQL

HexaRocket 🚀

Oracle to PostgreSQLSQL Server to PostgreSQLMySQL to PostgreSQLMariaDB to PostgreSQLAny to Any databases

Migration Services

Database MigrationsApplication Modernization

PostgreSQL Consulting

Architectural AuditsPerformance TuningTraining & Support