title: Postgress function not working as expected with LOOP and INTO
body: <p>I have following procedure</p>
  <pre><code>CREATE OR REPLACE FUNCTION createtestdata(
      )
      RETURNS boolean
      LANGUAGE 'plpgsql'
  
      COST 100
      VOLATILE 
  AS $BODY$
      DECLARE
          proj RECORD;
          ruleid integer;
          emailtemplateid integer;
          otherTemplates RECORD;
          markdoenTemplateId integer;
          pdfTemplateId integer;
          jsonTemplateId integer;
      BEGIN
      
  
          FOR proj IN
              SELECT p.id AS project_id FROM project p
              WHERE p.id != 10409 and p.projecttype='software'
              ORDER BY p.id
          LOOP
              select  &quot;ID&quot; from &quot;TEST_TEMPLATES&quot; 
              where &quot;PROJECT_ID&quot; = proj.project_id AND &quot;TYPE&quot; =1 ORDER BY RANDOM() LIMIT 1
              INTO emailtemplateid;
              RAISE NOTICE 'template id is %...', emailtemplateid;
  
              select  &quot;ID&quot;, &quot;TYPE&quot; from &quot;TEST_TEMPLATES&quot; 
              where &quot;PROJECT_ID&quot; = proj.project_id AND &quot;TYPE&quot; IN (2,4,5)
              ORDER BY RANDOM() LIMIT 1
              INTO otherTemplates;
  
              RAISE NOTICE 'OTHER TEMPLATES %',   otherTemplates; 
              RAISE NOTICE 'inserting records for project id %...', proj.project_id;
  
              pdfTemplateId = NULL;
              markdoenTemplateId = NULL;
              jsonTemplateId = NULL;
  
              IF otherTemplates.&quot;TYPE&quot; = 2 THEN
                  pdfTemplateId = otherTemplates.&quot;ID&quot;;
              ELSIF otherTemplates.&quot;TYPE&quot; = 4 THEN
                  markdoenTemplateId = otherTemplates.&quot;ID&quot;;
              ELSIF otherTemplates.&quot;TYPE&quot; = 5 THEN
                  jsonTemplateId = otherTemplates.&quot;ID&quot;;
              END IF;
              RAISE NOTICE 'PDF TEMP IS %... MKDN TEMP IS %... JSON TEMP IS %', 
              pdfTemplateId, markdoenTemplateId, jsonTemplateId;  
  
              INSERT INTO &quot;TEST_RULES&quot;(
                  &quot;BEFORE_NOF_DAYS&quot;, &quot;CREATED&quot;, &quot;CREATED_BY&quot;, &quot;CRON_EXPRESSION&quot;, &quot;IS_ACTIVE&quot;, 
                  &quot;IS_DELETED&quot;, &quot;MODIFIED&quot;, &quot;MODIFIED_BY&quot;, &quot;NAME&quot;, 
                  &quot;PROJECT_ID&quot;, &quot;SCHEDULE_TIME&quot;, &quot;TRIGGER_TYPE&quot;, &quot;VERSION_PATTERN&quot;)
              VALUES (null, now(), 'admin', null, true, 
                      false, now(), 'admin', 'rule for release project ' || proj.project_id, 
                      proj.project_id, null, 2, null) 
              RETURNING &quot;ID&quot; INTO ruleId;
  
              RAISE NOTICE 'Inserting email action for Rule id just created is %...', ruleId;
  
  
              
              INSERT INTO &quot;TEST_RULE_ACTIONS&quot;(
                  &quot;ACTION_TYPE&quot;, &quot;CONFLUENCE_INSTANCE_ID&quot;, &quot;CREATED&quot;, &quot;CREATED_BY&quot;, &quot;EMAIL_FROM&quot;, 
                  &quot;EXTERNAL_USERS&quot;, &quot;IS_DELETED&quot;, &quot;JIRA_GROUPS&quot;, &quot;JIRA_USERS&quot;, &quot;JSON_TEMPLATE_ID&quot;, &quot;MARKDOWN_TEMPLATE_ID&quot;,
                  &quot;MODIFIED&quot;, &quot;MODIFIED_BY&quot;, &quot;NAME&quot;, 
                  &quot;PAGE_LABEL&quot;, &quot;PARENT_ID&quot;, &quot;PDF_TEMPLATE_ID&quot;, &quot;PORTAL_ID&quot;, &quot;POST_END_POINT&quot;, &quot;POST_PASSWORD&quot;, 
                  &quot;POST_TEMPLATE_TYPE&quot;, &quot;POST_TYPE&quot;, &quot;POST_USER_NAME&quot;, &quot;REPLY_TO&quot;, &quot;RULE_ID&quot;, 
                  &quot;SEND_EMAIL_AS_BCC&quot;, &quot;SEQUENCE_NUMBER&quot;, &quot;SPACE_KEY&quot;, &quot;TEMPLATE_ID&quot;, &quot;TEMPLATE_TYPE_ID&quot;)
              VALUES (1, NULL, NOW(), 'admin', 'stest@test.com', 
                  '', false, 'arn-users', 'admin', jsonTemplateId, markdoenTemplateId, 
                  now(), 'admin', 'email action configured for project ' || proj.project_id, 
                  NULL, 0, pdfTemplateId, 0, NULL, NULL, 
                  NULL, 'page', NULL, 'stest@test.com', ruleId, 
                      false, 1, NULL, emailtemplateid, 1);
  
          END LOOP;
  
          RETURN true;
      END;
  $BODY$;
  </code></pre>
  <p>When I run the function, I get output</p>
  <pre><code>psql:dproc.sql:122: NOTICE:  template id is 3425...
  psql:dproc.sql:122: NOTICE:  project id 10494...
  psql:dproc.sql:122: NOTICE:  OTHER TEMPLATES (1709,5)
  psql:dproc.sql:122: NOTICE:  PDF TEMP IS &lt;NULL&gt;... MKDN TEMP IS &lt;NULL&gt;... JSON TEMP IS 1709
  psql:dproc.sql:122: NOTICE:  inserting records for project id 10497...
  **psql:dproc.sql:122: NOTICE:  Inserting email action for Rule id just created is &lt;NULL&gt;...**
  </code></pre>
  <p>The last line &quot;Inserting email action for Rule id just created is &quot; is wrong, the insert statement works correctly on my local SQL. but this does not work on AWS postgres</p>
  <p>Also if I put the insert statement at first statement in loop, it works. but then other two statements do not work</p>