Fix planning of SELECT FOR UPDATE on child table with partial index.
authorTom Lane
Fri, 12 Dec 2014 02:02:31 +0000 (21:02 -0500)
committerTom Lane
Fri, 12 Dec 2014 02:02:31 +0000 (21:02 -0500)
Ordinarily we can omit checking of a WHERE condition that matches a partial
index's condition, when we are using an indexscan on that partial index.
However, in SELECT FOR UPDATE we must include the "redundant" filter
condition in the plan so that it gets checked properly in an EvalPlanQual
recheck.  The planner got this mostly right, but improperly omitted the
filter condition if the index in question was on an inheritance child
table.  In READ COMMITTED mode, this could result in incorrectly returning
just-updated rows that no longer satisfy the filter condition.

The cause of the error is using get_parse_rowmark() when get_plan_rowmark()
is what should be used during planning.  In 9.3 and up, also fix the same
mistake in contrib/postgres_fdw.  It's currently harmless there (for lack
of inheritance support) but wrong is wrong, and the incorrect code might
get copied to someplace where it's more significant.

Report and fix by Kyotaro Horiguchi.  Back-patch to all supported branches.

contrib/postgres_fdw/postgres_fdw.c
src/backend/optimizer/plan/createplan.c

index f7ab0857a9167163822e4ee6d38a8b39a84e96fd..0d629e6687bf2424cc6e6aff8b09b3f960e9560d 100644 (file)
@@ -819,7 +819,7 @@ postgresGetForeignPlan(PlannerInfo *root,
    }
    else
    {
-       RowMarkClause *rc = get_parse_rowmark(root->parse, baserel->relid);
+       PlanRowMark *rc = get_plan_rowmark(root->rowMarks, baserel->relid);
 
        if (rc)
        {
@@ -832,15 +832,18 @@ postgresGetForeignPlan(PlannerInfo *root,
             * complete information about, and (b) it wouldn't work anyway on
             * older remote servers.  Likewise, we don't worry about NOWAIT.
             */
-           switch (rc->strength)
+           switch (rc->markType)
            {
-               case LCS_FORKEYSHARE:
-               case LCS_FORSHARE:
-                   appendStringInfo(&sql, " FOR SHARE");
+               case ROW_MARK_EXCLUSIVE:
+               case ROW_MARK_NOKEYEXCLUSIVE:
+                   appendStringInfoString(&sql, " FOR UPDATE");
                    break;
-               case LCS_FORNOKEYUPDATE:
-               case LCS_FORUPDATE:
-                   appendStringInfo(&sql, " FOR UPDATE");
+               case ROW_MARK_SHARE:
+               case ROW_MARK_KEYSHARE:
+                   appendStringInfoString(&sql, " FOR SHARE");
+                   break;
+               default:
+                   /* nothing needed */
                    break;
            }
        }
index 48b87a0f112e0b72961f453e3a76bf36e08fbf3b..38cc2ca2de64f19f3bbf0ac2b46eb3f5f7466aa6 100644 (file)
@@ -34,6 +34,7 @@
 #include "optimizer/planmain.h"
 #include "optimizer/planner.h"
 #include "optimizer/predtest.h"
+#include "optimizer/prep.h"
 #include "optimizer/restrictinfo.h"
 #include "optimizer/subselect.h"
 #include "optimizer/tlist.h"
@@ -1221,7 +1222,7 @@ create_indexscan_plan(PlannerInfo *root,
            if (best_path->indexinfo->indpred)
            {
                if (baserelid != root->parse->resultRelation &&
-                   get_parse_rowmark(root->parse, baserelid) == NULL)
+                   get_plan_rowmark(root->rowMarks, baserelid) == NULL)
                    if (predicate_implied_by(clausel,
                                             best_path->indexinfo->indpred))
                        continue;       /* implied by index predicate */