CLOSE

Closes an open cursor, releasing resources.

Category: cursors

Syntax

CLOSE <cursor>;

Description

## Overview CLOSE releases the resources held by an open cursor. After closing, the cursor can no longer be fetched from, but it can be reopened with OPEN if needed. ## Behavior - The executor looks up the cursor by name in the variable store. If the cursor is not found, a "Cursor not found" error is raised. - The cursor's `is_open` flag is set to false and its internal result batches (Arrow RecordBatches) are released, freeing memory. - Closing an already-closed cursor does not raise an error in the current implementation, but relying on this behavior is not recommended. - After CLOSE, the cursor can be reopened with OPEN, which re-executes the bound query and resets position to before the first row. ## Differences from PostgreSQL - In PostgreSQL, closing an already-closed cursor raises an error. DeltaForge currently tolerates this silently. Do not depend on this leniency. - PostgreSQL cursors participate in transaction lifecycle; they are automatically closed at transaction end. DeltaForge cursors persist until explicitly closed or until the enclosing block scope exits.

Parameters

NameTypeDescription
cursorSpecify the name of an open cursor to close and release its result batches.

Examples

DO $$
DECLARE
  emp_cursor CURSOR FOR SELECT id, name FROM employees;
  rec RECORD;
BEGIN
  OPEN emp_cursor;
  FETCH emp_cursor INTO rec;
  PRINT rec.name;
  CLOSE emp_cursor;
END;
$$;
DO $$
DECLARE
  cur CURSOR FOR SELECT * FROM orders WHERE status = 'pending';
  rec RECORD;
BEGIN
  OPEN cur;
  LOOP
    FETCH cur INTO rec;
    EXIT WHEN NOT FOUND;
    PRINT 'Order:', rec.id;
  END LOOP;
  CLOSE cur;
END;
$$;
DO $$
DECLARE
  c1 CURSOR FOR SELECT id FROM table_a;
  c2 CURSOR FOR SELECT id FROM table_b;
  rec RECORD;
BEGIN
  OPEN c1;
  OPEN c2;
  FETCH c1 INTO rec;
  PRINT 'A:', rec.id;
  FETCH c2 INTO rec;
  PRINT 'B:', rec.id;
  CLOSE c2;
  CLOSE c1;
END;
$$;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →