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

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "'jam3'" <jamorton3(at)gmail(dot)com>,<pgsql-general(at)postgresql(dot)org>, "David Johnston" <polobo(at)yahoo(dot)com>
Subject: Re: Re: Where is the char and varchar length in pg_catalog for function input variables
Date: 2012-09-05 21:41:53
Message-ID: 504780D10200002500049EE5@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"David Johnston" <polobo(at)yahoo(dot)com> wrote:

> 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) )

Note that this will quietly cut off the tail end of the supplied
data, so it should only be used when that is desirable. It is
generally better to throw an error than to lose data.

> whether "char" and "varchar" differ in their behavior in this
> respect I do not know.

Per the SQL standard, they both throw an error on attempts to assign
an oversized value, but allow truncation through explicit casts.

> It is generally not recommended to use "char"

Right. It is supported because the standard specifies it and its
behavior, but the semantics of char(n) are weird and the
performance, in PostgreSQL, is generally worse for char(n) than
varchar(n) or text.

-Kevin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Edson Richter 2012-09-05 22:47:23 Re: Moving several databases into one database with several schemas
Previous Message David Johnston 2012-09-05 21:27:44 Re: Re: Where is the char and varchar length in pg_catalog for function input variables