Re: text field constraint advice

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Dale Sykora <dalen(at)czexan(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: text field constraint advice
Date: 2005-01-26 07:08:36
Message-ID: 20050126070836.GA24793@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sibtay Abbas 2005-01-26 07:40:24 working with multidimensional arrays in plpgsql
Previous Message Oleg Bartunov 2005-01-26 06:53:47 Re: visualizing B-tree index coverage