title: "ERROR: too few arguments for format()" in a PL/pgSQL function
body: <p>I created a function to extract information from several tables using different time period:</p>
  <pre><code>CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF tmpindicadores AS
  $BODY$
  DECLARE
  r tmpindicadores%rowtype;
  record tmpindicadores.relname%type;
  tmpdata_inicio TIMESTAMP;
  tmpdata_fim TIMESTAMP;
  dia_inicio INTEGER := 01;
  dia_fim INTEGER := 01;
  mes INTEGER;
  ano INTEGER;
  
  BEGIN 
  SELECT EXTRACT (MONTH FROM CURRENT_DATE) INTO mes;enter code here
  SELECT EXTRACT (YEAR FROM CURRENT_DATE) INTO ano;
  tmpdata_inicio := ano || '-' ||mes ||'-' ||dia_inicio;
  tmpdata_fim := ano || '-' ||mes + 1 ||'-' ||dia_fim;
  
  FOR r IN SELECT relname FROM tmpindicadores
  LOOP
  record = r.relname;
  EXECUTE format($$INSERT INTO indicadores1 (SELECT * FROM %I WHERE time_date &gt;= %tmpdata_inicio 
  AND time_date &lt;= %tmpdata_fim)$$,record);
  RETURN NEXT r;
  END LOOP;
  COPY indicadores1 TO '/var/app_data/indicadores/arquivos/indicadores1.csv' DELIMITER ';' CSV 
  HEADER;
  RETURN;
  END
  $BODY$
  LANGUAGE plpgsql;
  
  SELECT * FROM getAllFoo();
  </code></pre>
  <p>But I get:</p>
  <blockquote>
  <pre><code>ERROR: too few arguments for format.
  </code></pre>
  </blockquote>
  <p>This error occurs in the <code>EXECUTE</code> line. When I replace the variables <code>tmpdata_inicio</code> and <code>tmpdata_fim</code> with '2022-04-01 00:00:00' and '2022-04-05 00:00:00' it works perfectly.</p>
  <p>What am I doing wrong?</p>