Common SQL Errors

PostgreSQL Error - 2001 no_additional_dynamic_result_sets_returned Error Explained and Fixed

August 4, 2025

The error signals that a CALL statement or cursor tried to fetch more dynamic result sets than the routine declared.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is the no_additional_dynamic_result_sets_returned error?

no_additional_dynamic_result_sets_returned appears when a PostgreSQL stored procedure finishes execution without returning the extra result sets the caller expected. Align the caller’s fetch logic with the procedure’s OUT parameters or RETURN NEXT statements to resolve the mismatch.

Error Highlights

Typical Error Message

no_additional_dynamic_result_sets_returned

Error Type

Cursor Execution Error

Language

PostgreSQL

Symbol

no_additional_dynamic_result_sets_returned

Error Code

2001

SQL State

Explanation

Table of Contents

What is no_additional_dynamic_result_sets_returned?

The condition name no_additional_dynamic_result_sets_returned maps to SQLSTATE 2001 in PostgreSQL. It fires when a caller tries to fetch an extra dynamic result set that the invoked stored procedure did not supply.

The server signals this edge-case to prevent undefined behavior, cursor corruption, or runaway network reads.

Fixing it ensures stable cursor iteration and predictable client logic.

What Causes This Error?

The error usually arises when a client library or application expects more result sets than the procedure’s RETURNS TABLE, OUT parameters, or RETURN NEXT statements actually deliver.

Mismatched FETCH statements, incorrectly set refcursor counts, and driver methods that assume multiple result batches also trigger the condition.

How to Fix no_additional_dynamic_result_sets_returned

First, confirm the exact number of dynamic result sets the procedure is designed to emit.

Inspect the routine definition with \df+ or pgAdmin.

Next, adjust application code so it stops calling FETCH or nextset() after the last legitimate set. When refcursors are used, close the cursor once done.

Common Scenarios and Solutions

When using psql, calling FETCH ALL on a refcursor after the final SET returns this error. Stop fetching once FOUND is false.

In psycopg3, calling cursor.nextset() after it already returned None raises the server condition.

Wrap the call in a conditional that checks for None.

Best Practices to Avoid This Error

Document every procedure with the exact count of expected result sets. Use RETURNS SETOF RECORD only when multiple sets are guaranteed.

Implement defensive coding on the client side: break out of fetch loops when no more sets are reported instead of assuming a fixed count.

Related Errors and Solutions

too_many_result_sets_returned indicates the opposite mistake - the routine returned more sets than declared.

Adjust the RETURNS clause.

invalid_cursor_state appears when you FETCH from a cursor that is already closed. Ensure proper cursor lifecycle management.

.

Common Causes

Related Errors

FAQs

Does this error affect single-result queries?

No. It only appears when stored procedures or functions can return multiple dynamic result sets.

Can I ignore the condition in my code?

Suppressing it hides a logic flaw and risks data loss. Always align fetch loops with real output.

Which PostgreSQL versions raise SQLSTATE 2001?

Versions 10+ follow SQL/MED condition naming, including 2001 for this error.

How does Galaxy help?

Galaxy’s SQL editor highlights routine definitions and warns when your FETCH loop exceeds declared result sets, preventing the error early.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo