title: Best practice to iterate over result set in Postgres/PHP/PDO?
body: <p>I'm using PHP 5.3.6 with PDO to access Postgres 9.0.4. I've been asked to reduce the memory footprint of a report. The current implementation is simple: execute the query, do a fetchAll() and then iterate with foreach() through the resulting array. This obviously doesn't scale with huge result sets: it can temporarily consume 100MB or more.</p>
  
  <p>I have a new implementation which takes the PDO statement handle and then iterates directly on it using foreach(), i.e. no intermediate array via fetchAll(). (From what I've read, iterating a statement handle with foreach calls fetch() under the covers.) This is just as fast and consumes <strong>way</strong> less memory: about 28kB. Still, I'm not confident I'm doing it right because, although I've done a ton of Googling, it's tough to find answers to basic questions about this:</p>
  
  <ul>
  <li><p>I've seen articles that suggest solving my original problem using cursors. Does the Postgress PDO driver already use cursors internally? If writing my own SQL to create a cursor is required, I'm willing to but I'd prefer to write the simplest code possible (but no simpler!).</p></li>
  <li><p>If foreach calls fetch() each iteration, isn't that too network chatty? Or is it smart and fetches many rows at once, e.g. 500, to save bandwidth? (This may imply that it uses cursors internally.)</p></li>
  <li><p>I've seen an article that wraps the statement handle in a class that implements Iterator interface. Isn't this redundant given that a PDO statement handle already does this? Or am I missing something?</p></li>
  <li><p>My call to prepare the SQL statement looks like this:</p>
  
  <p>$sth = $dbh->prepare($sql);</p></li>
  </ul>
  
  <p>I found that it made no memory or speed difference if I did this:</p>
  
  <pre><code>$sth = $dbh-&gt;prepare($sql, array( PDO::ATTR_CURSOR =&gt; PDO::CURSOR_FWDONLY ) );
  </code></pre>
  
  <p>Is this because this is the default anyway for the Postgres PDO driver? This would make sense if it is already using cursors internally.</p>
  
  <p>General comments about the approach and other ways to solve this problem are welcome.</p>