title: PostgreSQL: return select count(*) from table;
body: <p>please help me with 1 more <a href="http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html" rel="nofollow">PL/pgSQL</a> question.</p>
  
  <p>I have a PHP-script run as daily cronjob and deleting old records from 1 main table and few further tables referencing its "id" column:</p>
  
  <pre><code>create or replace function quincytrack_clean()
          returns integer as $BODY$
          begin
                  create temp table old_ids 
                  (id varchar(20)) on commit drop;
  
                  insert into old_ids
                  select id from quincytrack
                  where age(QDATETIME) &gt; interval '30 days';
  
                  delete from hide_id where id in
                  (select id from old_ids);
  
                  delete from related_mks where id in
                  (select id from old_ids);
  
                  delete from related_cl where id in
                  (select id from old_ids);
  
                  delete from related_comment where id in
                  (select id from old_ids);
  
                  delete from quincytrack where id in
                  (select id from old_ids);
  
                  return select count(*) from old_ids;
          end;
  $BODY$ language plpgsql;
  </code></pre>
  
  <p>And here is how I call it from the PHP script:</p>
  
  <pre><code>$sth = $pg-&gt;prepare('select quincytrack_clean()');
  $sth-&gt;execute();
  if ($row = $sth-&gt;fetch(PDO::FETCH_ASSOC))
          printf("removed %u old rows\n", $row['count']);
  </code></pre>
  
  <p>Why do I get the following error?</p>
  
  <pre><code>SQLSTATE[42601]: Syntax error: 7 
  ERROR:  syntax error at or near "select" at character 9
  QUERY:  SELECT  select count(*) from old_ids
  CONTEXT:  SQL statement in PL/PgSQL function
  "quincytrack_clean" near line 23
  </code></pre>
  
  <p>Thank you! Alex</p>