summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/util/clauses.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-08-12 11:20:18 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2019-08-12 11:20:18 -0400
commit5ee190f8ec37c1bbfb3061e18304e155d600bc8e (patch)
tree5767a0de205205f91373bd5645f9c09bd046b0cf /src/backend/optimizer/util/clauses.c
parent251c8e39bc6b0a3ff1620d9ac10888a7660e6b88 (diff)
Rationalize use of list_concat + list_copy combinations.
In the wake of commit 1cff1b95a, the result of list_concat no longer shares the ListCells of the second input. Therefore, we can replace "list_concat(x, list_copy(y))" with just "list_concat(x, y)". To improve call sites that were list_copy'ing the first argument, or both arguments, invent "list_concat_copy()" which produces a new list sharing no ListCells with either input. (This is a bit faster than "list_concat(list_copy(x), y)" because it makes the result list the right size to start with.) In call sites that were not list_copy'ing the second argument, the new semantics mean that we are usually leaking the second List's storage, since typically there is no remaining pointer to it. We considered inventing another list_copy variant that would list_free the second input, but concluded that for most call sites it isn't worth worrying about, given the relative compactness of the new List representation. (Note that in cases where such leakage would happen, the old code already leaked the second List's header; so we're only discussing the size of the leak not whether there is one. I did adjust two or three places that had been troubling to free that header so that they manually free the whole second List.) Patch by me; thanks to David Rowley for review. Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
Diffstat (limited to 'src/backend/optimizer/util/clauses.c')
-rw-r--r--src/backend/optimizer/util/clauses.c54
1 files changed, 21 insertions, 33 deletions
diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 7ad9d9ab79e..a04b62274d2 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -1009,8 +1009,8 @@ max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
return true;
save_safe_param_ids = context->safe_param_ids;
- context->safe_param_ids = list_concat(list_copy(subplan->paramIds),
- context->safe_param_ids);
+ context->safe_param_ids = list_concat_copy(context->safe_param_ids,
+ subplan->paramIds);
if (max_parallel_hazard_walker(subplan->testexpr, context))
return true; /* no need to restore safe_param_ids */
list_free(context->safe_param_ids);
@@ -3697,18 +3697,12 @@ simplify_or_arguments(List *args,
/* flatten nested ORs as per above comment */
if (is_orclause(arg))
{
- List *subargs = list_copy(((BoolExpr *) arg)->args);
+ List *subargs = ((BoolExpr *) arg)->args;
+ List *oldlist = unprocessed_args;
- /* overly tense code to avoid leaking unused list header */
- if (!unprocessed_args)
- unprocessed_args = subargs;
- else
- {
- List *oldhdr = unprocessed_args;
-
- unprocessed_args = list_concat(subargs, unprocessed_args);
- pfree(oldhdr);
- }
+ unprocessed_args = list_concat_copy(subargs, unprocessed_args);
+ /* perhaps-overly-tense code to avoid leaking old lists */
+ list_free(oldlist);
continue;
}
@@ -3718,14 +3712,14 @@ simplify_or_arguments(List *args,
/*
* It is unlikely but not impossible for simplification of a non-OR
* clause to produce an OR. Recheck, but don't be too tense about it
- * since it's not a mainstream case. In particular we don't worry
- * about const-simplifying the input twice.
+ * since it's not a mainstream case. In particular we don't worry
+ * about const-simplifying the input twice, nor about list leakage.
*/
if (is_orclause(arg))
{
- List *subargs = list_copy(((BoolExpr *) arg)->args);
+ List *subargs = ((BoolExpr *) arg)->args;
- unprocessed_args = list_concat(subargs, unprocessed_args);
+ unprocessed_args = list_concat_copy(subargs, unprocessed_args);
continue;
}
@@ -3799,18 +3793,12 @@ simplify_and_arguments(List *args,
/* flatten nested ANDs as per above comment */
if (is_andclause(arg))
{
- List *subargs = list_copy(((BoolExpr *) arg)->args);
+ List *subargs = ((BoolExpr *) arg)->args;
+ List *oldlist = unprocessed_args;
- /* overly tense code to avoid leaking unused list header */
- if (!unprocessed_args)
- unprocessed_args = subargs;
- else
- {
- List *oldhdr = unprocessed_args;
-
- unprocessed_args = list_concat(subargs, unprocessed_args);
- pfree(oldhdr);
- }
+ unprocessed_args = list_concat_copy(subargs, unprocessed_args);
+ /* perhaps-overly-tense code to avoid leaking old lists */
+ list_free(oldlist);
continue;
}
@@ -3820,14 +3808,14 @@ simplify_and_arguments(List *args,
/*
* It is unlikely but not impossible for simplification of a non-AND
* clause to produce an AND. Recheck, but don't be too tense about it
- * since it's not a mainstream case. In particular we don't worry
- * about const-simplifying the input twice.
+ * since it's not a mainstream case. In particular we don't worry
+ * about const-simplifying the input twice, nor about list leakage.
*/
if (is_andclause(arg))
{
- List *subargs = list_copy(((BoolExpr *) arg)->args);
+ List *subargs = ((BoolExpr *) arg)->args;
- unprocessed_args = list_concat(subargs, unprocessed_args);
+ unprocessed_args = list_concat_copy(subargs, unprocessed_args);
continue;
}
@@ -4188,7 +4176,7 @@ add_function_defaults(List *args, HeapTuple func_tuple)
defaults = list_copy_tail(defaults, ndelete);
/* And form the combined argument list, not modifying the input list */
- return list_concat(list_copy(args), defaults);
+ return list_concat_copy(args, defaults);
}
/*