4 -- Per SQL standard, CHAR means character(1), that is a varlena type
5 -- with a constraint restricting it to one character (not byte)
6 SELECT char 'c' = char 'c' AS true;
13 -- Build a table for testing
14 -- (This temporarily hides the table created in test_setup.sql)
16 CREATE TEMP TABLE CHAR_TBL(f1 char);
17 INSERT INTO CHAR_TBL (f1) VALUES ('a');
18 INSERT INTO CHAR_TBL (f1) VALUES ('A');
19 -- any of the following three input formats are acceptable
20 INSERT INTO CHAR_TBL (f1) VALUES ('1');
21 INSERT INTO CHAR_TBL (f1) VALUES (2);
22 INSERT INTO CHAR_TBL (f1) VALUES ('3');
24 INSERT INTO CHAR_TBL (f1) VALUES ('');
25 -- try char's of greater than 1 length
26 INSERT INTO CHAR_TBL (f1) VALUES ('cd');
27 ERROR: value too long for type character(1)
28 INSERT INTO CHAR_TBL (f1) VALUES ('c ');
29 SELECT * FROM CHAR_TBL;
106 -- Now test longer arrays of char
108 -- This char_tbl was already created and filled in test_setup.sql.
109 -- Here we just try to insert bad values.
111 INSERT INTO CHAR_TBL (f1) VALUES ('abcde');
112 ERROR: value too long for type character(4)
113 SELECT * FROM CHAR_TBL;
122 -- Also try it with non-error-throwing API
123 SELECT pg_input_is_valid('abcd ', 'char(4)');
129 SELECT pg_input_is_valid('abcde', 'char(4)');
135 SELECT * FROM pg_input_error_info('abcde', 'char(4)');
136 message | detail | hint | sql_error_code
137 --------------------------------------+--------+------+----------------
138 value too long for type character(4) | | | 22001
142 -- Also test "char", which is an ad-hoc one-byte type. It can only
143 -- really store ASCII characters, but we allow high-bit-set characters
144 -- to be accessed via bytea-like escapes.
152 SELECT '\101'::"char";
158 SELECT '\377'::"char";
164 SELECT 'a'::"char"::text;
170 SELECT '\377'::"char"::text;
176 SELECT '\000'::"char"::text;
182 SELECT 'a'::text::"char";
188 SELECT '\377'::text::"char";
194 SELECT ''::text::"char";