Constant folding

However, there are many more optimizations in PostgreSQL which happen behind the scenes and which contribute to overall good performance. One of those features is called constant folding. The idea is to turn expressions into constants, as shown in the following example:

test=# explain SELECT * FROM a WHERE aid = 3 + 1;                          QUERY PLAN ----------------------------------------------------------------  Index Only Scan using idx_a on a          (cost=0.57..4.58 rows=1 width=4)    Index Cond: (aid = 4) (2 rows)

As you can see, PostgreSQL will try to look for 4. As aid is indexed, PostgreSQL will go for an index scan. Note that our table has just one column, so PostgreSQL even figured that all the data it needs can be found in the index.

What ...

Get Mastering PostgreSQL 10 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.