Skip to content

Commit 2ae8a01

Browse files
committed
Fix planning of SELECT FOR UPDATE on child table with partial index.
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.
1 parent f14196c commit 2ae8a01

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ postgresGetForeignPlan(PlannerInfo *root,
819819
}
820820
else
821821
{
822-
RowMarkClause *rc = get_parse_rowmark(root->parse, baserel->relid);
822+
PlanRowMark *rc = get_plan_rowmark(root->rowMarks, baserel->relid);
823823

824824
if (rc)
825825
{
@@ -832,15 +832,18 @@ postgresGetForeignPlan(PlannerInfo *root,
832832
* complete information about, and (b) it wouldn't work anyway on
833833
* older remote servers. Likewise, we don't worry about NOWAIT.
834834
*/
835-
switch (rc->strength)
835+
switch (rc->markType)
836836
{
837-
case LCS_FORKEYSHARE:
838-
case LCS_FORSHARE:
839-
appendStringInfo(&sql, " FOR SHARE");
837+
case ROW_MARK_EXCLUSIVE:
838+
case ROW_MARK_NOKEYEXCLUSIVE:
839+
appendStringInfoString(&sql, " FOR UPDATE");
840840
break;
841-
case LCS_FORNOKEYUPDATE:
842-
case LCS_FORUPDATE:
843-
appendStringInfo(&sql, " FOR UPDATE");
841+
case ROW_MARK_SHARE:
842+
case ROW_MARK_KEYSHARE:
843+
appendStringInfoString(&sql, " FOR SHARE");
844+
break;
845+
default:
846+
/* nothing needed */
844847
break;
845848
}
846849
}

src/backend/optimizer/plan/createplan.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "optimizer/planmain.h"
3535
#include "optimizer/planner.h"
3636
#include "optimizer/predtest.h"
37+
#include "optimizer/prep.h"
3738
#include "optimizer/restrictinfo.h"
3839
#include "optimizer/subselect.h"
3940
#include "optimizer/tlist.h"
@@ -1221,7 +1222,7 @@ create_indexscan_plan(PlannerInfo *root,
12211222
if (best_path->indexinfo->indpred)
12221223
{
12231224
if (baserelid != root->parse->resultRelation &&
1224-
get_parse_rowmark(root->parse, baserelid) == NULL)
1225+
get_plan_rowmark(root->rowMarks, baserelid) == NULL)
12251226
if (predicate_implied_by(clausel,
12261227
best_path->indexinfo->indpred))
12271228
continue; /* implied by index predicate */

0 commit comments

Comments
 (0)