Hi,
In create_merge_append_path() we have following code
1331
1332 /* Now we can compute total costs of the MergeAppend */
1333 cost_merge_append(&pathnode->path, root,
1334 pathkeys, list_length(subpaths),
1335 input_startup_cost, input_total_cost,
1336 rel->tuples);
1337
The last arguemnt to cost_merge_append() is described as
'tuples' is the number of tuples in all the streams
For an append relation, set_append_rel_size() sets rel->tuples to the
sum of rows output by each child i.e. sum of rel->rows from each
child.
1091 rel->rows = parent_rows;
1092 rel->reltarget->width = rint(parent_size / parent_rows);
1093 for (i = 0; i < nattrs; i++)
1094 rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
1095
1096 /*
1097 * Set "raw tuples" count equal to "rows" for the appendrel; needed
1098 * because some places assume rel->tuples is valid for any baserel.
1099 */
1100 rel->tuples = parent_rows;
AFAIU, There's difference in the tuples and rows members of
RelOptInfo. While tuples gives the estimate of number of tuples per
pg_class, rows gives the number of rows output by that relation. From
that perspective rel->tuples should be set of the sum of
child_rel->tuples from each of the children. If we do that the last
argument to cost_merge_append() should change from rel->tuples to
rel->rows.
Does that make sense? Attached patch makes those changes.
--
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company