Обсуждение: PK/FK impacts using text data type

Поиск
Список
Период
Сортировка

PK/FK impacts using text data type

От
"Ferrell, Denise CTR NSWCDD, Z11"
Дата:
Good Morning,

Using Linux/PostgreSQL 9.3.

We have been discussing whether or not to change all of our varchar (character varying) data types to text for ease of
usein procedures/functions/triggers... 

Are there any impacts on keys by using text data type vs. character varying or character varying(n)?

Thank you in advance,
Denise Ferrell


Re: PK/FK impacts using text data type

От
Albe Laurenz
Дата:
Denise Ferrell wrote:
> We have been discussing whether or not to change all of our varchar (character varying)
> data types to text for ease of use in procedures/functions/triggers...
>
> Are there any impacts on keys by using text data type vs. character varying or character varying(n)?

The only useful thing about varchar is that it imposes a length constraint on a database field.

Values used for indexing have a length limit, so if you change an indexed column to text
somebody could try to insert values that will cause errors because the limit has been exceeded.

Also you application code cannot rely on a certain size limit any more.

If you are ok with that, go ahead and use text.

Yours,
Laurenz Albe

Re: PK/FK impacts using text data type

От
Tom Lane
Дата:
Albe Laurenz <laurenz.albe@wien.gv.at> writes:
> The only useful thing about varchar is that it imposes a length constraint on a database field.

> Values used for indexing have a length limit, so if you change an indexed column to text
> somebody could try to insert values that will cause errors because the limit has been exceeded.

I'm not particularly buying that argument.  If you insert an overlength
field value, you will get an error either way; it's just spelled
differently.  Also, using varchar(N) isn't a particularly efficient way to
guard the btree length limit, because N is measured in characters but the
btree length limit is in bytes.  You'd have to use a very conservatively
small N to be absolutely sure you don't get a btree error, and that would
result in throwing some errors that actually weren't necessary at all.

> Also you application code cannot rely on a certain size limit any more.

This is a valid argument, though again only to the extent that you need a
size limit measured in characters not bytes.

            regards, tom lane


Re: PK/FK impacts using text data type

От
David G Johnston
Дата:
Albe Laurenz *EXTERN* wrote
> Denise Ferrell wrote:
>> We have been discussing whether or not to change all of our varchar
>> (character varying)
>> data types to text for ease of use in procedures/functions/triggers...
>>
>> Are there any impacts on keys by using text data type vs. character
>> varying or character varying(n)?
>
> The only useful thing about varchar is that it imposes a length constraint
> on a database field.
>
> Values used for indexing have a length limit, so if you change an indexed
> column to text
> somebody could try to insert values that will cause errors because the
> limit has been exceeded.
>
> Also you application code cannot rely on a certain size limit any more.
>
> If you are ok with that, go ahead and use text.
>
> Yours,
> Laurenz Albe

Appropriate check constraints can handle the majority of the needed cases.
In theory, domains would be helpful here too but I'm not totally conversant
on them at the moment.

David J.



--
View this message in context: http://postgresql.nabble.com/PK-FK-impacts-using-text-data-type-tp5833407p5833533.html
Sent from the PostgreSQL - admin mailing list archive at Nabble.com.