Re: Efficiency question: VARCHAR with empty string vs NULL - Mailing list pgsql-general

From Tom Lane
Subject Re: Efficiency question: VARCHAR with empty string vs NULL
Date
Msg-id 13510.1022266454@sss.pgh.pa.us
Whole thread Raw
In response to Efficiency question: VARCHAR with empty string vs NULL  (Doug Fields <dfields-pg-general@pexicom.com>)
Responses Re: Efficiency question: VARCHAR with empty string
List pgsql-general
Doug Fields <dfields-pg-general@pexicom.com> writes:
> Is there a performance difference with VARCHAR elements of value NULL and
> zero-length string?

These are not semantically equivalent (if you think they are, you've
been using Oracle too long).  You will almost certainly regret it
if you try recoding your app to make them equivalent.  An example:

regression=# select 'foo'::varchar || ''::varchar;
 ?column?
----------
 foo
(1 row)

regression=# select 'foo'::varchar || null::varchar;
 ?column?
----------

(1 row)


But to answer your question, NULLs might save a couple bytes if there
are multiple NULLs per row.  I think that replacing a single empty
varchar with a NULL would net out to no change (you lose the 4-byte
varchar length word, but have to store a null-values bitmap instead),
depending on alignment issues and how many columns there are in the
table.

            regards, tom lane

pgsql-general by date:

Previous
From: Neil Conway
Date:
Subject: Re: Altering existing table to be WITHOUT OIDs
Next
From: Neil Conway
Date:
Subject: Re: Case Insensitive Data Type