]> granicus.if.org Git - postgresql/commitdiff
Fix performance hazard in REFRESH MATERIALIZED VIEW CONCURRENTLY.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 19 Mar 2018 21:23:07 +0000 (17:23 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 19 Mar 2018 21:23:21 +0000 (17:23 -0400)
Jeff Janes discovered that commit 7ca25b7de made one of the queries run by
REFRESH MATERIALIZED VIEW CONCURRENTLY perform badly.  The root cause is
bad cardinality estimation for correlated quals, but a principled solution
to that problem is some way off, especially since the planner lacks any
statistics about whole-row variables.  Moreover, in non-error cases this
query produces no rows, meaning it must be run to completion; but use of
LIMIT 1 encourages the planner to pick a fast-start, slow-completion plan,
exactly not what we want.  Remove the LIMIT clause, and instead rely on
the count parameter we pass to SPI_execute() to prevent excess work if the
query does return some rows.

While we've heard no field reports of planner misbehavior with this query,
it could be that people are having performance issues that haven't reached
the level of pain needed to cause a bug report.  In any case, that LIMIT
clause can't possibly do anything helpful with any existing version of the
planner, and it demonstrably can cause bad choices in some cases, so
back-patch to 9.4 where the code was introduced.

Thomas Munro

Discussion: https://postgr.es/m/CAMkU=1z-JoGymHneGHar1cru4F1XDfHqJDzxP_CtK5cL3DOfmg@mail.gmail.com

src/backend/commands/matview.c

index ab6a889b129bf349b218689d29ac228324af614e..2ff23abbe931fe5696d5c84b663b727e85812f43 100644 (file)
@@ -657,10 +657,10 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
        appendStringInfo(&querybuf,
                                         "SELECT newdata FROM %s newdata "
                                         "WHERE newdata IS NOT NULL AND EXISTS "
-                                        "(SELECT * FROM %s newdata2 WHERE newdata2 IS NOT NULL "
+                                        "(SELECT 1 FROM %s newdata2 WHERE newdata2 IS NOT NULL "
                                         "AND newdata2 OPERATOR(pg_catalog.*=) newdata "
                                         "AND newdata2.ctid OPERATOR(pg_catalog.<>) "
-                                        "newdata.ctid) LIMIT 1",
+                                        "newdata.ctid)",
                                         tempname, tempname);
        if (SPI_execute(querybuf.data, false, 1) != SPI_OK_SELECT)
                elog(ERROR, "SPI_exec failed: %s", querybuf.data);