Acquire locks on views in AcquirePlannerLocks, too.
authorTom Lane <[email protected]>
Wed, 5 Apr 2023 19:56:07 +0000 (15:56 -0400)
committerTom Lane <[email protected]>
Wed, 5 Apr 2023 19:56:35 +0000 (15:56 -0400)
Commit 47bb9db75 taught AcquireExecutorLocks to re-acquire locks
on views using data from their RTE_SUBQUERY replacements, but
it now seems like we should make AcquirePlannerLocks do the same.
In this way, if a view has been redefined, we will notice that
a bit earlier while checking validity of a cached plan and thereby
avoid some wasted work.

Report and patch by Amit Langote.

Discussion: https://postgr.es/m/CA+HiwqH0xZOQ+GQAdKeckY1R4NOeHdzhtfxkAMJLSchpapNk5w@mail.gmail.com

src/backend/utils/cache/plancache.c

index 77c2ba3f8f4ae08cb144ebaa4effa86a9c005171..87210fcf627de183942e5720cfc4b58b345fe63d 100644 (file)
@@ -1846,6 +1846,14 @@ ScanQueryForLocks(Query *parsetree, bool acquire)
                break;
 
            case RTE_SUBQUERY:
+               /* If this was a view, must lock/unlock the view */
+               if (OidIsValid(rte->relid))
+               {
+                   if (acquire)
+                       LockRelationOid(rte->relid, rte->rellockmode);
+                   else
+                       UnlockRelationOid(rte->relid, rte->rellockmode);
+               }
                /* Recurse into subquery-in-FROM */
                ScanQueryForLocks(rte->subquery, acquire);
                break;