The Iterator of the Cursed Cursor
Sequentially traversing massive datasets without exhausting memory reserves.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Necromancy // Sequential Draining
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SQL PATTERN //
-- Utilizing a Cursor within a PL/pgSQL block
DO $$
DECLARE
soul_record RECORD;
soul_cursor CURSOR FOR
SELECT soul_id, corruption_level FROM damned_souls;
BEGIN
OPEN soul_cursor;
LOOP
-- Fetching one row at a time (The Iterator step)
FETCH soul_cursor INTO soul_record;
EXIT WHEN NOT FOUND;
-- Perform the purification ritual
IF soul_record.corruption_level > 90 THEN
RAISE NOTICE 'Purging soul %', soul_record.soul_id;
END IF;
END LOOP;
CLOSE soul_cursor;
END;
$$;
The Iterator of the Cursed Cursor
SQL is inherently set-based, designed to manipulate millions of rows at once. But occasionally, a ritual requires precise, sequential, row-by-row inspection. The Iterator pattern manifests as the Cursor.
A cursor allows us to pointer-traverse a result set sequentially. While generally slower than set-based transmutation, it is vital when dealing with complex, imperative logic per row, preventing the database from loading the entire spectral horde into memory simultaneously.
Runic Tags:
#cursor
#iterator
#behavioral