0
0
PostgreSQLquery~10 mins

Performing operations on cursors in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Performing operations on cursors
Declare Cursor
Open Cursor
Fetch Rows
Process Rows
Repeat Fetch until no rows
Close Cursor
End
This flow shows how a cursor is declared, opened, rows are fetched and processed repeatedly until no more rows remain, then the cursor is closed.
Execution Sample
PostgreSQL
DECLARE cur CURSOR FOR SELECT id, name FROM users;
OPEN cur;
FETCH NEXT FROM cur INTO v_id, v_name;
-- process data
CLOSE cur;
This code declares a cursor for a query, opens it, fetches one row into variables, processes it, and then closes the cursor.
Execution Table
StepActionCursor StateVariables (v_id, v_name)Output/Result
1DECLARE cursor for SELECT id, name FROM usersDeclared but closedNULL, NULLCursor ready for use
2OPEN cursorOpen, positioned before first rowNULL, NULLCursor ready to fetch
3FETCH NEXT INTO v_id, v_nameOpen, positioned on first row1, 'Alice'Fetched first row
4Process rowOpen, positioned on first row1, 'Alice'Processed row with id=1
5FETCH NEXT INTO v_id, v_nameOpen, positioned on second row2, 'Bob'Fetched second row
6Process rowOpen, positioned on second row2, 'Bob'Processed row with id=2
7FETCH NEXT INTO v_id, v_nameOpen, positioned on third row3, 'Carol'Fetched third row
8Process rowOpen, positioned on third row3, 'Carol'Processed row with id=3
9FETCH NEXT INTO v_id, v_nameOpen, positioned after last rowNULL, NULLNo more rows, fetch returns no data
10CLOSE cursorClosedNULL, NULLCursor closed, resources freed
💡 Fetch returns no data at step 9, so loop ends and cursor is closed at step 10.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9Final
v_idNULL123NULLNULL
v_nameNULL'Alice''Bob''Carol'NULLNULL
Key Moments - 3 Insights
Why do v_id and v_name become NULL after the last fetch?
At step 9 in the execution_table, the FETCH returns no data because the cursor reached the end of the result set, so variables are set to NULL indicating no more rows.
What happens if we try to FETCH before OPENing the cursor?
You cannot fetch before opening the cursor; the cursor must be OPEN to position it on the result set. The execution_table starts with OPEN at step 2 before FETCH at step 3.
Why do we need to CLOSE the cursor?
Closing the cursor at step 10 releases database resources. Leaving cursors open can cause resource leaks and lock issues.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what are the values of v_id and v_name after step 5?
Av_id=3, v_name='Carol'
Bv_id=1, v_name='Alice'
Cv_id=2, v_name='Bob'
Dv_id=NULL, v_name=NULL
💡 Hint
Check the 'Variables' column at step 5 in the execution_table.
At which step does the FETCH return no more rows?
AStep 7
BStep 9
CStep 10
DStep 3
💡 Hint
Look for the step where variables become NULL and output says 'No more rows'.
If we forget to CLOSE the cursor, what is the likely effect?
ADatabase resources remain allocated, causing potential issues
BCursor fetches rows twice
CCursor automatically closes after last fetch
DVariables reset to NULL automatically
💡 Hint
Refer to the key_moments explanation about closing cursors.
Concept Snapshot
DECLARE cursor FOR query;
OPEN cursor;
FETCH NEXT FROM cursor INTO variables;
Repeat FETCH and process rows until no more rows;
CLOSE cursor to free resources.
Cursors allow row-by-row processing of query results.
Full Transcript
This visual execution shows how to perform operations on cursors in PostgreSQL. First, a cursor is declared for a SELECT query. Then it is opened to prepare for fetching rows. Each FETCH moves the cursor to the next row and loads data into variables. After processing each row, FETCH repeats until no rows remain, indicated by variables becoming NULL. Finally, the cursor is closed to release resources. This step-by-step trace helps beginners understand cursor lifecycle and variable changes during execution.