Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit21fdfd0

Browse files
committed
Fix edge case leading to agg transitions skipping ExecAggTransReparent() calls.
The code checking whether an aggregate transition value needs to bereparented into the current context has always only compared thetransition return value with the previous transition value by datum,i.e. without regard for NULLness. This normally works, because whenthe transition function returns NULL (via fcinfo->isnull), it'llreturn a value that won't be the same as its input value.But there's no hard requirement that that's the case. And it turnsout, it's possible to hit this case (see discussion or reproducers),leading to a non-null transition value not being reparented, followedby a crash caused by that.Instead of adding another comparison of NULLness, instead haveExecAggTransReparent() ensure that pergroup->transValue ends up as 0when the new transition value is NULL. That avoids having to add anadditional branch to the much more common cases of the transitionfunction returning the old transition value (which is a pointer inthis case), and when the new value is different, but not NULL.In branches since69c3936, also deduplicate the reparenting codebetween the expression evaluation based transitions, and the path forordered aggregates.Reported-By: Teodor Sigaev, Nikita GlukhovAuthor: Andres FreundDiscussion:https://postgr.es/m/bd34e930-cfec-ea9b-3827-a8bc50891393@sigaev.ruBackpatch: 9.4-, this issue has existed since at least 7.4
1 parentef8e6b2 commit21fdfd0

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

‎src/backend/executor/execExprInterp.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,15 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
17021702
* anything. Also, if transfn returned a pointer to a R/W
17031703
* expanded object that is already a child of the aggcontext,
17041704
* assume we can adopt that value without copying it.
1705+
*
1706+
* It's safe to compare newVal with pergroup->transValue without
1707+
* regard for either being NULL, because ExecAggTransReparent()
1708+
* takes care to set transValue to 0 when NULL. Otherwise we could
1709+
* end up accidentally not reparenting, when the transValue has
1710+
* the same numerical value as newValue, despite being NULL. This
1711+
* is a somewhat hot path, making it undesirable to instead solve
1712+
* this with another branch for the common case of the transition
1713+
* function returning its (modified) input argument.
17051714
*/
17061715
if (DatumGetPointer(newVal)!=DatumGetPointer(pergroup->transValue))
17071716
newVal=ExecAggTransReparent(aggstate,pertrans,
@@ -4087,6 +4096,8 @@ ExecAggTransReparent(AggState *aggstate, AggStatePerTrans pertrans,
40874096
DatumnewValue,boolnewValueIsNull,
40884097
DatumoldValue,boololdValueIsNull)
40894098
{
4099+
Assert(newValue!=oldValue);
4100+
40904101
if (!newValueIsNull)
40914102
{
40924103
MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory);
@@ -4100,6 +4111,16 @@ ExecAggTransReparent(AggState *aggstate, AggStatePerTrans pertrans,
41004111
pertrans->transtypeByVal,
41014112
pertrans->transtypeLen);
41024113
}
4114+
else
4115+
{
4116+
/*
4117+
* Ensure that AggStatePerGroup->transValue ends up being 0, so
4118+
* callers can safely compare newValue/oldValue without having to
4119+
* check their respective nullness.
4120+
*/
4121+
newValue= (Datum)0;
4122+
}
4123+
41034124
if (!oldValueIsNull)
41044125
{
41054126
if (DatumIsReadWriteExpandedObject(oldValue,

‎src/backend/executor/nodeAgg.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
#include"catalog/pg_aggregate.h"
222222
#include"catalog/pg_proc.h"
223223
#include"catalog/pg_type.h"
224+
#include"executor/execExpr.h"
224225
#include"executor/executor.h"
225226
#include"executor/nodeAgg.h"
226227
#include"miscadmin.h"
@@ -626,33 +627,22 @@ advance_transition_function(AggState *aggstate,
626627
* first input, we don't need to do anything. Also, if transfn returned a
627628
* pointer to a R/W expanded object that is already a child of the
628629
* aggcontext, assume we can adopt that value without copying it.
630+
*
631+
* It's safe to compare newVal with pergroup->transValue without
632+
* regard for either being NULL, because ExecAggTransReparent()
633+
* takes care to set transValue to 0 when NULL. Otherwise we could
634+
* end up accidentally not reparenting, when the transValue has
635+
* the same numerical value as newValue, despite being NULL. This
636+
* is a somewhat hot path, making it undesirable to instead solve
637+
* this with another branch for the common case of the transition
638+
* function returning its (modified) input argument.
629639
*/
630640
if (!pertrans->transtypeByVal&&
631641
DatumGetPointer(newVal)!=DatumGetPointer(pergroupstate->transValue))
632-
{
633-
if (!fcinfo->isnull)
634-
{
635-
MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory);
636-
if (DatumIsReadWriteExpandedObject(newVal,
637-
false,
638-
pertrans->transtypeLen)&&
639-
MemoryContextGetParent(DatumGetEOHP(newVal)->eoh_context)==CurrentMemoryContext)
640-
/* do nothing */ ;
641-
else
642-
newVal=datumCopy(newVal,
643-
pertrans->transtypeByVal,
644-
pertrans->transtypeLen);
645-
}
646-
if (!pergroupstate->transValueIsNull)
647-
{
648-
if (DatumIsReadWriteExpandedObject(pergroupstate->transValue,
649-
false,
650-
pertrans->transtypeLen))
651-
DeleteExpandedObject(pergroupstate->transValue);
652-
else
653-
pfree(DatumGetPointer(pergroupstate->transValue));
654-
}
655-
}
642+
newVal=ExecAggTransReparent(aggstate,pertrans,
643+
newVal,fcinfo->isnull,
644+
pergroupstate->transValue,
645+
pergroupstate->transValueIsNull);
656646

657647
pergroupstate->transValue=newVal;
658648
pergroupstate->transValueIsNull=fcinfo->isnull;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp