Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b6ba94e

Browse filesBrowse files
committed
Fix performance hazard in REFRESH MATERIALIZED VIEW CONCURRENTLY.
Jeff Janes discovered that commit 7ca25b7 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
1 parent b1e48cc commit b6ba94e
Copy full SHA for b6ba94e

File tree

1 file changed

+2
-2
lines changed
Filter options

1 file changed

+2
-2
lines changed

‎src/backend/commands/matview.c

Copy file name to clipboardExpand all lines: src/backend/commands/matview.c
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,10 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
577577
appendStringInfo(&querybuf,
578578
"SELECT newdata FROM %s newdata "
579579
"WHERE newdata IS NOT NULL AND EXISTS "
580-
"(SELECT * FROM %s newdata2 WHERE newdata2 IS NOT NULL "
580+
"(SELECT 1 FROM %s newdata2 WHERE newdata2 IS NOT NULL "
581581
"AND newdata2 OPERATOR(pg_catalog.*=) newdata "
582582
"AND newdata2.ctid OPERATOR(pg_catalog.<>) "
583-
"newdata.ctid) LIMIT 1",
583+
"newdata.ctid)",
584584
tempname, tempname);
585585
if (SPI_execute(querybuf.data, false, 1) != SPI_OK_SELECT)
586586
elog(ERROR, "SPI_exec failed: %s", querybuf.data);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.