mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
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
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -236,7 +236,7 @@ extract_or_clause(RestrictInfo *or_rinfo, RelOptInfo *rel)
|
||||
subclause = (Node *) make_ands_explicit(subclauses);
|
||||
if (is_orclause(subclause))
|
||||
clauselist = list_concat(clauselist,
|
||||
list_copy(((BoolExpr *) subclause)->args));
|
||||
((BoolExpr *) subclause)->args);
|
||||
else
|
||||
clauselist = lappend(clauselist, subclause);
|
||||
}
|
||||
|
@@ -1718,43 +1718,39 @@ build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel,
|
||||
*/
|
||||
for (cnt = 0; cnt < partnatts; cnt++)
|
||||
{
|
||||
List *outer_expr;
|
||||
List *outer_null_expr;
|
||||
List *inner_expr;
|
||||
List *inner_null_expr;
|
||||
/* mark these const to enforce that we copy them properly */
|
||||
const List *outer_expr = outer_rel->partexprs[cnt];
|
||||
const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
|
||||
const List *inner_expr = inner_rel->partexprs[cnt];
|
||||
const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
|
||||
List *partexpr = NIL;
|
||||
List *nullable_partexpr = NIL;
|
||||
|
||||
outer_expr = list_copy(outer_rel->partexprs[cnt]);
|
||||
outer_null_expr = list_copy(outer_rel->nullable_partexprs[cnt]);
|
||||
inner_expr = list_copy(inner_rel->partexprs[cnt]);
|
||||
inner_null_expr = list_copy(inner_rel->nullable_partexprs[cnt]);
|
||||
|
||||
switch (jointype)
|
||||
{
|
||||
case JOIN_INNER:
|
||||
partexpr = list_concat(outer_expr, inner_expr);
|
||||
nullable_partexpr = list_concat(outer_null_expr,
|
||||
inner_null_expr);
|
||||
partexpr = list_concat_copy(outer_expr, inner_expr);
|
||||
nullable_partexpr = list_concat_copy(outer_null_expr,
|
||||
inner_null_expr);
|
||||
break;
|
||||
|
||||
case JOIN_SEMI:
|
||||
case JOIN_ANTI:
|
||||
partexpr = outer_expr;
|
||||
nullable_partexpr = outer_null_expr;
|
||||
partexpr = list_copy(outer_expr);
|
||||
nullable_partexpr = list_copy(outer_null_expr);
|
||||
break;
|
||||
|
||||
case JOIN_LEFT:
|
||||
partexpr = outer_expr;
|
||||
nullable_partexpr = list_concat(inner_expr,
|
||||
outer_null_expr);
|
||||
partexpr = list_copy(outer_expr);
|
||||
nullable_partexpr = list_concat_copy(inner_expr,
|
||||
outer_null_expr);
|
||||
nullable_partexpr = list_concat(nullable_partexpr,
|
||||
inner_null_expr);
|
||||
break;
|
||||
|
||||
case JOIN_FULL:
|
||||
nullable_partexpr = list_concat(outer_expr,
|
||||
inner_expr);
|
||||
nullable_partexpr = list_concat_copy(outer_expr,
|
||||
inner_expr);
|
||||
nullable_partexpr = list_concat(nullable_partexpr,
|
||||
outer_null_expr);
|
||||
nullable_partexpr = list_concat(nullable_partexpr,
|
||||
|
@@ -665,9 +665,8 @@ make_tlist_from_pathtarget(PathTarget *target)
|
||||
* copy_pathtarget
|
||||
* Copy a PathTarget.
|
||||
*
|
||||
* The new PathTarget has its own List cells, but shares the underlying
|
||||
* target expression trees with the old one. We duplicate the List cells
|
||||
* so that items can be added to one target without damaging the other.
|
||||
* The new PathTarget has its own exprs List, but shares the underlying
|
||||
* target expression trees with the old one.
|
||||
*/
|
||||
PathTarget *
|
||||
copy_pathtarget(PathTarget *src)
|
||||
|
Reference in New Issue
Block a user