Re: text field constraint advice - Mailing list pgsql-general

From Michael Fuhr
Subject Re: text field constraint advice
Date
Msg-id 20050126070836.GA24793@winnie.fuhr.org
Whole thread Raw
In response to text field constraint advice  (Dale Sykora <dalen@czexan.net>)
Responses Re: text field constraint advice  (Jeff Davis <jdavis-pgsql@empires.org>)
List pgsql-general
On Wed, Jan 26, 2005 at 12:27:17AM -0600, Dale Sykora wrote:

>     I would like to use a text field in a table and limit the size to
> reduce the chance of denial-of-service/buffer overflow/etc.  I assume I
> can define table fields similar to the following
> "field_name text check (len(field) < 160)" although my syntax is
> probably wrong.

You could use varchar(n) instead of text with a check constraint.

> Is checking text length considered a good idea?

That depends on the application and the trustworthiness of the data
source.  If you know that values should never exceed a certain
length and you want to prevent obviously bad values from being
inserted, then enforcing a length limit makes sense.

> If so, what would be a reasonable limit?  I was thinking about
> 10 * average_field_char_length (if avg value is 16 char, set
> limit to 160 char).

Again, that depends on the application.  If you're storing product
part numbers then most of them will probably fall close to the
average length, so allowing ten times the average length would be
unnecessary.  On the other hand, if you're storing product descriptions
then you might need to allow for greater variation.  Use whatever
makes sense for the type of data you're storing.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

pgsql-general by date:

Previous
From: Oleg Bartunov
Date:
Subject: Re: visualizing B-tree index coverage
Next
From: Sibtay Abbas
Date:
Subject: working with multidimensional arrays in plpgsql