title: Loop on tables with PL/pgSQL in Postgres 9.0+
body: <p>I want to loop through all my tables to count rows in each of them. The following query gets me an error:</p>
  
  <pre><code>DO $$
  DECLARE
      tables CURSOR FOR
          SELECT tablename FROM pg_tables
          WHERE tablename NOT LIKE 'pg_%'
          ORDER BY tablename;
      tablename varchar(100);
      nbRow int;
  BEGIN
      FOR tablename IN tables LOOP
          EXECUTE 'SELECT count(*) FROM ' || tablename INTO nbRow;
          -- Do something with nbRow
      END LOOP;
  END$$;
  </code></pre>
  
  <p>Errors:</p>
  
  <blockquote>
  <pre><code>ERROR:  syntax error at or near ")"
  LINE 1: SELECT count(*) FROM (sql_features)
                                            ^
  QUERY:  SELECT count(*) FROM (sql_features)
  CONTEXT:  PL/pgSQL function inline_code_block line 8 at EXECUTE statement
  </code></pre>
  </blockquote>
  
  <p><code>sql_features</code> is a table's name in my DB. I already tried to use <code>quote_ident()</code> but to no avail.</p>