cash_out(PG_FUNCTION_ARGS)
 {
        Cash            value = PG_GETARG_CASH(0);
+       uint64          uvalue;
        char       *result;
        char            buf[128];
        char       *bufptr;
 
        if (value < 0)
        {
-               /* make the amount positive for digit-reconstruction loop */
-               value = -value;
                /* set up formatting data */
                signsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
                sign_posn = lconvert->n_sign_posn;
                sep_by_space = lconvert->p_sep_by_space;
        }
 
+       /* make the amount positive for digit-reconstruction loop */
+       uvalue = pg_abs_s64(value);
+
        /* we build the digits+decimal-point+sep string right-to-left in buf[] */
        bufptr = buf + sizeof(buf) - 1;
        *bufptr = '\0';
                        memcpy(bufptr, ssymbol, strlen(ssymbol));
                }
 
-               *(--bufptr) = ((uint64) value % 10) + '0';
-               value = ((uint64) value) / 10;
+               *(--bufptr) = (uvalue % 10) + '0';
+               uvalue = uvalue / 10;
                digit_pos--;
-       } while (value || digit_pos >= 0);
+       } while (uvalue || digit_pos >= 0);
 
        /*----------
         * Now, attach currency symbol and sign symbol in the correct order.
 
        if (val < 0)
        {
                var->sign = NUMERIC_NEG;
-               uval = -val;
+               uval = pg_abs_s64(val);
        }
        else
        {
         * Now we can proceed with the multiplications.
         */
        neg = (exp < 0);
-       mask = abs(exp);
+       mask = pg_abs_s32(exp);
 
        init_var(&base_prod);
        set_var_from_var(base, &base_prod);
 
 #include <limits.h>
 #include <ctype.h>
 
+#include "common/int.h"
 #include "port/pg_bitutils.h"
 #include "utils/builtins.h"
 
        uint16          tmp = 0;
        bool            neg = false;
        unsigned char digit;
+       int16           result;
 
        /*
         * The majority of cases are likely to be base-10 digits without any
 
        if (neg)
        {
-               /* check the negative equivalent will fit without overflowing */
-               if (unlikely(tmp > (uint16) (-(PG_INT16_MIN + 1)) + 1))
+               if (unlikely(pg_neg_u16_overflow(tmp, &result)))
                        goto out_of_range;
-               return -((int16) tmp);
+               return result;
        }
 
        if (unlikely(tmp > PG_INT16_MAX))
 
        if (neg)
        {
-               /* check the negative equivalent will fit without overflowing */
-               if (tmp > (uint16) (-(PG_INT16_MIN + 1)) + 1)
+               if (unlikely(pg_neg_u16_overflow(tmp, &result)))
                        goto out_of_range;
-               return -((int16) tmp);
+               return result;
        }
 
        if (tmp > PG_INT16_MAX)
        uint32          tmp = 0;
        bool            neg = false;
        unsigned char digit;
+       int32           result;
 
        /*
         * The majority of cases are likely to be base-10 digits without any
 
        if (neg)
        {
-               /* check the negative equivalent will fit without overflowing */
-               if (unlikely(tmp > (uint32) (-(PG_INT32_MIN + 1)) + 1))
+               if (unlikely(pg_neg_u32_overflow(tmp, &result)))
                        goto out_of_range;
-               return -((int32) tmp);
+               return result;
        }
 
        if (unlikely(tmp > PG_INT32_MAX))
 
        if (neg)
        {
-               /* check the negative equivalent will fit without overflowing */
-               if (tmp > (uint32) (-(PG_INT32_MIN + 1)) + 1)
+               if (unlikely(pg_neg_u32_overflow(tmp, &result)))
                        goto out_of_range;
-               return -((int32) tmp);
+               return result;
        }
 
        if (tmp > PG_INT32_MAX)
        uint64          tmp = 0;
        bool            neg = false;
        unsigned char digit;
+       int64           result;
 
        /*
         * The majority of cases are likely to be base-10 digits without any
 
        if (neg)
        {
-               /* check the negative equivalent will fit without overflowing */
-               if (unlikely(tmp > (uint64) (-(PG_INT64_MIN + 1)) + 1))
+               if (unlikely(pg_neg_u64_overflow(tmp, &result)))
                        goto out_of_range;
-               return -((int64) tmp);
+               return result;
        }
 
        if (unlikely(tmp > PG_INT64_MAX))
 
        if (neg)
        {
-               /* check the negative equivalent will fit without overflowing */
-               if (tmp > (uint64) (-(PG_INT64_MIN + 1)) + 1)
+               if (unlikely(pg_neg_u64_overflow(tmp, &result)))
                        goto out_of_range;
-               return -((int64) tmp);
+               return result;
        }
 
        if (tmp > PG_INT64_MAX)
 
        time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
                        * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
 
-       result = date * USECS_PER_DAY + time;
-       /* check for major overflow */
-       if ((result - time) / USECS_PER_DAY != date)
-               ereport(ERROR,
-                               (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
-                                errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
-                                               year, month, day,
-                                               hour, min, sec)));
-
-       /* check for just-barely overflow (okay except time-of-day wraps) */
-       /* caution: we want to allow 1999-12-31 24:00:00 */
-       if ((result < 0 && date > 0) ||
-               (result > 0 && date < -1))
+       if (unlikely(pg_mul_s64_overflow(date, USECS_PER_DAY, &result) ||
+                                pg_add_s64_overflow(result, time, &result)))
                ereport(ERROR,
                                (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                                 errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
        date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
        time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
 
-       *result = date * USECS_PER_DAY + time;
-       /* check for major overflow */
-       if ((*result - time) / USECS_PER_DAY != date)
-       {
-               *result = 0;                    /* keep compiler quiet */
-               return -1;
-       }
-       /* check for just-barely overflow (okay except time-of-day wraps) */
-       /* caution: we want to allow 1999-12-31 24:00:00 */
-       if ((*result < 0 && date > 0) ||
-               (*result > 0 && date < -1))
+       if (unlikely(pg_mul_s64_overflow(date, USECS_PER_DAY, result) ||
+                                pg_add_s64_overflow(*result, time, result)))
        {
                *result = 0;                    /* keep compiler quiet */
                return -1;
 
 
 /*---------
  * The following guidelines apply to all the overflow routines:
- * - If a + b overflows, return true, otherwise store the result of a + b
- * into *result. The content of *result is implementation defined in case of
- * overflow.
- * - If a - b overflows, return true, otherwise store the result of a - b
- * into *result. The content of *result is implementation defined in case of
- * overflow.
- * - If a * b overflows, return true, otherwise store the result of a * b
- * into *result. The content of *result is implementation defined in case of
+ *
+ * If the result overflows, return true, otherwise store the result into
+ * *result.  The content of *result is implementation defined in case of
  * overflow.
+ *
+ *  bool pg_add_*_overflow(a, b, *result)
+ *
+ *    Calculate a + b
+ *
+ *  bool pg_sub_*_overflow(a, b, *result)
+ *
+ *    Calculate a - b
+ *
+ *  bool pg_mul_*_overflow(a, b, *result)
+ *
+ *    Calculate a * b
+ *
+ *  bool pg_neg_*_overflow(a, *result)
+ *
+ *    Calculate -a
+ *
+ *
+ * In addition, this file contains:
+ *
+ *  <unsigned int type> pg_abs_*(<signed int type> a)
+ *
+ *    Calculate absolute value of a.  Unlike the standard library abs()
+ *    and labs() functions, the return type is unsigned, so the operation
+ *    cannot overflow.
  *---------
  */
 
 #endif
 }
 
+static inline uint16
+pg_abs_s16(int16 a)
+{
+       /*
+        * This first widens the argument from int16 to int32 for use with abs().
+        * The result is then narrowed from int32 to uint16.  This prevents any
+        * possibility of overflow.
+        */
+       return (uint16) abs((int32) a);
+}
+
 /*
  * INT32
  */
 #endif
 }
 
+static inline uint32
+pg_abs_s32(int32 a)
+{
+       /*
+        * This first widens the argument from int32 to int64 for use with
+        * i64abs().  The result is then narrowed from int64 to uint32.  This
+        * prevents any possibility of overflow.
+        */
+       return (uint32) i64abs((int64) a);
+}
+
 /*
  * INT64
  */
 #endif
 }
 
+static inline uint64
+pg_abs_s64(int64 a)
+{
+       if (unlikely(a == PG_INT64_MIN))
+               return (uint64) PG_INT64_MAX + 1;
+       return (uint64) i64abs(a);
+}
+
 /*------------------------------------------------------------------------
  * Overflow routines for unsigned integers
  *------------------------------------------------------------------------
 #endif
 }
 
+static inline bool
+pg_neg_u16_overflow(uint16 a, int16 *result)
+{
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
+       return __builtin_sub_overflow(0, a, result);
+#else
+       int32           res = -((int32) a);
+
+       if (unlikely(res < PG_INT16_MIN))
+       {
+               *result = 0x5EED;               /* to avoid spurious warnings */
+               return true;
+       }
+       *result = res;
+       return false;
+#endif
+}
+
 /*
  * INT32
  */
 #endif
 }
 
+static inline bool
+pg_neg_u32_overflow(uint32 a, int32 *result)
+{
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
+       return __builtin_sub_overflow(0, a, result);
+#else
+       int64           res = -((int64) a);
+
+       if (unlikely(res < PG_INT32_MIN))
+       {
+               *result = 0x5EED;               /* to avoid spurious warnings */
+               return true;
+       }
+       *result = res;
+       return false;
+#endif
+}
+
 /*
  * UINT64
  */
 #endif
 }
 
+static inline bool
+pg_neg_u64_overflow(uint64 a, int64 *result)
+{
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
+       return __builtin_sub_overflow(0, a, result);
+#elif defined(HAVE_INT128)
+       int128          res = -((int128) a);
+
+       if (unlikely(res < PG_INT64_MIN))
+       {
+               *result = 0x5EED;               /* to avoid spurious warnings */
+               return true;
+       }
+       *result = res;
+       return false;
+#else
+       if (unlikely(a > (uint64) PG_INT64_MAX + 1))
+       {
+               *result = 0x5EED;               /* to avoid spurious warnings */
+               return true;
+       }
+       if (unlikely(a == (uint64) PG_INT64_MAX + 1))
+               *result = PG_INT64_MIN;
+       else
+               *result = -((int64) a);
+       return false;
+#endif
+}
+
 /*------------------------------------------------------------------------
  *
  * Comparison routines for integer types.
 
 #error -ffast-math is known to break this code
 #endif
 
+#include "common/int.h"
 #include "dt.h"
 #include "pgtypes_date.h"
 #include "pgtypes_timestamp.h"
 
        dDate = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
        time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
-       *result = (dDate * USECS_PER_DAY) + time;
-       /* check for major overflow */
-       if ((*result - time) / USECS_PER_DAY != dDate)
-               return -1;
-       /* check for just-barely overflow (okay except time-of-day wraps) */
-       /* caution: we want to allow 1999-12-31 24:00:00 */
-       if ((*result < 0 && dDate > 0) ||
-               (*result > 0 && dDate < -1))
+       if (unlikely(pg_mul_s64_overflow(dDate, USECS_PER_DAY, result) ||
+                                pg_add_s64_overflow(*result, time, result)))
                return -1;
        if (tzp != NULL)
                *result = dt2local(*result, -(*tzp));
 
 
 select age(timestamp '-infinity', timestamp '-infinity');
 ERROR:  interval out of range
+-- test timestamp near POSTGRES_EPOCH_JDATE
+select timestamp '1999-12-31 24:00:00';
+        timestamp         
+--------------------------
+ Sat Jan 01 00:00:00 2000
+(1 row)
+
+select make_timestamp(1999, 12, 31, 24, 0, 0);
+      make_timestamp      
+--------------------------
+ Sat Jan 01 00:00:00 2000
+(1 row)
+
 
 
 SELECT age(timestamptz '-infinity', timestamptz '-infinity');
 ERROR:  interval out of range
+-- test timestamp near POSTGRES_EPOCH_JDATE
+select timestamptz '1999-12-31 24:00:00';
+         timestamptz          
+------------------------------
+ Sat Jan 01 00:00:00 2000 PST
+(1 row)
+
+select make_timestamptz(1999, 12, 31, 24, 0, 0);
+       make_timestamptz       
+------------------------------
+ Sat Jan 01 00:00:00 2000 PST
+(1 row)
+
 
 select age(timestamp 'infinity', timestamp '-infinity');
 select age(timestamp '-infinity', timestamp 'infinity');
 select age(timestamp '-infinity', timestamp '-infinity');
+
+-- test timestamp near POSTGRES_EPOCH_JDATE
+select timestamp '1999-12-31 24:00:00';
+select make_timestamp(1999, 12, 31, 24, 0, 0);
 
 SELECT age(timestamptz 'infinity', timestamptz '-infinity');
 SELECT age(timestamptz '-infinity', timestamptz 'infinity');
 SELECT age(timestamptz '-infinity', timestamptz '-infinity');
+
+-- test timestamp near POSTGRES_EPOCH_JDATE
+select timestamptz '1999-12-31 24:00:00';
+select make_timestamptz(1999, 12, 31, 24, 0, 0);