Skip to content

Commit c018786

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/[email protected] Backpatch-through: 12
1 parent d025cf8 commit c018786

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Diff for: src/backend/parser/parse_utilcmd.c

+24
Original file line numberDiff line numberDiff line change
@@ -4081,6 +4081,30 @@ transformPartitionBoundValue(ParseState *pstate, Node *val,
40814081
{
40824082
Oid exprCollOid = exprCollation(value);
40834083

4084+
/*
4085+
* Check we have a collation iff it is a collatable type. The only
4086+
* expected failures here are (1) COLLATE applied to a noncollatable
4087+
* type, or (2) partition bound expression had an unresolved
4088+
* collation. But we might as well code this to be a complete
4089+
* consistency check.
4090+
*/
4091+
if (type_is_collatable(colType))
4092+
{
4093+
if (!OidIsValid(exprCollOid))
4094+
ereport(ERROR,
4095+
(errcode(ERRCODE_INDETERMINATE_COLLATION),
4096+
errmsg("could not determine which collation to use for partition bound expression"),
4097+
errhint("Use the COLLATE clause to set the collation explicitly.")));
4098+
}
4099+
else
4100+
{
4101+
if (OidIsValid(exprCollOid))
4102+
ereport(ERROR,
4103+
(errcode(ERRCODE_DATATYPE_MISMATCH),
4104+
errmsg("collations are not supported by type %s",
4105+
format_type_be(colType))));
4106+
}
4107+
40844108
if (OidIsValid(exprCollOid) &&
40854109
exprCollOid != DEFAULT_COLLATION_OID &&
40864110
exprCollOid != partCollation)

Diff for: src/test/regress/expected/create_table.out

+6
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (genera
652652
ERROR: set-returning functions are not allowed in partition bound
653653
LINE 1: ...expr_fail PARTITION OF list_parted FOR VALUES IN (generate_s...
654654
^
655+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ('1' collate "POSIX");
656+
ERROR: collations are not supported by type integer
657+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((1+1) collate "POSIX");
658+
ERROR: collations are not supported by type integer
659+
LINE 1: ...ail PARTITION OF list_parted FOR VALUES IN ((1+1) collate "P...
660+
^
655661
-- syntax does not allow empty list of values for list partitions
656662
CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES IN ();
657663
ERROR: syntax error at or near ")"

Diff for: src/test/regress/sql/create_table.sql

+2
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(so
549549
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(1));
550550
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((select 1));
551551
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (generate_series(4, 6));
552+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ('1' collate "POSIX");
553+
CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((1+1) collate "POSIX");
552554

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

0 commit comments

Comments
 (0)