postgresql/src/backend
Andres Freund 2657283256 Minimally fix partial aggregation for aggregates that don't have one argument.
For partial aggregation combine steps,
AggStatePerTrans->numTransInputs was set to the transition function's
number of inputs, rather than the combine function's number of
inputs (always 1).

That lead to partial aggregates with strict combine functions to
wrongly check for NOT NULL input as required by strictness. When the
aggregate wasn't exactly passed one argument, the strictness check was
either omitted (in the 0 args case) or too many arguments were
checked. In the latter case we'd read beyond the end of
FunctionCallInfoData->args (only in master).

AggStatePerTrans->numTransInputs actually has been wrong since since
9.6, where partial aggregates were added. But it turns out to not be
an active problem in 9.6 and 10, because numTransInputs wasn't used at
all for combine functions: Before c253b722f6 there simply was no NULL
check for the input to strict trans functions, and after that the
check was simply hardcoded for the right offset in fcinfo, as it's
done by code specific to combine functions.

In bf6c614a2f (11) the strictness check was generalized, with common
code doing the strictness checks for both plain and combine transition
functions, based on numTransInputs. For combine functions this lead to
not emitting an expression step to check for strict input in the 0
arguments case, and in the > 1 arguments case, we'd check too many
arguments.Due to the fact that the relevant fcinfo->isnull[2..] was
always zero-initialized (more or less by accident, by being part of
the AggStatePerTrans struct, which is palloc0'ed), there was no
observable damage in the latter case before a9c35cf85c, we just
checked too many array elements.

Due to the changes in a9c35cf85c, > 1 argument bug became visible,
because these days fcinfo is a) dynamically allocated without being
zeroed b) exactly the length required for the number of specified
arguments (hardcoded to 2 in this case).

This commit only contains a fairly minimal fix, setting numTransInputs
to a hardcoded 1 when building a pertrans for a combine function. It
seems likely that we'll want to clean this up further (e.g. the
arguments build_pertrans_for_aggref() aren't particularly meaningful
for combine functions). But the wrap date for 12 beta1 is coming up
fast, so it seems good to have a minimal fix in place.

Backpatch to 11. While AggStatePerTrans->numTransInputs was set
wrongly before that, the value was not used for combine functions.

Reported-By: Rajkumar Raghuwanshi
Diagnosed-By: Kyotaro Horiguchi, Jeevan Chalke, Andres Freund, David Rowley
Author: David Rowley, Kyotaro Horiguchi, Andres Freund
Discussion: https://postgr.es/m/CAKcux6=uZEyWyLw0N7HtR9OBc-sWEFeByEZC7t-KDf15FKxVew@mail.gmail.com
2019-05-19 18:01:06 -07:00
..
access Don't to predicate lock for analyze scans, refactor scan option passing. 2019-05-19 15:10:28 -07:00
bootstrap In bootstrap mode, use default signal handling for SIGINT etc. 2019-05-14 10:22:28 -04:00
catalog Update SQL features/conformance information to SQL:2016 2019-05-14 15:44:37 +02:00
commands More message style fixes 2019-05-16 19:14:31 -04:00
executor Minimally fix partial aggregation for aggregates that don't have one argument. 2019-05-19 18:01:06 -07:00
foreign Update copyright for 2019 2019-01-02 12:44:25 -05:00
jit Improve comment spelling and style in llvmjit_deform.c. 2019-04-30 16:20:07 -07:00
lib Fix example in comment. 2019-04-09 08:33:42 +03:00
libpq Clean up minor warnings from buildfarm. 2019-04-28 12:45:55 -04:00
main Replace @postgresql.org with @lists.postgresql.org for mailinglists 2019-01-19 19:06:35 +01:00
nodes Restructure creation of run-time pruning steps. 2019-05-17 19:44:34 -04:00
optimizer Repair issues with faulty generation of merge-append plans. 2019-05-09 16:53:05 -04:00
parser More message style fixes 2019-05-16 19:14:31 -04:00
partitioning Restructure creation of run-time pruning steps. 2019-05-17 19:44:34 -04:00
po Translation updates 2018-06-25 12:37:18 +02:00
port Cope with EINVAL and EIDRM shmat() failures in PGSharedMemoryAttach. 2019-05-10 14:56:41 -04:00
postmaster Revert "postmaster: Start syslogger earlier". 2019-05-19 11:14:23 -04:00
regex Fix misoptimization of "{1,1}" quantifiers in regular expressions. 2019-05-12 18:53:38 -04:00
replication More message style fixes 2019-05-16 19:14:31 -04:00
rewrite Perform RLS subquery checks as the right user when going via a view. 2019-04-02 08:13:59 +01:00
snowball Update copyright for 2019 2019-01-02 12:44:25 -05:00
statistics Fix mvdistinct and dependencies size calculations 2019-04-21 20:23:34 +02:00
storage Make BufFileCreateTemp() ensure that temp tablespaces are set up. 2019-05-18 13:51:16 -04:00
tcop Fix potential catalog corruption with temporary identity columns 2019-04-29 08:49:03 +02:00
tsearch Fix more strcmp() calls using boolean-like comparisons for result checks 2019-04-12 10:16:49 +09:00
utils Fix declarations of couple jsonpath functions 2019-05-19 07:45:42 +03:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Remove PARTIAL_LINKING build mode. 2018-03-30 17:33:04 -07:00
Makefile Get rid of jsonpath_gram.h and jsonpath_scanner.h 2019-03-20 11:13:34 +03:00
nls.mk Translation updates 2018-06-25 12:37:18 +02:00