Sample Query to show Cardinality estimation error in PostgreSQL

前端 未结 1 1123
既然无缘
既然无缘 2020-12-04 03:30

I am working on a project using PostgreSQL9.3. I was using the below query to show how selectivity estimation errors can lead to multi-fold increase in query execution time

相关标签:
1条回答
  • 2020-12-04 04:27

    This is to answer the comment by @Twelfth as well as the question itself.

    Three quotes from this chapter in the manual: "Controlling the Planner with Explicit JOIN Clauses"

    Explicit inner join syntax (INNER JOIN, CROSS JOIN, or unadorned JOIN) is semantically the same as listing the input relations in FROM, so it does not constrain the join order.

    ...

    To force the planner to follow the join order laid out by explicit JOINs, set the join_collapse_limit run-time parameter to 1. (Other possible values are discussed below.)

    ...

    Constraining the planner's search in this way is a useful technique both for reducing planning time and for directing the planner to a good query plan.

    Bold emphasis mine. Conversely, you can abuse the same to direct the query planner to a bad query plan for your testing purposes. Read the whole manual page. It should be instrumental.

    Also, you can force nested loops by disabling alternative methods one by one (best in your session only). Like:

    SET enable_hashjoin = off;
    

    Etc.
    About checking and setting parameters:

    • Query a parameter (postgresql.conf setting) like "max_connections"

    Force actual estimation errors

    One obvious way would be to disable autovacuum and add / remove rows from the table. Then the query planner is working with outdated statistics. Note that some other commands update statistics as well.

    Statistics are stored in the catalog tables pg_class and pg_statistics.

    SELECT * FROM pg_class WHERE oid = 'mytable'::regclass;
    SELECT * FROM pg_statistic WHERE starelid = 'mytable'::regclass;
    

    This leads me to another option. You could forge entries in these two tables. Superuser privileges required.
    You don't strike me as a newcomer, but a warning for the general public: If you break something in the catalog tables, your database (cluster) might go belly-up. You have been warned.

    0 讨论(0)
提交回复
热议问题