satisfies_hash_partition crash

Поиск
Список
Период
Сортировка
Искать

satisfies_hash_partition crash

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
Hi,

This test case crashes the server for me:

CREATE TABLE hp (a int) PARTITION BY HASH (a);
SELECT satisfies_hash_partition('hp'::regclass, 1, 0, VARIADIC NULL::int[]);

I'm inclined to mostly blame my commit
f3b0897a1213f46b4d3a99a7f8ef3a4b32e03572, which fixed related problems
but overlooked this one. I think the fix should be simple, but since
that commit is from 2017, it will need to be back-patched all the way.

--
Robert Haas
EDB: http://www.enterprisedb.com


Re: satisfies_hash_partition crash

От:
Ewan Young <kdbase.hack@gmail.com>
Дата:
Вложения:
On Thu, Jul 2, 2026 at 9:22 AM Tender Wang  wrote:
>
> Robert Haas  于2026年7月2日周四 03:11写道:
> >
> > Hi,
> >
> > This test case crashes the server for me:
> >
> > CREATE TABLE hp (a int) PARTITION BY HASH (a);
> > SELECT satisfies_hash_partition('hp'::regclass, 1, 0, VARIADIC NULL::int[]);
> >
> > I'm inclined to mostly blame my commit
> > f3b0897a1213f46b4d3a99a7f8ef3a4b32e03572, which fixed related problems
> > but overlooked this one. I think the fix should be simple, but since
> > that commit is from 2017, it will need to be back-patched all the way.
>
> Adding PG_ARGISNULL(3) to the beginning of the if() seems workable.

Thanks for looking at this!

I tried the PG_ARGISNULL(3) approach and ran into one subtlety I
thought worth mentioning:
it seems like the check can't go into the top-level if()
unconditionally. In a non-variadic call, a NULL
fourth argument is a valid NULL value for the first partition key, and
it looks like satisfies_hash_partition()
needs to keep returning the normal result there — that's how the
hash-partition CHECK constraint routes
rows with NULL key columns. Making it an unconditional false seems to
make hash partitions reject NULL rows.

I've attached a patch with a small regression test in case any of it
is useful, but I'm happy to leave the actual fix
to you.

Thanks again!

> And the comments need to be adjusted.
>
>
> --
> Thanks,
> Tender Wang
>
>


-- 
Regards,
Ewan Young

Re: satisfies_hash_partition crash

От:
Tender Wang <tndrwang@gmail.com>
Дата:
Ewan Young  于2026年7月2日周四 10:08写道:
> I tried the PG_ARGISNULL(3) approach and ran into one subtlety I
> thought worth mentioning:
> it seems like the check can't go into the top-level if()
> unconditionally. In a non-variadic call, a NULL
> fourth argument is a valid NULL value for the first partition key, and
> it looks like satisfies_hash_partition()
> needs to keep returning the normal result there — that's how the
> hash-partition CHECK constraint routes
> rows with NULL key columns. Making it an unconditional false seems to
> make hash partitions reject NULL rows.

Yes, you're right.

> I've attached a patch with a small regression test in case any of it
> is useful, but I'm happy to leave the actual fix
> to you.
How about this:
diff --git a/src/backend/partitioning/partbounds.c
b/src/backend/partitioning/partbounds.c
index 6fb150a8763..53c409cf2b1 100644
--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -4863,7 +4863,13 @@ satisfies_hash_partition(PG_FUNCTION_ARGS)
                }
                else
                {
-                       ArrayType  *variadic_array = PG_GETARG_ARRAYTYPE_P(3);
+                       ArrayType  *variadic_array;
+
+                       /* Return false if the array is NULL. */
+                       if (PG_ARGISNULL(3))
+                               PG_RETURN_BOOL(false);
+
+                       variadic_array = PG_GETARG_ARRAYTYPE_P(3);

                        /* allocate space for our cache -- just one
FmgrInfo in this case */
                        fcinfo->flinfo->fn_extra =

This way can avoid two get_fn_expr_variadic() calls if the last
argument is not combined into an array.
Thoughts?


-- 
Thanks,
Tender Wang


Re: satisfies_hash_partition crash

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
Вложения:
On Thu, Jul 2, 2026 at 2:17 AM Tender Wang  wrote:
> Personally,  I prefer a local bool variable near the top and reuse it.

I prefer to more thoroughly confine this fix to the variadic code
path, since I don't think we really expect that to be exercised in
practice. So here's my proposal.

-- 
Robert Haas
EDB: http://www.enterprisedb.com

Re: satisfies_hash_partition crash

От:
Tender Wang <tndrwang@gmail.com>
Дата:
Robert Haas  于2026年7月3日周五 01:54写道:
>
> On Thu, Jul 2, 2026 at 2:17 AM Tender Wang  wrote:
> > Personally,  I prefer a local bool variable near the top and reuse it.
>
> I prefer to more thoroughly confine this fix to the variadic code
> path, since I don't think we really expect that to be exercised in
> practice. So here's my proposal.
WFM
+1



-- 
Thanks,
Tender Wang


Re: satisfies_hash_partition crash

От:
Ewan Young <kdbase.hack@gmail.com>
Дата:
On Fri, Jul 3, 2026 at 1:54 AM Robert Haas  wrote:
>
> On Thu, Jul 2, 2026 at 2:17 AM Tender Wang  wrote:
> > Personally,  I prefer a local bool variable near the top and reuse it.
>
> I prefer to more thoroughly confine this fix to the variadic code
> path, since I don't think we really expect that to be exercised in
> practice. So here's my proposal.

v2 looks good to me — I tested it and confirmed that it works. Thanks
for picking this up.

>
> --
> Robert Haas
> EDB: http://www.enterprisedb.com

-- 
Regards,
Ewan Young


Re: satisfies_hash_partition crash

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Thu, Jul 2, 2026 at 10:07 PM Ewan Young  wrote:
> v2 looks good to me — I tested it and confirmed that it works. Thanks
> for picking this up.

OK, thanks for the reviews, committed and back-patched.

-- 
Robert Haas
EDB: http://www.enterprisedb.com


FAQ