Incorrect type in pg_cast causes Postmaster to core dump
I was setting up an implicit type cast for an application that was inserting a boolean into a numeric field, but I used the wrong return type from the function... and this caused the Postmaster to core dump:
[postgres@efm1 ~]$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[postgres@efm1 ~]$ pg_ctl -c start
[postgres@efm1 ~]$ < 2017-04-21 11:10:35.287 BST > LOG: redirecting log output to logging collector process
< 2017-04-21 11:10:35.287 BST > HINT: Future log output will appear in directory "pg_log".
/var/lib/pgsql/9.6/data/core.27553: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from 'postgres: postgres postgres', real uid: 1002, effective uid: 1002, real gid: 1002, effective gid: 1002, execfn: '/usr/pgsql-9.6/bin/postgres', platform: 'x86_64'
I realise that my types were wrong (function returned an "integer", and should have returned a "numeric"), and I've fixed that and now it's working fine... but how can a type mismatch cause the entire cluster to crash?
Greg Clough <[hidden email]> writes:
> I realise that my types were wrong (function returned an "integer", and
> should have returned a "numeric"), and I've fixed that and now it's working
> fine... but how can a type mismatch cause the entire cluster to crash?
Trivially. You told the system it should use "bool_to_num (boolean)
RETURNS integer" to coerce boolean to numeric, so it expected what
came out of the function to be numeric --- which is a pass-by-reference
type, so the Datum value should be a pointer, which something or other
would've tried to dereference. The actual value was just an integer
"1" though, which is not a valid pointer, so you got a SIGSEGV.
There are no training wheels for Postgres superusers. If you put
broken entries into the system catalogs using superuser powers,
you can break the database arbitrarily badly.
As Michael noted, the type compatibility check that's needed here
exists in CREATE CAST, not at runtime. CREATE CAST would have done
other essential housekeeping as well, such as creating a pg_depend
entry to prevent the function from being dropped out from under