mirror of
https://github.com/postgres/postgres.git
synced 2025-04-21 12:05:57 +03:00
Fix assorted missing infrastructure for ON CONFLICT.
subquery_planner() failed to apply expression preprocessing to the arbiterElems and arbiterWhere fields of an OnConflictExpr. No doubt the theory was that this wasn't necessary because we don't actually try to execute those expressions; but that's wrong, because it results in failure to match to index expressions or index predicates that are changed at all by preprocessing. Per bug #14132 from Reynold Smith. Also add pullup_replace_vars processing for onConflictWhere. Perhaps it's impossible to have a subquery reference there, but I'm not exactly convinced; and even if true today it's a failure waiting to happen. Also add some comments to other places where one or another field of OnConflictExpr is intentionally ignored, with explanation as to why it's okay to do so. Also, catalog/dependency.c failed to record any dependency on the named constraint in ON CONFLICT ON CONSTRAINT, allowing such a constraint to be dropped while rules exist that depend on it, and allowing pg_dump to dump such a rule before the constraint it refers to. The normal execution path managed to error out reasonably for a dangling constraint reference, but ruleutils.c dumped core; so in addition to fixing the omission, add a protective check in ruleutils.c, since we can't retroactively add a dependency in existing databases. Back-patch to 9.5 where this code was introduced. Report: <20160510190350.2608.48667@wrigleys.postgresql.org>
This commit is contained in:
parent
9be58a2b8e
commit
26e66184d6
@ -1789,6 +1789,15 @@ find_expr_references_walker(Node *node,
|
|||||||
add_object_address(OCLASS_TYPE, cd->resulttype, 0,
|
add_object_address(OCLASS_TYPE, cd->resulttype, 0,
|
||||||
context->addrs);
|
context->addrs);
|
||||||
}
|
}
|
||||||
|
else if (IsA(node, OnConflictExpr))
|
||||||
|
{
|
||||||
|
OnConflictExpr *onconflict = (OnConflictExpr *) node;
|
||||||
|
|
||||||
|
if (OidIsValid(onconflict->constraint))
|
||||||
|
add_object_address(OCLASS_CONSTRAINT, onconflict->constraint, 0,
|
||||||
|
context->addrs);
|
||||||
|
/* fall through to examine arguments */
|
||||||
|
}
|
||||||
else if (IsA(node, SortGroupClause))
|
else if (IsA(node, SortGroupClause))
|
||||||
{
|
{
|
||||||
SortGroupClause *sgc = (SortGroupClause *) node;
|
SortGroupClause *sgc = (SortGroupClause *) node;
|
||||||
|
@ -77,6 +77,7 @@ create_upper_paths_hook_type create_upper_paths_hook = NULL;
|
|||||||
#define EXPRKIND_APPINFO 7
|
#define EXPRKIND_APPINFO 7
|
||||||
#define EXPRKIND_PHV 8
|
#define EXPRKIND_PHV 8
|
||||||
#define EXPRKIND_TABLESAMPLE 9
|
#define EXPRKIND_TABLESAMPLE 9
|
||||||
|
#define EXPRKIND_ARBITER_ELEM 10
|
||||||
|
|
||||||
/* Passthrough data for standard_qp_callback */
|
/* Passthrough data for standard_qp_callback */
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -620,13 +621,23 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
|
|||||||
|
|
||||||
if (parse->onConflict)
|
if (parse->onConflict)
|
||||||
{
|
{
|
||||||
parse->onConflict->onConflictSet = (List *)
|
parse->onConflict->arbiterElems = (List *)
|
||||||
preprocess_expression(root, (Node *) parse->onConflict->onConflictSet,
|
preprocess_expression(root,
|
||||||
EXPRKIND_TARGET);
|
(Node *) parse->onConflict->arbiterElems,
|
||||||
|
EXPRKIND_ARBITER_ELEM);
|
||||||
parse->onConflict->onConflictWhere =
|
parse->onConflict->arbiterWhere =
|
||||||
preprocess_expression(root, (Node *) parse->onConflict->onConflictWhere,
|
preprocess_expression(root,
|
||||||
|
parse->onConflict->arbiterWhere,
|
||||||
EXPRKIND_QUAL);
|
EXPRKIND_QUAL);
|
||||||
|
parse->onConflict->onConflictSet = (List *)
|
||||||
|
preprocess_expression(root,
|
||||||
|
(Node *) parse->onConflict->onConflictSet,
|
||||||
|
EXPRKIND_TARGET);
|
||||||
|
parse->onConflict->onConflictWhere =
|
||||||
|
preprocess_expression(root,
|
||||||
|
parse->onConflict->onConflictWhere,
|
||||||
|
EXPRKIND_QUAL);
|
||||||
|
/* exclRelTlist contains only Vars, so no preprocessing needed */
|
||||||
}
|
}
|
||||||
|
|
||||||
root->append_rel_list = (List *)
|
root->append_rel_list = (List *)
|
||||||
|
@ -2507,6 +2507,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
|
|||||||
&context);
|
&context);
|
||||||
finalize_primnode((Node *) mtplan->onConflictWhere,
|
finalize_primnode((Node *) mtplan->onConflictWhere,
|
||||||
&context);
|
&context);
|
||||||
|
/* exclRelTlist contains only Vars, doesn't need examination */
|
||||||
foreach(l, mtplan->plans)
|
foreach(l, mtplan->plans)
|
||||||
{
|
{
|
||||||
context.paramids =
|
context.paramids =
|
||||||
|
@ -1039,8 +1039,19 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
|
|||||||
parse->returningList = (List *)
|
parse->returningList = (List *)
|
||||||
pullup_replace_vars((Node *) parse->returningList, &rvcontext);
|
pullup_replace_vars((Node *) parse->returningList, &rvcontext);
|
||||||
if (parse->onConflict)
|
if (parse->onConflict)
|
||||||
|
{
|
||||||
parse->onConflict->onConflictSet = (List *)
|
parse->onConflict->onConflictSet = (List *)
|
||||||
pullup_replace_vars((Node *) parse->onConflict->onConflictSet, &rvcontext);
|
pullup_replace_vars((Node *) parse->onConflict->onConflictSet,
|
||||||
|
&rvcontext);
|
||||||
|
parse->onConflict->onConflictWhere =
|
||||||
|
pullup_replace_vars(parse->onConflict->onConflictWhere,
|
||||||
|
&rvcontext);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
|
||||||
|
* can't contain any references to a subquery
|
||||||
|
*/
|
||||||
|
}
|
||||||
replace_vars_in_jointree((Node *) parse->jointree, &rvcontext,
|
replace_vars_in_jointree((Node *) parse->jointree, &rvcontext,
|
||||||
lowest_nulling_outer_join);
|
lowest_nulling_outer_join);
|
||||||
Assert(parse->setOperations == NULL);
|
Assert(parse->setOperations == NULL);
|
||||||
@ -1633,8 +1644,19 @@ pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
|
|||||||
parse->returningList = (List *)
|
parse->returningList = (List *)
|
||||||
pullup_replace_vars((Node *) parse->returningList, &rvcontext);
|
pullup_replace_vars((Node *) parse->returningList, &rvcontext);
|
||||||
if (parse->onConflict)
|
if (parse->onConflict)
|
||||||
|
{
|
||||||
parse->onConflict->onConflictSet = (List *)
|
parse->onConflict->onConflictSet = (List *)
|
||||||
pullup_replace_vars((Node *) parse->onConflict->onConflictSet, &rvcontext);
|
pullup_replace_vars((Node *) parse->onConflict->onConflictSet,
|
||||||
|
&rvcontext);
|
||||||
|
parse->onConflict->onConflictWhere =
|
||||||
|
pullup_replace_vars(parse->onConflict->onConflictWhere,
|
||||||
|
&rvcontext);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
|
||||||
|
* can't contain any references to a subquery
|
||||||
|
*/
|
||||||
|
}
|
||||||
replace_vars_in_jointree((Node *) parse->jointree, &rvcontext, NULL);
|
replace_vars_in_jointree((Node *) parse->jointree, &rvcontext, NULL);
|
||||||
Assert(parse->setOperations == NULL);
|
Assert(parse->setOperations == NULL);
|
||||||
parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
|
parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
|
||||||
|
@ -623,7 +623,6 @@ infer_arbiter_indexes(PlannerInfo *root)
|
|||||||
Bitmapset *indexedAttrs = NULL;
|
Bitmapset *indexedAttrs = NULL;
|
||||||
List *idxExprs;
|
List *idxExprs;
|
||||||
List *predExprs;
|
List *predExprs;
|
||||||
List *whereExplicit;
|
|
||||||
AttrNumber natt;
|
AttrNumber natt;
|
||||||
ListCell *el;
|
ListCell *el;
|
||||||
|
|
||||||
@ -685,6 +684,7 @@ infer_arbiter_indexes(PlannerInfo *root)
|
|||||||
{
|
{
|
||||||
int attno = idxRel->rd_index->indkey.values[natt];
|
int attno = idxRel->rd_index->indkey.values[natt];
|
||||||
|
|
||||||
|
/* XXX broken */
|
||||||
if (attno < 0)
|
if (attno < 0)
|
||||||
elog(ERROR, "system column in index");
|
elog(ERROR, "system column in index");
|
||||||
|
|
||||||
@ -745,13 +745,12 @@ infer_arbiter_indexes(PlannerInfo *root)
|
|||||||
goto next;
|
goto next;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Any user-supplied ON CONFLICT unique index inference WHERE clause
|
* If it's a partial index, its predicate must be implied by the ON
|
||||||
* need only be implied by the cataloged index definitions predicate.
|
* CONFLICT's WHERE clause.
|
||||||
*/
|
*/
|
||||||
predExprs = RelationGetIndexPredicate(idxRel);
|
predExprs = RelationGetIndexPredicate(idxRel);
|
||||||
whereExplicit = make_ands_implicit((Expr *) onconflict->arbiterWhere);
|
|
||||||
|
|
||||||
if (!predicate_implied_by(predExprs, whereExplicit))
|
if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere))
|
||||||
goto next;
|
goto next;
|
||||||
|
|
||||||
results = lappend_oid(results, idxForm->indexrelid);
|
results = lappend_oid(results, idxForm->indexrelid);
|
||||||
|
@ -5570,12 +5570,15 @@ get_insert_query_def(Query *query, deparse_context *context)
|
|||||||
context->varprefix = save_varprefix;
|
context->varprefix = save_varprefix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (confl->constraint != InvalidOid)
|
else if (OidIsValid(confl->constraint))
|
||||||
{
|
{
|
||||||
char *constraint = get_constraint_name(confl->constraint);
|
char *constraint = get_constraint_name(confl->constraint);
|
||||||
|
|
||||||
|
if (!constraint)
|
||||||
|
elog(ERROR, "cache lookup failed for constraint %u",
|
||||||
|
confl->constraint);
|
||||||
appendStringInfo(buf, " ON CONSTRAINT %s",
|
appendStringInfo(buf, " ON CONSTRAINT %s",
|
||||||
quote_qualified_identifier(NULL, constraint));
|
quote_identifier(constraint));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (confl->action == ONCONFLICT_NOTHING)
|
if (confl->action == ONCONFLICT_NOTHING)
|
||||||
|
@ -454,6 +454,21 @@ ERROR: column excluded.oid does not exist
|
|||||||
LINE 1: ...values (1) on conflict (key) do update set data = excluded.o...
|
LINE 1: ...values (1) on conflict (key) do update set data = excluded.o...
|
||||||
^
|
^
|
||||||
drop table syscolconflicttest;
|
drop table syscolconflicttest;
|
||||||
|
--
|
||||||
|
-- Previous tests all managed to not test any expressions requiring
|
||||||
|
-- planner preprocessing ...
|
||||||
|
--
|
||||||
|
create table insertconflict (a bigint, b bigint);
|
||||||
|
create unique index insertconflicti1 on insertconflict(coalesce(a, 0));
|
||||||
|
create unique index insertconflicti2 on insertconflict(b)
|
||||||
|
where coalesce(a, 1) > 0;
|
||||||
|
insert into insertconflict values (1, 2)
|
||||||
|
on conflict (coalesce(a, 0)) do nothing;
|
||||||
|
insert into insertconflict values (1, 2)
|
||||||
|
on conflict (b) where coalesce(a, 1) > 0 do nothing;
|
||||||
|
insert into insertconflict values (1, 2)
|
||||||
|
on conflict (b) where coalesce(a, 1) > 1 do nothing;
|
||||||
|
drop table insertconflict;
|
||||||
-- ******************************************************************
|
-- ******************************************************************
|
||||||
-- * *
|
-- * *
|
||||||
-- * Test inheritance (example taken from tutorial) *
|
-- * Test inheritance (example taken from tutorial) *
|
||||||
|
@ -261,6 +261,28 @@ insert into syscolconflicttest values (1) on conflict (key) do update set data =
|
|||||||
insert into syscolconflicttest values (1) on conflict (key) do update set data = excluded.oid::text;
|
insert into syscolconflicttest values (1) on conflict (key) do update set data = excluded.oid::text;
|
||||||
drop table syscolconflicttest;
|
drop table syscolconflicttest;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Previous tests all managed to not test any expressions requiring
|
||||||
|
-- planner preprocessing ...
|
||||||
|
--
|
||||||
|
create table insertconflict (a bigint, b bigint);
|
||||||
|
|
||||||
|
create unique index insertconflicti1 on insertconflict(coalesce(a, 0));
|
||||||
|
|
||||||
|
create unique index insertconflicti2 on insertconflict(b)
|
||||||
|
where coalesce(a, 1) > 0;
|
||||||
|
|
||||||
|
insert into insertconflict values (1, 2)
|
||||||
|
on conflict (coalesce(a, 0)) do nothing;
|
||||||
|
|
||||||
|
insert into insertconflict values (1, 2)
|
||||||
|
on conflict (b) where coalesce(a, 1) > 0 do nothing;
|
||||||
|
|
||||||
|
insert into insertconflict values (1, 2)
|
||||||
|
on conflict (b) where coalesce(a, 1) > 1 do nothing;
|
||||||
|
|
||||||
|
drop table insertconflict;
|
||||||
|
|
||||||
|
|
||||||
-- ******************************************************************
|
-- ******************************************************************
|
||||||
-- * *
|
-- * *
|
||||||
|
Loading…
x
Reference in New Issue
Block a user