Skip to content

Commit d9525c4

Browse files
committedJun 3, 2021
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 a886e2f commit d9525c4

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed
 

‎src/backend/commands/matview.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,12 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
601601
*/
602602
resetStringInfo(&querybuf);
603603
appendStringInfo(&querybuf,
604-
"SELECT newdata FROM %s newdata "
605-
"WHERE newdata IS NOT NULL AND EXISTS "
606-
"(SELECT 1 FROM %s newdata2 WHERE newdata2 IS NOT NULL "
607-
"AND newdata2 OPERATOR(pg_catalog.*=) newdata "
608-
"AND newdata2.ctid OPERATOR(pg_catalog.<>) "
609-
"newdata.ctid)",
604+
"SELECT _$newdata FROM %s _$newdata "
605+
"WHERE _$newdata IS NOT NULL AND EXISTS "
606+
"(SELECT 1 FROM %s _$newdata2 WHERE _$newdata2 IS NOT NULL "
607+
"AND _$newdata2 OPERATOR(pg_catalog.*=) _$newdata "
608+
"AND _$newdata2.ctid OPERATOR(pg_catalog.<>) "
609+
"_$newdata.ctid)",
610610
tempname, tempname);
611611
if (SPI_execute(querybuf.data, false, 1) != SPI_OK_SELECT)
612612
elog(ERROR, "SPI_exec failed: %s", querybuf.data);
@@ -634,8 +634,8 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
634634
resetStringInfo(&querybuf);
635635
appendStringInfo(&querybuf,
636636
"CREATE TEMP TABLE %s AS "
637-
"SELECT mv.ctid AS tid, newdata "
638-
"FROM %s mv FULL JOIN %s newdata ON (",
637+
"SELECT _$mv.ctid AS tid, _$newdata "
638+
"FROM %s _$mv FULL JOIN %s _$newdata ON (",
639639
diffname, matviewname, tempname);
640640

641641
/*
@@ -728,9 +728,9 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
728728
if (foundUniqueIndex)
729729
appendStringInfoString(&querybuf, " AND ");
730730

731-
leftop = quote_qualified_identifier("newdata",
731+
leftop = quote_qualified_identifier("_$newdata",
732732
NameStr(attr->attname));
733-
rightop = quote_qualified_identifier("mv",
733+
rightop = quote_qualified_identifier("_$mv",
734734
NameStr(attr->attname));
735735

736736
generate_operator_clause(&querybuf,
@@ -758,8 +758,8 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
758758
Assert(foundUniqueIndex);
759759

760760
appendStringInfoString(&querybuf,
761-
" AND newdata OPERATOR(pg_catalog.*=) mv) "
762-
"WHERE newdata IS NULL OR mv IS NULL "
761+
" AND _$newdata OPERATOR(pg_catalog.*=) _$mv) "
762+
"WHERE _$newdata IS NULL OR _$mv IS NULL "
763763
"ORDER BY tid");
764764

765765
/* Create the temporary "diff" table. */
@@ -785,19 +785,19 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
785785
/* Deletes must come before inserts; do them first. */
786786
resetStringInfo(&querybuf);
787787
appendStringInfo(&querybuf,
788-
"DELETE FROM %s mv WHERE ctid OPERATOR(pg_catalog.=) ANY "
789-
"(SELECT diff.tid FROM %s diff "
790-
"WHERE diff.tid IS NOT NULL "
791-
"AND diff.newdata IS NULL)",
788+
"DELETE FROM %s _$mv WHERE ctid OPERATOR(pg_catalog.=) ANY "
789+
"(SELECT _$diff.tid FROM %s _$diff "
790+
"WHERE _$diff.tid IS NOT NULL "
791+
"AND _$diff._$newdata IS NULL)",
792792
matviewname, diffname);
793793
if (SPI_exec(querybuf.data, 0) != SPI_OK_DELETE)
794794
elog(ERROR, "SPI_exec failed: %s", querybuf.data);
795795

796796
/* Inserts go last. */
797797
resetStringInfo(&querybuf);
798798
appendStringInfo(&querybuf,
799-
"INSERT INTO %s SELECT (diff.newdata).* "
800-
"FROM %s diff WHERE tid IS NULL",
799+
"INSERT INTO %s SELECT (_$diff._$newdata).* "
800+
"FROM %s _$diff WHERE tid IS NULL",
801801
matviewname, diffname);
802802
if (SPI_exec(querybuf.data, 0) != SPI_OK_INSERT)
803803
elog(ERROR, "SPI_exec failed: %s", querybuf.data);

0 commit comments

Comments
 (0)