On line 214 of mod/chat/lib.php, there is the statement:
...COALESCE(($keepdays), 999999) * 24 * 3600)";...
The coalesce select the first non-null, so if $keepdays only returns null, it selects 999999 and the number of days. This get multiplied out to seconds (86399913600), but that number is bigger than an integer in postgres, so it throws and error and that sql dies.
A postgres int is 32 bit signed (+-2147483647), so the closest you can get it 2147483647/3600/24 = 24855.134803 ~= 24855.
So I think that 999999 should be replaced by 24855 on that line, to keep the int from overflowing.