I am wondering if there is something related to variables and nested loops in postgresql that works differently than in other languages.
Example:
CREATE OR REPLACE FUNCTION public.generate_syllables()
RETURNS integer AS
$BODY$
DECLARE
w RECORD;
s RECORD;
current_syllable integer := 1;
vowel_trigger integer := 0;
syllable_count integer := 1;
BEGIN
FOR w IN SELECT id FROM words LOOP
FOR s IN SELECT sound, id FROM sounds WHERE id = w.id ORDER BY ordering LOOP
IF (SELECT sr.vowel FROM sound_reference sr WHERE sr.sound = s.sound) = 1 AND vowel_trigger = 1 THEN
syllable_count := syllable_count + 1;
UPDATE sounds SET syllable = syllable_count WHERE id = s.id;
vowel_trigger := 0;
ELSIF (SELECT sr.vowel FROM sound_reference sr WHERE sr.sound = s.sound) = 1 THEN
vowel_trigger := 1;
UPDATE sounds SET syllable = syllable_count WHERE id = s.id;
ELSE
UPDATE sounds SET syllable = syllable_count WHERE id = s.id;
END IF;
END LOOP;
UPDATE words SET syllables = syllable_count WHERE id = w.id;
syllable_count := 1;
vowel_trigger := 0;
END LOOP;
RETURN 1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
When I run this function as is, the function never enters the first condition in the if statement. I tested this by adding a return statement within that first condition. At first I thought this must be a logic error, but I have gone through it by hand with examples that are generated from my dataset, and it should work as desired. What is even stranger, is when I comment out the line in the outer loop, for vowel_trigger := 0, then it DOES enter the first if statement. Of course then, the logic does not work correctly either, and from that I have gathered that the syllable_count is being set back to 0 BEFORE the nested loop finishes looping, which would also explain why the first condition is never entered, because vowel_trigger is set back to 0 before the loop makes it back up to the first condition.
In other words, it seems to me that my nested loop is not acting like a nested loop, but rather the nested loop extends into the outer loop, before the nested loop restarts. I imagine I must just not understand how to properly create a nested loop, or perhaps they just can't work this way in POSTGRESQL... any advice would be greatly appreciated.