Boolean type storage format

Started by Alexander Gataricover 13 years ago5 messagesgeneral
Jump to latest
#1Alexander Gataric
gataric@usa.net

What is the data physically stored as for boolean type? I know that it is one byte but is it char, int, or something else?

Sent from my smartphone

#2Raghavendra
raghavendra.rao@enterprisedb.com
In reply to: Alexander Gataric (#1)
Re: Boolean type storage format

On Wed, Oct 31, 2012 at 8:52 PM, Alexander Gataric <gataric@usa.net> wrote:

What is the data physically stored as for boolean type? I know that it is
one byte but is it char, int, or something else?

False represented by zero bytes and True by 1 byte with value 1.

---
Regards,
Raghavendra
EnterpriseDB Corporation
Blog: http://raghavt.blogspot.com/

#3Mike Christensen
mike@kitchenpc.com
In reply to: Raghavendra (#2)
Re: Boolean type storage format

It would also matter what columns were next to it, correct?

For example, if you had 4 bools in a row, that could also be 1 byte..

On Wed, Oct 31, 2012 at 11:08 AM, Raghavendra <
raghavendra.rao@enterprisedb.com> wrote:

Show quoted text

On Wed, Oct 31, 2012 at 8:52 PM, Alexander Gataric <gataric@usa.net>wrote:

What is the data physically stored as for boolean type? I know that it is
one byte but is it char, int, or something else?

False represented by zero bytes and True by 1 byte with value 1.

---
Regards,
Raghavendra
EnterpriseDB Corporation
Blog: http://raghavt.blogspot.com/

#4Craig Ringer
craig@2ndquadrant.com
In reply to: Mike Christensen (#3)
Re: Boolean type storage format

On 11/01/2012 02:25 AM, Mike Christensen wrote:

It would also matter what columns were next to it, correct?

It doesn't look like PostgreSQL packs booleans. It still matters what's
next to it because of the alignment requirements of other data types,
but you still have a minimum of one byte per boolean. See:

regress=> SELECT pg_column_size( ROW('t'::boolean) );
pg_column_size
----------------
25
(1 row)

regress=> SELECT pg_column_size( ROW('t'::boolean, 't'::boolean) );
pg_column_size
----------------
26
(1 row)

regress=> SELECT pg_column_size( ROW('t'::boolean, 't'::boolean,
'f'::boolean, 't'::boolean) );
pg_column_size
----------------
28
(1 row)

--
Craig Ringer

#5Marti Raudsepp
marti@juffo.org
In reply to: Raghavendra (#2)
Re: Boolean type storage format

On Wed, Oct 31, 2012 at 8:08 PM, Raghavendra
<raghavendra.rao@enterprisedb.com> wrote:

False represented by zero bytes and True by 1 byte with value 1.

This is not true AFAIK. Both boolean TRUE and FALSE values require 1 byte.

A NULL value is zero bytes (though it still consumes 1 bit in the null
bitmap). This is true for all types, not just boolean.

Regards,
Marti