I'm using PostgreSQL 9.6 on Linux. I got an error when I do a test on chr() function.

postgres=# select chr(1199111);
ERROR:  requested character too large for encoding: 1199111
postgres=# select chr(55296);
ERROR:  requested character not valid for encoding: 55296
postgres=# select chr(100000);
(1 row)

Here, as you can see with value 100k it worked (didn't raise error), however others didn't. I'm curious about that. Could someone please explain to me why ?

I attach test script

str text ;
for i in 1..1200000
select chr(i) into str;
exception when others then raise notice '=> i: %     => str: %', i , str ;
#exit; -- you can uncomment this star key
end ;
end loop;
$$ ;


postgres=# show server_encoding;
(1 row)
  • Please add the result of show server_encoding to your question – Daniel Vérité Feb 17 '17 at 10:52
  • @DanielVérité sorry for my late response, added server_encoding – Luan Huynh Feb 19 '17 at 15:39

Because it didn't raise an error, doesn't mean it worked. None of these work, but only one of them is so absurd as to make chr() explode.

  • 100000 (186A0) maps to the Supplementary Multilingual Plane, but there is nothing there (yet).
  • 55296 (U+D800) maps to the Basic Multilingual Plane, but nothing is there (yet).
  • 1199111 (U+124C07) is not even valid, it maps to a plane so high that Unicode doesn't even support it.

Unicode only supports 1,111,998: 17 planes × 65,536 characters per plane - 2048 surrogates - 66 noncharacters. See this answer for more information. 1.11 Million code points, is far less than 1.19 Million code points.

  • Very good answer. Only one remark: while it is possible that in the future a character is assigned to 100000, which is in the Ideographic Script Area of the SMP, it is against the standard to assign a character to 55296, which is “permanently reserved” as a surrogate. – Dario Feb 19 '17 at 20:04
  • thanks, en.wikipedia.org/wiki/… – Luan Huynh Feb 20 '17 at 3:27
  • @LuanHuynh consider marking this answer as accepted if it answered your question. – Evan Carroll Feb 20 '17 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.