Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4ceaa76

Browse files
committed
Reduce risks of conflicts in internal queries of REFRESH MATVIEW CONCURRENTLY
The internal SQL queries used by REFRESH MATERIALIZED VIEW CONCURRENTLY include some aliases for its diff and temporary relations with rather-generic names: diff, newdata, newdata2 and mv. Depending on the queries used for the materialized view, using CONCURRENTLY could lead to some internal failures if the query and those internal aliases conflict. Those names have been chosen in 841c29c. This commit switches instead to a naming pattern which is less likely going to cause conflicts, based on an idea from Thomas Munro, by appending _$ to those aliases. This is not perfect as those new names could still conflict, but at least it has the advantage to keep the code readable and simple while reducing the likelihood of conflicts to be close to zero. Reported-by: Mathis Rudolf Author: Bharath Rupireddy Reviewed-by: Bernd Helmle, Thomas Munro, Michael Paquier Discussion: https://postgr.es/m/109c267a-10d2-3c53-b60e-720fcf44d9e8@credativ.de Backpatch-through: 9.6
1 parent ebd542b commit 4ceaa76

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/backend/commands/matview.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,12 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
622622
*/
623623
resetStringInfo(&querybuf);
624624
appendStringInfo(&querybuf,
625-
"SELECT newdata FROM %s newdata "
626-
"WHERE newdata IS NOT NULL AND EXISTS "
627-
"(SELECT 1 FROM %s newdata2 WHERE newdata2 IS NOT NULL "
628-
"AND newdata2 OPERATOR(pg_catalog.*=) newdata "
629-
"AND newdata2.ctid OPERATOR(pg_catalog.<>) "
630-
"newdata.ctid)",
625+
"SELECT _$newdata FROM %s _$newdata "
626+
"WHERE _$newdata IS NOT NULL AND EXISTS "
627+
"(SELECT 1 FROM %s _$newdata2 WHERE _$newdata2 IS NOT NULL "
628+
"AND _$newdata2 OPERATOR(pg_catalog.*=) _$newdata "
629+
"AND _$newdata2.ctid OPERATOR(pg_catalog.<>) "
630+
"_$newdata.ctid)",
631631
tempname, tempname);
632632
if (SPI_execute(querybuf.data, false, 1) != SPI_OK_SELECT)
633633
elog(ERROR, "SPI_exec failed: %s", querybuf.data);
@@ -655,8 +655,8 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
655655
resetStringInfo(&querybuf);
656656
appendStringInfo(&querybuf,
657657
"CREATE TEMP TABLE %s AS "
658-
"SELECT mv.ctid AS tid, newdata "
659-
"FROM %s mv FULL JOIN %s newdata ON (",
658+
"SELECT _$mv.ctid AS tid, _$newdata "
659+
"FROM %s _$mv FULL JOIN %s _$newdata ON (",
660660
diffname, matviewname, tempname);
661661

662662
/*
@@ -749,9 +749,9 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
749749
if (foundUniqueIndex)
750750
appendStringInfoString(&querybuf, " AND ");
751751

752-
leftop = quote_qualified_identifier("newdata",
752+
leftop = quote_qualified_identifier("_$newdata",
753753
NameStr(attr->attname));
754-
rightop = quote_qualified_identifier("mv",
754+
rightop = quote_qualified_identifier("_$mv",
755755
NameStr(attr->attname));
756756

757757
generate_operator_clause(&querybuf,
@@ -779,8 +779,8 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
779779
Assert(foundUniqueIndex);
780780

781781
appendStringInfoString(&querybuf,
782-
" AND newdata OPERATOR(pg_catalog.*=) mv) "
783-
"WHERE newdata IS NULL OR mv IS NULL "
782+
" AND _$newdata OPERATOR(pg_catalog.*=) _$mv) "
783+
"WHERE _$newdata IS NULL OR _$mv IS NULL "
784784
"ORDER BY tid");
785785

786786
/* Create the temporary "diff" table. */
@@ -806,19 +806,19 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
806806
/* Deletes must come before inserts; do them first. */
807807
resetStringInfo(&querybuf);
808808
appendStringInfo(&querybuf,
809-
"DELETE FROM %s mv WHERE ctid OPERATOR(pg_catalog.=) ANY "
810-
"(SELECT diff.tid FROM %s diff "
811-
"WHERE diff.tid IS NOT NULL "
812-
"AND diff.newdata IS NULL)",
809+
"DELETE FROM %s _$mv WHERE ctid OPERATOR(pg_catalog.=) ANY "
810+
"(SELECT _$diff.tid FROM %s _$diff "
811+
"WHERE _$diff.tid IS NOT NULL "
812+
"AND _$diff._$newdata IS NULL)",
813813
matviewname, diffname);
814814
if (SPI_exec(querybuf.data, 0) != SPI_OK_DELETE)
815815
elog(ERROR, "SPI_exec failed: %s", querybuf.data);
816816

817817
/* Inserts go last. */
818818
resetStringInfo(&querybuf);
819819
appendStringInfo(&querybuf,
820-
"INSERT INTO %s SELECT (diff.newdata).* "
821-
"FROM %s diff WHERE tid IS NULL",
820+
"INSERT INTO %s SELECT (_$diff._$newdata).* "
821+
"FROM %s _$diff WHERE tid IS NULL",
822822
matviewname, diffname);
823823
if (SPI_exec(querybuf.data, 0) != SPI_OK_INSERT)
824824
elog(ERROR, "SPI_exec failed: %s", querybuf.data);

0 commit comments

Comments
 (0)