↑↓ select ↵ open ⌫ change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

Wiki / Data Types / Bit strings

bit

fixed-length bit string

Reading PostgreSQL 18.6.

PG10–20 core source inventory. Casts and operator classes show explicitly declared relationships; absent rows do not rule out coercions or indexing through other mechanisms.

Definition

fixed-length bit string

bit [ ( n ) ]
Catalog name
pg_catalog.bit
Type OID
1560
Type kind
Base type
Declared length
Variable length (varlena)
Storage strategy
extended
Input function
bit_in
Output function
bit_out

English manual

Read the complete manual section

Read the source definition

8.10. Bit String Types

Bit strings are strings of 1's and 0's. They can be used to store or visualize bit masks. There are two SQL bit types: bit(n) and bit varying(n), where n is a positive integer.

bit type data must match the length n exactly; it is an error to attempt to store shorter or longer bit strings. bit varying data is of variable length up to the maximum length n; longer strings will be rejected. Writing bit without a length is equivalent to bit(1), while bit varying without a length specification means unlimited length.

Note

If one explicitly casts a bit-string value to bit(n), it will be truncated or zero-padded on the right to be exactly n bits, without raising an error. Similarly, if one explicitly casts a bit-string value to bit varying(n), it will be truncated on the right if it is more than n bits.

Refer to Section 4.1.2.5 for information about the syntax of bit string constants. Bit-logical operators and string manipulation functions are available; see Section 9.6.

Example 8.3. Using the Bit String Types

CREATE TABLE test (a BIT(3), b BIT VARYING(5));
INSERT INTO test VALUES (B'101', B'00');
INSERT INTO test VALUES (B'10', B'101');

ERROR:  bit string length 2 does not match type bit(3)

INSERT INTO test VALUES (B'10'::bit(3), B'101');
SELECT * FROM test;

  a  |  b
-----+-----
 101 | 00
 100 | 101


A bit string value requires 1 byte for each group of 8 bits, plus 5 or 8 bytes overhead depending on the length of the string (but long values may be compressed or moved out-of-line, as explained in Section 8.3 for character strings).

Catalog attributes

Source bootstrap values for this build. See pg_type for field meanings. Header defaults are included; build-dependent constants remain symbolic. This is not a live-server measurement.

oid
1560
descr
fixed-length bit string
typacl
_null_
typlen
-1
typelem
0
typname
bit
typsend
bit_send
typtype
b
typalign
i
typarray
0
typbyval
f
typdelim
','
typinput
bit_in
typmodin
bittypmodin
typndims
0
typowner
POSTGRES
typrelid
0
typmodout
bittypmodout
typoutput
bit_out
typtypmod
-1
typanalyze
-
typdefault
_null_
typnotnull
f
typreceive
bit_recv
typstorage
x
typbasetype
0
typcategory
V
typcollation
0
typisdefined
t
typnamespace
pg_catalog
typsubscript
-
typdefaultbin
_null_
array_type_oid
1561
typispreferred
f
array_type_name
_bit

Catalog casts 7

Explicit pg_cast records involving this type. PostgreSQL also supports coercions outside pg_cast; an absent row does not prove that a conversion is impossible.

FromToContextMethodFunction
bitvarbitImplicitBinary compatible0
varbitbitImplicitBinary compatible0
int8bitExplicitFunctionbit(int8,int4)
int4bitExplicitFunctionbit(int4,int4)
bitint8ExplicitFunctionint8(bit)
bitint4ExplicitFunctionint4(bit)
bitbitImplicitFunctionbit(bit,int4,bool)

Operator overloads 12

Each operand signature is a separate overload. Catalog implementation functions and result types belong to the same source build.

OperatorLeft operandRight operandResultMeaningImplementation
=bitbitboolequalbiteq
<>bitbitboolnot equalbitne
<bitbitboolless thanbitlt
>bitbitboolgreater thanbitgt
<=bitbitboolless than or equalbitle
>=bitbitboolgreater than or equalbitge
&bitbitbitbitwise andbitand
|bitbitbitbitwise orbitor
#bitbitbitbitwise exclusive orbitxor
~None (unary)bitbitbitwise notbitnot
<<bitint4bitbitwise shift leftbitshiftleft
>>bitint4bitbitwise shift rightbitshiftright

Operator classes 2

Operator classes whose declared input type matches this type. Polymorphic classes, casts and expression indexes can provide additional index paths; this list is not an exhaustive yes/no index-support test.

ClassIndex methodInput typeFamilyDefaultStorage type
bit_opsbtreebitbtree/bit_opsYesSame as input
bit_minmax_opsbrinbitbrin/bit_minmax_opsYesbit

Version comparison

PostgreSQL 17.11 → 18.6. Source build identifiers and prose are excluded from attribute changes.

No catalog or structured attribute changes between these samples.

The documentation also differs between these builds; inspect the versioned manual definitions.

Documentation and source

Build
18.6 · https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
Fingerprint
555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f

Related entries

All Data Types · Download this version as JSON · The first recorded sample does not establish when a type was introduced.