↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

Wiki / 数据类型

bit

fixed-length bit string

当前阅读 PG 18·选择有来源记录的版本

此版本暂无所选语言的定义,以下显示原始英文内容。

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
aliases
未知
coverage
source inventory; exact declared input types for operator classes
manual documentation
dedicated family chapter
manual path
datatype-bit.html
ranges
未知
signature
bit [ ( n ) ]

版本定义 PG 18

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).

比较版本

完整来源事实

casts

map[castcontext:i castfunc:0 castmethod:b castsource:bit casttarget:varbit], map[castcontext:i castfunc:0 castmethod:b castsource:varbit casttarget:bit], map[castcontext:e castfunc:bit(int8,int4) castmethod:f castsource:int8 casttarget:bit], map[castcontext:e castfunc:bit(int4,int4) castmethod:f castsource:int4 casttarget:bit], map[castcontext:e castfunc:int8(bit) castmethod:f castsource:bit casttarget:int8], map[castcontext:e castfunc:int4(bit) castmethod:f castsource:bit casttarget:int4], map[castcontext:i castfunc:bit(bit,int4,bool) castmethod:f castsource:bit casttarget:bit]

catalog

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

operator classes

map[opcdefault:t opcfamily:btree/bit_ops opcintype:bit opckeytype:0 opcmethod:btree opcname:bit_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:t opcfamily:brin/bit_minmax_ops opcintype:bit opckeytype:bit opcmethod:brin opcname:bit_minmax_ops opcnamespace:pg_catalog opcowner:POSTGRES]

operators

map[descr:equal oid:1784 oprcanhash:f oprcanmerge:t oprcode:biteq oprcom:=(bit,bit) oprjoin:eqjoinsel oprkind:b oprleft:bit oprname:= oprnamespace:pg_catalog oprnegate:<>(bit,bit) oprowner:POSTGRES oprrest:eqsel oprresult:bool oprright:bit], map[descr:not equal oid:1785 oprcanhash:f oprcanmerge:f oprcode:bitne oprcom:<>(bit,bit) oprjoin:neqjoinsel oprkind:b oprleft:bit oprname:<> oprnamespace:pg_catalog oprnegate:=(bit,bit) oprowner:POSTGRES oprrest:neqsel oprresult:bool oprright:bit], map[descr:less than oid:1786 oprcanhash:f oprcanmerge:f oprcode:bitlt oprcom:>(bit,bit) oprjoin:scalarltjoinsel oprkind:b oprleft:bit oprname:< oprnamespace:pg_catalog oprnegate:>=(bit,bit) oprowner:POSTGRES oprrest:scalarltsel oprresult:bool oprright:bit], map[descr:greater than oid:1787 oprcanhash:f oprcanmerge:f oprcode:bitgt oprcom:<(bit,bit) oprjoin:scalargtjoinsel oprkind:b oprleft:bit oprname:> oprnamespace:pg_catalog oprnegate:<=(bit,bit) oprowner:POSTGRES oprrest:scalargtsel oprresult:bool oprright:bit], map[descr:less than or equal oid:1788 oprcanhash:f oprcanmerge:f oprcode:bitle oprcom:>=(bit,bit) oprjoin:scalarlejoinsel oprkind:b oprleft:bit oprname:<= oprnamespace:pg_catalog oprnegate:>(bit,bit) oprowner:POSTGRES oprrest:scalarlesel oprresult:bool oprright:bit], map[descr:greater than or equal oid:1789 oprcanhash:f oprcanmerge:f oprcode:bitge oprcom:<=(bit,bit) oprjoin:scalargejoinsel oprkind:b oprleft:bit oprname:>= oprnamespace:pg_catalog oprnegate:<(bit,bit) oprowner:POSTGRES oprrest:scalargesel oprresult:bool oprright:bit], map[descr:bitwise and oid:1791 oprcanhash:f oprcanmerge:f oprcode:bitand oprcom:&(bit,bit) oprjoin:- oprkind:b oprleft:bit oprname:& oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:bit oprright:bit], map[descr:bitwise or oid:1792 oprcanhash:f oprcanmerge:f oprcode:bitor oprcom:|(bit,bit) oprjoin:- oprkind:b oprleft:bit oprname:| oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:bit oprright:bit], map[descr:bitwise exclusive or oid:1793 oprcanhash:f oprcanmerge:f oprcode:bitxor oprcom:#(bit,bit) oprjoin:- oprkind:b oprleft:bit oprname:# oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:bit oprright:bit], map[descr:bitwise not oid:1794 oprcanhash:f oprcanmerge:f oprcode:bitnot oprcom:0 oprjoin:- oprkind:l oprleft:0 oprname:~ oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:bit oprright:bit], map[descr:bitwise shift left oid:1795 oprcanhash:f oprcanmerge:f oprcode:bitshiftleft oprcom:0 oprjoin:- oprkind:b oprleft:bit oprname:<< oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:bit oprright:int4], map[descr:bitwise shift right oid:1796 oprcanhash:f oprcanmerge:f oprcode:bitshiftright oprcom:0 oprjoin:- oprkind:b oprleft:bit oprname:>> oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:bit oprright:int4]

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f

正文语言: en · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f