Search Postgresql Archives

Re: Re: Where is the char and varchar length in pg_catalog for function input variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 
> How does postgres figure this out to throw the error msg?
> 
> 
> select test1('this is way way longer than 10 characters','this is way way
way
> way way way way way way way way way longer than 20 characters')
> 
> ERROR:  value too long for type character(10)
> CONTEXT:  SQL statement "insert into test_table values ($1, $2)"
> PL/pgSQL function "test1" line 3 at SQL statement
> 
> ********** Error **********
> 
> ERROR: value too long for type character(10)
> 

When it goes to execute:

INSERT INTO test_table ('this is way way ...', 'this is way way way...')

The char(10) type definition for test_table.column1 is too short to hold the
supplied value (stored in $1 in the function) and throws an error.

The length of $1 and $2 inside the function are however long the input
values are because they ignore the length specifier on the function call
types.

If you want to guarantee that the INSERT will work you would need to write:

INSERT INTO test_table VALUES ( $1::char(10), $2::varchar(20) )

This tells PostgreSQL to truncate the supplied value at whatever specified
length is noted; the same as writing substring($1, 1, 10)::char or
substring($1, 1, 20)::varchar though whether "char" and "varchar" differ in
their behavior in this respect I do not know.  It is generally not
recommended to use "char"

David J.







-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux