Search code examples
postgresqlpartitioning

Unable to partition varchar field storing numeric value with leading 0 in postgres


I am using Postgres 13.9

Below is the table structure:-

create table t5
(
id varchar(3), --using id as varchar since I want to retain leading 0. 
fname varchar
)partition by range (id);

create table t5_a partition of t5 for values from (001) to (010);
create table t5_b partition of t5 for values from (010) to (020);
create table t5_c partition of t5 for values from (020) to (030);

If I try to insert below

insert into t5 values('007', 'Tom');

It throws below error:-

Error: no partition of relation "t5" found for row

Solution

  • I added single quote in ranges and I am able to insert data now:-

    create table t5_a partition of t5 for values from ('001') to ('010');
    create table t5_b partition of t5 for values from ('010') to ('020');
    create table t5_c partition of t5 for values from ('020') to ('030');
    
    insert into t5 values('007', 'Tom');
    

    However, one thing to note is, if you try to insert data by passing only 7 instead of '007', it will throw an error.