Limit

Like everything else, query limits are built on top of existing scans that return a set of rows:

EXPLAIN ANALYZE SELECT customerid FROM customers LIMIT 10;
QUERY PLAN                                                   
----------
Limit  (cost=0.00..0.34 rows=10 width=4) (actual time=0.016..0.063 rows=10 loops=1)
       ->  Seq Scan on customers  (cost=0.00..676.00 rows=20000 width=4) (actual time=0.013..0.030 rows=10 loops=1)
Total runtime: 0.117 ms  

Note the actual rows output by the Seq Scan here. This shows one of the aspects of query execution that isn't necessarily obvious. The way queries are carried out, the top node in the query plan is started, and it asks its children nodes for things on demand. It's a top-down execution model; nodes only produce output when said output is required. ...

Get PostgreSQL 10 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.