Skip to content

Commit 0a9ae18

Browse files
committed
Fix crash when using COLLATE in partition bound expressions
Attempting to use a COLLATE clause with a type that it not collatable in a partition bound expression could crash the server. This commit fixes the code by adding more checks similar to what is done when computing index or partition attributes by making sure that there is a collation iff the type is collatable. Backpatch down to 12, as 7c079d7 introduced this problem. Reported-by: Alexander Lakhin Author: Dmitry Dolgov Discussion: https://postgr.es/m/16325-809194cf742313ab@postgresql.org Backpatch-through: 12
1 parent 35d1eef commit 0a9ae18

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,6 +4037,30 @@ transformPartitionBoundValue(ParseState *pstate, Node *val,
40374037
{
40384038
Oid exprCollOid = exprCollation(value);
40394039

4040+
/*
4041+
* Check we have a collation iff it is a collatable type. The only
4042+
* expected failures here are (1) COLLATE applied to a noncollatable
4043+
* type, or (2) partition bound expression had an unresolved
4044+
* collation. But we might as well code this to be a complete
4045+
* consistency check.
4046+
*/
4047+
if (type_is_collatable(colType))
4048+
{
4049+
if (!OidIsValid(exprCollOid))
4050+
ereport(ERROR,
4051+
(errcode(ERRCODE_INDETERMINATE_COLLATION),
4052+
errmsg("could not determine which collation to use for partition bound expression"),
4053+
errhint("Use the COLLATE clause to set the collation explicitly.")));
4054+
}
4055+
else
4056+
{
4057+
if (OidIsValid(exprCollOid))
4058+
ereport(ERROR,
4059+
(errcode(ERRCODE_DATATYPE_MISMATCH),
4060+
errmsg("collations are not supported by type %s",
4061+
format_type_be(colType))));
4062+
}
4063+
40404064
if (OidIsValid(exprCollOid) &&
40414065
exprCollOid != DEFAULT_COLLATION_OID &&
40424066
exprCollOid != partCollation)

src/test/regress/expected/create_table.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (genera
603603
ERROR: set-returning functions are not allowed in partition bound
604604
LINE 1: ...expr_fail PARTITION OF list_parted FOR VALUES IN (generate_s...
605605
^
606+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ('1' collate "POSIX");
607+
ERROR: collations are not supported by type integer
608+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((1+1) collate "POSIX");
609+
ERROR: collations are not supported by type integer
610+
LINE 1: ...ail PARTITION OF list_parted FOR VALUES IN ((1+1) collate "P...
611+
^
606612
-- syntax does not allow empty list of values for list partitions
607613
CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES IN ();
608614
ERROR: syntax error at or near ")"

src/test/regress/sql/create_table.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(so
516516
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(1));
517517
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((select 1));
518518
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (generate_series(4, 6));
519+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ('1' collate "POSIX");
520+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((1+1) collate "POSIX");
519521

520522
-- syntax does not allow empty list of values for list partitions
521523
CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES IN ();

0 commit comments

Comments
 (0)