Forum OpenACS Q&A: text versus varchar data types

Collapse
Posted by James Harris on
Is there any reason (efficiency, speed, other) to use varchar(n)
instead of text when creating new tables in Postgres?

Both types take up the same amount of storage space ((4+n) bytes) so
it would appear that using text would be better in nearly all
circumstances unless there is a speed or other tradeoff.

The only reason I can think of is for SQL92 compatability, but
porting to another database is very low on my list of priorities so
this isn't an issue.

Collapse
Posted by James Harris on
Also, is there a limit on n when using varchar(n)?
Collapse
Posted by Andrei Popov on
An emperical test:
$ psql _tmp
_tmp=# create table t (f varchar(1000000));
CREATE
_tmp=# \q

$ python
Python 2.1 (#1, Apr 17 2001, 09:45:01)
[GCC 2.95.3-2 (cygwin special)] on cygwin_nt-4.01
Type "copyright", "credits" or "license" for more information.
>>>a='A'*100000
>>>from pg import DB
>>> cnx=DB('_tmp','localhost')
>>> qstr = 'insert into t values (\'' + '123' +'\');'
>>> cnx.query(qstr)
2826034
>>> a = 'A'*65535
>>> qstr = 'insert into t values (\'' + '123' +'\');'
>>> cnx.query(qstr)
2826060
>>> a = 'A'*1000000
>>> qstr = 'insert into t values (\'' + '123' +'\');'
>>> cnx.query(qstr)
2826061
>>> ^D

$ psql _tmp
_tmp=# select length(f) from t;
 length
---------
   65535
 1000000
  100000
(3 rows)

_tmp=# select substring(f,64000,1) from t;
 substring
-----------
 A
 A
 A
(3 rows)

_tmp=# drop table t;
DROP
_tmp=# \q
$
Looks like no limit, eh?
Collapse
Posted by Andrei Popov on
If you see double-slashed things in my post above, these are because I could not submit a post otherwise...