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

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

Wiki / 数据类型

boolean

logical Boolean (true/false)

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

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

Catalog name
pg_catalog.bool
Type OID
16
Type kind
Base type
Declared length
1 byte
Storage strategy
plain
Input function
boolin
Output function
boolout
Documented declaration
boolean
Storage Size
1 byte
Manual description
state of true or false
aliases
bool
coverage
source inventory; exact declared input types for operator classes
manual documentation
dedicated family chapter
manual path
datatype-boolean.html
ranges
未知
signature
boolean

版本定义 PG 18

8.6. Boolean Type

PostgreSQL provides the standard SQL type boolean; see Table 8.19. The boolean type can have several states: “true”, “false”, and a third state, “unknown”, which is represented by the SQL null value.

Table 8.19. Boolean Data Type

Name Storage Size Description
boolean 1 byte state of true or false

Boolean constants can be represented in SQL queries by the SQL key words TRUE, FALSE, and NULL.

The datatype input function for type boolean accepts these string representations for the “true” state:

true
yes
on
1

and these representations for the “false” state:

false
no
off
0

Unique prefixes of these strings are also accepted, for example t or n. Leading or trailing whitespace is ignored, and case does not matter.

The datatype output function for type boolean always emits either t or f, as shown in Example 8.2.

Example 8.2. Using the boolean Type

CREATE TABLE test1 (a boolean, b text);
INSERT INTO test1 VALUES (TRUE, 'sic est');
INSERT INTO test1 VALUES (FALSE, 'non est');
SELECT * FROM test1;
 a |    b
---+---------
 t | sic est
 f | non est

SELECT * FROM test1 WHERE a;
 a |    b
---+---------
 t | sic est

The key words TRUE and FALSE are the preferred (SQL-compliant) method for writing Boolean constants in SQL queries. But you can also use the string representations by following the generic string-literal constant syntax described in Section 4.1.2.7, for example 'yes'::boolean.

Note that the parser automatically understands that TRUE and FALSE are of type boolean, but this is not so for NULL because that can have any type. So in some contexts you might have to cast NULL to boolean explicitly, for example NULL::boolean. Conversely, the cast can be omitted from a string-literal Boolean value in contexts where the parser can deduce that the literal must be of type boolean.

比较版本

完整来源事实

casts

map[castcontext:e castfunc:bool(int4) castmethod:f castsource:int4 casttarget:bool], map[castcontext:e castfunc:int4(bool) castmethod:f castsource:bool casttarget:int4], map[castcontext:a castfunc:text(bool) castmethod:f castsource:bool casttarget:text], map[castcontext:a castfunc:text(bool) castmethod:f castsource:bool casttarget:varchar], map[castcontext:a castfunc:text(bool) castmethod:f castsource:bool casttarget:bpchar], map[castcontext:e castfunc:bool(jsonb) castmethod:f castsource:jsonb casttarget:bool]

catalog

{"array_type_name":"_bool","array_type_oid":"1000","descr":"boolean, format 't'/'f'","oid":"16","typacl":"_null_","typalign":"c","typanalyze":"-","typarray":"0","typbasetype":"0","typbyval":"t","typcategory":"B","typcollation":"0","typdefault":"_null_","typdefaultbin":"_null_","typdelim":"','","typelem":"0","typinput":"boolin","typisdefined":"t","typispreferred":"t","typlen":"1","typmodin":"-","typmodout":"-","typname":"bool","typnamespace":"pg_catalog","typndims":"0","typnotnull":"f","typoutput":"boolout","typowner":"POSTGRES","typreceive":"boolrecv","typrelid":"0","typsend":"boolsend","typstorage":"p","typsubscript":"-","typtype":"b","typtypmod":"-1"}

operator classes

map[opcdefault:t opcfamily:btree/bool_ops opcintype:bool opckeytype:0 opcmethod:btree opcname:bool_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:t opcfamily:hash/bool_ops opcintype:bool opckeytype:0 opcmethod:hash opcname:bool_ops opcnamespace:pg_catalog opcowner:POSTGRES]

operators

map[descr:less than oid:58 oprcanhash:f oprcanmerge:f oprcode:boollt oprcom:>(bool,bool) oprjoin:scalarltjoinsel oprkind:b oprleft:bool oprname:< oprnamespace:pg_catalog oprnegate:>=(bool,bool) oprowner:POSTGRES oprrest:scalarltsel oprresult:bool oprright:bool], map[descr:greater than oid:59 oprcanhash:f oprcanmerge:f oprcode:boolgt oprcom:<(bool,bool) oprjoin:scalargtjoinsel oprkind:b oprleft:bool oprname:> oprnamespace:pg_catalog oprnegate:<=(bool,bool) oprowner:POSTGRES oprrest:scalargtsel oprresult:bool oprright:bool], map[descr:not equal oid:85 oid_symbol:BooleanNotEqualOperator oprcanhash:f oprcanmerge:f oprcode:boolne oprcom:<>(bool,bool) oprjoin:neqjoinsel oprkind:b oprleft:bool oprname:<> oprnamespace:pg_catalog oprnegate:=(bool,bool) oprowner:POSTGRES oprrest:neqsel oprresult:bool oprright:bool], map[descr:equal oid:91 oid_symbol:BooleanEqualOperator oprcanhash:t oprcanmerge:t oprcode:booleq oprcom:=(bool,bool) oprjoin:eqjoinsel oprkind:b oprleft:bool oprname:= oprnamespace:pg_catalog oprnegate:<>(bool,bool) oprowner:POSTGRES oprrest:eqsel oprresult:bool oprright:bool], map[descr:less than or equal oid:1694 oprcanhash:f oprcanmerge:f oprcode:boolle oprcom:>=(bool,bool) oprjoin:scalarlejoinsel oprkind:b oprleft:bool oprname:<= oprnamespace:pg_catalog oprnegate:>(bool,bool) oprowner:POSTGRES oprrest:scalarlesel oprresult:bool oprright:bool], map[descr:greater than or equal oid:1695 oprcanhash:f oprcanmerge:f oprcode:boolge oprcom:<=(bool,bool) oprjoin:scalargejoinsel oprkind:b oprleft:bool oprname:>= oprnamespace:pg_catalog oprnegate:<(bool,bool) oprowner:POSTGRES oprrest:scalargesel oprresult:bool oprright:bool]

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f

正文语言: en · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f