"char"
这是 PostgreSQL 中的单字节类型,范围为 -128,127。来自the docs,
"char" 类型(注意引号)与char(1) 的不同之处在于它只使用一个字节的存储空间。它在系统目录内部用作简单的枚举类型。
您可以将其偏向 [-128,127],方法是在写入数据库之前从 [0-255] 范围内的任何输入中减去 128,然后在从数据库中读取时将其添加回输出中。
-- works
SELECT (-128)::"char", 127::"char";
-- generates out of range
SELECT (-128)::"char";
SELECT 128::"char";
-- Shifts to unsigned range.
-- If you're going to be using "char"
-- review the results of this query!
SELECT
x::int AS "inputUnsigned",
chr(x) AS "extendedASCII",
-- this is the "char" types representation for that input.
signed::"char" AS "charRepresentation",
signed AS "inputUnsignedToSigned",
signed+128 AS "inputUnsignedToSignedToUnsigned"
FROM generate_series(1,255) AS gs(x)
-- Here we map the input in the range of [0,255] to [-128,127]
CROSS JOIN LATERAL ( VALUES (x::int-128) )
AS v(signed);
输出的小摘录
inputUnsigned | extendedASCII | charRepresentation | inputUnsignedToSigned | inputUnsignedToSignedToUnsigned
---------------+---------------+--------------------+-----------------------+---------------------------------
....
190 | ¾ | > | 62 | 190
191 | ¿ | ? | 63 | 191
192 | À | @ | 64 | 192
193 | Á | A | 65 | 193
194 | Â | B | 66 | 194
195 | Ã | C | 67 | 195
196 | Ä | D | 68 | 196
...
我们使用generate_series(1,255) 只是因为chr(0) 抛出,因为您无法生成或输出ASCII NUL(PostgreSQL 使用cstrings)
pguint分机
Pguint 是提供两种单字节表示的扩展,