title: Why is iterating through a large Django QuerySet consuming massive amounts of memory?
body: <p>The table in question contains roughly ten million rows.</p>
  
  <pre><code>for event in Event.objects.all():
      print event
  </code></pre>
  
  <p>This causes memory usage to increase steadily to 4 GB or so, at which point the rows print rapidly. The lengthy delay before the first row printed surprised me – I expected it to print almost instantly.</p>
  
  <p>I also tried <code>Event.objects.iterator()</code> which behaved the same way.</p>
  
  <p>I don't understand what Django is loading into memory or why it is doing this. I expected Django to iterate through the results at the database level, which'd mean the results would be printed at roughly a constant rate (rather than all at once after a lengthy wait).</p>
  
  <p>What have I misunderstood?</p>
  
  <p>(I don't know whether it's relevant, but I'm using PostgreSQL.)</p>