Cursors
POSTGRESQL

Cursors in PostgreSQL - A Guide

intro

A crucial aspect of managing data retrieval in PostgreSQL is understanding how cursors work. This article provides a comprehensive guide to cursors in PostgreSQL, covering the different types of cursors available, the operations that can be performed, and best practices for optimizing performance - read on!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
PostgreSQL logo POSTGRESQL
THE POSTGRESQL DATABASE

Why Use Cursors?

Cursors are a powerful tool for working with large datasets in any database management system. Cursors allow you to fetch and process data by a query one at a time, making it useful for functions like filtering and sorting data. They are a powerful feature that allows database developers to manage data retrieval more efficiently by preparing a query so that data will be created when a FETCH is called.

However, working with cursors can be challenging - we need to consider which type of cursor to use, how to perform operations on the cursor, and how to employ cursors to optimize performance.

Types of Cursors in PostgreSQL

There are two types of cursors in Postgres: the implicit and the explicit/declare cursor types. The implicit cursor type is created automatically by a database when a user uses the FOR clause in a SELECT statement. On the other hand, the explicit/declare type is created openly by a user using the DECLARE statement. This type of cursor allows users to specify the name of the cursor, the type of cursor, and the query that will be used to fetch rows from a database.

The Process Flow of Cursors in PostgreSQL

Let’s look at the four-phased process flow of cursors in Postgres. The following code illustrates how cursors work in Postgres. First, you declare a cursor, next, you open the cursor, then you fetch rows from a result set into a target, and finally, you close the cursor.

Image 1 - The Process Flow of Cursors in Postgres
Image 1 - The Process Flow of Cursors in Postgres

In the subsequent sections, we’re going to look at each of the phases in detail.

Declaring a Cursor

The first thing to do to be able to access a cursor in Postgres is to create or declare one.

Generally, cursor declarations have a few variations and can be declared in several ways depending on your specific need. Let us look at the variations:

  • Basic Cursor
Copy
        
1 DECLARE [cursor_name] CURSOR FOR [query]

DECLARE: a function to declare a cursor.

[cursor_name]: we specify the name of the cursor here.

[query]: this is where a query is given to the cursor.

  • Scrollable Cursor
Copy
        
1 DECLARE cursor_name CURSOR SCROLL FOR SELECT column1, column2 FROM my_table;

The SCROLL keyword is used in this example to declare a scrollable cursor. With the aid of scrollable cursors, you can access non-sequential rows and move either backward or forwards across the result set.

  • Parameterized Cursor
Copy
        
1 DECLARE cursor_name CURSOR (prm1 INTEGER, prm2 TEXT) FOR 2 SELECT column1, column2 FROM my_table WHERE column1 = prm1 AND column2 = prm2; 3 4 DECLARE cursor_name CURSOR (prm1 INTEGER, prm2 TEXT) FOR 5 SELECT column1, column2 FROM table_name WHERE column1 = prm1 AND column2 = prm2;

This example declares a cursor called cursor_name with the parameters prm1 and prm2. The cursor chooses rows from table_name's columns 1 and 2, filtering them depending on the provided parameters.

Now that we know the variations in cursor declarations, let us touch on how how to open cursors in PostgreSQL.

Opening a Cursor

A cursor is simply opened with the code below:

Copy
        
1 OPEN my_cursor;

For example, this is how we’re going to open a basic cursor in DbVisualizer:

Image 2 - Opening Cursor in the SQL Commander of DbVisualizer
Image 2 - Opening Cursor in the SQL Commander of DbVisualizer

The next line of action after opening a cursor is to fetch rows from the cursor.  Let’s look at how this is done.

Fetching Rows from a Cursor

After opening a cursor, we can get data using the FETCH statement. The FETCH function gets the next row(s) from the cursor.  It then returns NULL if no row is found. The syntax for fetching data is below

Copy
        
1 FETCH [direction] [count] FROM cursor_name;

Here, the direction represents the direction of fetching, and the count indicates the number of rows to be fetched. It is important to note that the direction of the fetching can be in several ways. For example, if a user wants to fetch rows in a specific order, a direction like NEXT can be used to fetch the next row in the result set, or PRIOR to fetch the previous row.

Let us look at some other directions in the fetching of rows:

Copy
        
1 FIRST 2 LAST 3 ABSOLUTE count 4 RELATIVE count 5 count 6 ALL 7 FORWARD 8 FORWARD count 9 FORWARD ALL 10 BACKWARD 11 BACKWARD count 12 BACKWARD ALL

These are other available direction options in the fetching of data from a cursor and the choice of a specific direction depends on the user’s need. Next in the process flow is the closing of cursors. Let’s jump straight into it.

Closing a Cursor

One good practice in cursor management is to close cursors after using them. Doing so is essential for many reasons. One of these reasons is that when a cursor is opened, a transaction is opened within the database, and when this transaction is not closed after use, changes made during the transaction may not be committed to the database leading to inconsistencies in data.

Copy
        
1 CLOSE cursor_name;

In the code above, the CLOSE statement instructs Postgres to close the specified cursor. After closing the cursor, a user will no longer be able to fetch data from it. In the following section, we’re going to look at a practical example of how cursors work in Postgres in DbVisualizer.

Let us create a sample table and execute it using the query below in DbVisualizer:

Image 3 - Building a Query to Create a Sample Table
Image 3 - Building a Query to Create a Sample Table

Next, let’s build a query that will insert data into the created table:

Image 4 - Building a Query to Insert Data into a Table
Image 4 - Building a Query to Insert Data into a Table

Now that our table is ready we can take on the first step, which is the declaration of the cursor:

Image 5 - Declaring a Cursor
Image 5 - Declaring a Cursor

Next, let us fetch the data with the query below in DbVisualizer:

Image 6 - Fetching Data from a Cursor
Image 6 - Fetching Data from a Cursor

By fetching data from the cursor, we’re going to end up with a list of the first 10 students from the students' table as shown below:

Image 7 - Fetched Data from Table
Image 7 - Fetched Data from Table

Great! Let us try to explore another direction of the data fetching from the cursor. In the query below, we’re going to employ the PRIOR direction.

Image 8 - Fetching Data with a PRIOR Direction
Image 8 - Fetching Data with a PRIOR Direction

Earlier, we said that the PRIOR direction fetches data from the previous row and from the output above, it can be seen that the query fetched data from row 9. This is a confirmation of the theory behind the PRIOR direction as our cursor was initially at the row 10.

Great! The last thing onboard is to close the cursor, which is an essential step in cursor management. It is always advisable to close cursors after use to prevent activities like data inconsistency in our database.

Outro

Working with cursors in PostgreSQL can be challenging, but it's not rocket science either. To take full advantage of this powerful feature, you need to understand the different types of cursors available, how to perform operations on them, and best practices for optimizing performance. With this knowledge, you can improve your database development skills and efficiently manage large datasets.

Make use of DbVisualizer, the world’s leading database client, and have a wide range of features at your disposal. Follow us on dev.to and we will be back with more database development tips soon!

FAQs

What are cursors in PostgreSQL?

Cursors in PostgreSQL are pointers or references to a result set of a query, which allow you to fetch and process data one row at a time.

What are the different types of cursors available in PostgreSQL?

There are two types of cursors in PostgreSQL: implicit and explicit cursors.

How do I use cursors in PostgreSQL?

To use cursors in PostgreSQL, you need to declare a cursor, open it, fetch rows from the result set, process the data, and finally close the cursor.

Should I consider using additional tools while working with cursors?

We suggest you employ DbVisualizer whiles working with cursors. DbVisualizer is the world’s most highly rated database editor and SQL client that provides developers and DBAs with powerful features geared toward making their work easier.

Dbvis download link img
About the author
Leslie S. Gyamfi.
Leslie S. Gyamfi
Leslie Gyamfi is a mobile/web app developer with a passion for creating innovative solutions. He is dedicated to delivering high-quality products and technical articles. You can connect with him on LinkedIn
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

A Guide To the SQL DECLARE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SERVER 7 min 2024-12-12
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

How to Find Elements In an Array: PostgreSQL Guide

author Antonello Zanini tags POSTGRESQL 3 min 2024-12-05
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

Postgres Create Array of Numbers: Complete Guide

author Lukas Vileikis tags POSTGRESQL SQL 7 min 2024-11-21
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18
title

Postgres DATEDIFF Equivalent Functions: Calculating Date and Time Differences

author Lukas Vileikis tags POSTGRESQL 6 min 2024-11-04
title

CREATE SCHEMA PostgreSQL Statement: What, Why & When to Use

author Lukas Vileikis tags POSTGRESQL 4 min 2024-10-30
title

SQL EXPLAIN: The Definitive Tool to Optimize Queries

author Antonello Zanini tags MySQL POSTGRESQL SQL 12 min 2024-10-29

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗