postgres.git / summary / log / commit / refs

commit    cdde886d36b5a4d7ad9e1d02596f7fa1c8c129e3
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Tue Mar 26 01:09:14 2019 +0000
First released in: 12.0

    Fix crash when using partition bound expressions
    
    Since 7c079d7, partition bounds are able to use generalized expression
    syntax when processed, treating "minvalue" and "maxvalue" as specific
    cases as they get passed down for transformation as a column references.
    
    The checks for infinite bounds in range expressions have been lax
    though, causing crashes when trying to use column reference names with
    more than one field.  Here is an example causing a crash:
    CREATE TABLE list_parted (a int) PARTITION BY LIST (a);
    CREATE TABLE part_list_crash PARTITION OF list_parted
      FOR VALUES IN (somename.somename);
    
    Note that the creation of the second relation should fail as partition
    bounds cannot have column references in their expressions, so when
    finding an expression which does not match the expected infinite bounds,
    then this commit lets the generic transformation machinery check after
    it.  The error message generated in this case references as well a
    missing RTE, which is confusing.  This problem will be treated
    separately as it impacts as well default expressions for some time, and
    for now only the cases where a crash can happen are fixed.
    
    While on it, extend the set of regression tests in place for list
    partition bounds and add an extra set for range partition bounds.
    
    Reported-by: Alexander Lakhin
    Author: Michael Paquier
    Reviewed-by: Amit Langote
    Discussion: https://postgr.es/m/15668-0377b1981aa1a393@postgresql.org


src/backend/parser/parse_utilcmd.c | 16 +++++++-- src/test/regress/expected/create_table.out | 55 +++++++++++++++++++++++++++++- src/test/regress/sql/create_table.sql | 23 ++++++++++++- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index a37d1f18bea..b4ec96d6d6d 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -3746,12 +3746,24 @@ transformPartitionRangeBounds(ParseState *pstate, List *blist, ColumnRef *cref = (ColumnRef *) expr; char *cname = NULL; + /* + * There should be a single field named either "minvalue" or + * "maxvalue". + */ if (list_length(cref->fields) == 1 && IsA(linitial(cref->fields), String)) cname = strVal(linitial(cref->fields)); - Assert(cname != NULL); - if (strcmp("minvalue", cname) == 0) + if (cname == NULL) + { + /* + * ColumnRef is not in the desired single-field-name form. + * For consistency between all partition strategies, let the + * expression transformation report any errors rather than + * doing it ourselves. + */ + } + else if (strcmp("minvalue", cname) == 0) { prd = makeNode(PartitionRangeDatum); prd->kind = PARTITION_RANGE_DATUM_MINVALUE; diff --git a/src/test/regress/expected/create_table.out b/src/test/regress/expected/create_table.out index d51e547278e..1cf21cc26f0 100644 --- a/src/test/regress/expected/create_table.out +++ b/src/test/regress/expected/create_table.out @@ -489,11 +489,15 @@ Partitions: part_null FOR VALUES IN (NULL), part_p2 FOR VALUES IN (2), part_p3 FOR VALUES IN (3) --- forbidden expressions for partition bound +-- forbidden expressions for partition bound with list partitioned table CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (somename); ERROR: column "somename" does not exist LINE 1: ...expr_fail PARTITION OF list_parted FOR VALUES IN (somename); ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (somename.somename); +ERROR: missing FROM-clause entry for table "somename" +LINE 1: ...expr_fail PARTITION OF list_parted FOR VALUES IN (somename.s... + ^ CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (a); ERROR: cannot use column references in partition bound expression LINE 1: ..._bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (a); @@ -502,6 +506,14 @@ CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(a) ERROR: aggregate functions are not allowed in partition bound LINE 1: ...s_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(a)); ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(somename)); +ERROR: column "somename" does not exist +LINE 1: ..._fail PARTITION OF list_parted FOR VALUES IN (sum(somename))... + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(1)); +ERROR: aggregate functions are not allowed in partition bound +LINE 1: ...s_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(1)); + ^ CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((select 1)); ERROR: cannot use subquery in partition bound LINE 1: ...expr_fail PARTITION OF list_parted FOR VALUES IN ((select 1)... @@ -558,6 +570,47 @@ DROP TABLE bigintp; CREATE TABLE range_parted ( a date ) PARTITION BY RANGE (a); +-- forbidden expressions for partition bounds with range partitioned table +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (somename) TO ('2019-01-01'); +ERROR: column "somename" does not exist +LINE 2: FOR VALUES FROM (somename) TO ('2019-01-01'); + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (somename.somename) TO ('2019-01-01'); +ERROR: missing FROM-clause entry for table "somename" +LINE 2: FOR VALUES FROM (somename.somename) TO ('2019-01-01'); + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (a) TO ('2019-01-01'); +ERROR: cannot use column references in partition bound expression +LINE 2: FOR VALUES FROM (a) TO ('2019-01-01'); + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (max(a)) TO ('2019-01-01'); +ERROR: aggregate functions are not allowed in partition bound +LINE 2: FOR VALUES FROM (max(a)) TO ('2019-01-01'); + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (max(somename)) TO ('2019-01-01'); +ERROR: column "somename" does not exist +LINE 2: FOR VALUES FROM (max(somename)) TO ('2019-01-01'); + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (max('2019-02-01'::date)) TO ('2019-01-01'); +ERROR: aggregate functions are not allowed in partition bound +LINE 2: FOR VALUES FROM (max('2019-02-01'::date)) TO ('2019-01-01'... + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM ((select 1)) TO ('2019-01-01'); +ERROR: cannot use subquery in partition bound +LINE 2: FOR VALUES FROM ((select 1)) TO ('2019-01-01'); + ^ +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (generate_series(1, 3)) TO ('2019-01-01'); +ERROR: set-returning functions are not allowed in partition bound +LINE 2: FOR VALUES FROM (generate_series(1, 3)) TO ('2019-01-01'); + ^ -- trying to specify list for range partitioned table CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES IN ('a'); ERROR: invalid bound specification for a range partition diff --git a/src/test/regress/sql/create_table.sql b/src/test/regress/sql/create_table.sql index 4091c19cf03..ba935488ba8 100644 --- a/src/test/regress/sql/create_table.sql +++ b/src/test/regress/sql/create_table.sql @@ -450,10 +450,13 @@ CREATE TABLE part_p3 PARTITION OF list_parted FOR VALUES IN ((2+1)); CREATE TABLE part_null PARTITION OF list_parted FOR VALUES IN (null); \d+ list_parted --- forbidden expressions for partition bound +-- forbidden expressions for partition bound with list partitioned table CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (somename); +CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (somename.somename); CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (a); CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(a)); +CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(somename)); +CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(1)); CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((select 1)); CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (generate_series(4, 6)); @@ -497,6 +500,24 @@ CREATE TABLE range_parted ( a date ) PARTITION BY RANGE (a); +-- forbidden expressions for partition bounds with range partitioned table +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (somename) TO ('2019-01-01'); +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (somename.somename) TO ('2019-01-01'); +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (a) TO ('2019-01-01'); +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (max(a)) TO ('2019-01-01'); +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (max(somename)) TO ('2019-01-01'); +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (max('2019-02-01'::date)) TO ('2019-01-01'); +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM ((select 1)) TO ('2019-01-01'); +CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted + FOR VALUES FROM (generate_series(1, 3)) TO ('2019-01-01'); + -- trying to specify list for range partitioned table CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES IN ('a'); -- trying to specify modulus and remainder for range partitioned table [parent: 2e3da03e9ee4]