diff --git a/configure b/configure
index d5ace62..60b0d90 100755
--- a/configure
+++ b/configure
@@ -732,6 +732,7 @@ CPP
 BITCODE_CXXFLAGS
 BITCODE_CFLAGS
 CFLAGS_VECTOR
+PERMIT_DECLARATION_AFTER_STATEMENT
 LLVM_BINPATH
 LLVM_CXXFLAGS
 LLVM_CFLAGS
@@ -5252,6 +5253,7 @@ if test "$GCC" = yes -a "$ICC" = no; then
   CFLAGS="-Wall -Wmissing-prototypes -Wpointer-arith"
   CXXFLAGS="-Wall -Wpointer-arith"
   # These work in some but not all gcc versions
+  save_CFLAGS=$CFLAGS
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CC} supports -Wdeclaration-after-statement, for CFLAGS" >&5
 $as_echo_n "checking whether ${CC} supports -Wdeclaration-after-statement, for CFLAGS... " >&6; }
@@ -5292,7 +5294,13 @@ if test x"$pgac_cv_prog_CC_cflags__Wdeclaration_after_statement" = x"yes"; then
 fi
 
 
-  # -Wdeclaration-after-statement isn't applicable for C++
+  # -Wdeclaration-after-statement isn't applicable for C++.  Specific C files
+  # disable it, so AC_SUBST the negative form.
+  PERMIT_DECLARATION_AFTER_STATEMENT=
+  if test x"$save_CFLAGS" != "$CFLAGS"; then
+    PERMIT_DECLARATION_AFTER_STATEMENT=-Wno-declaration-after-statement
+  fi
+
   # Really don't want VLAs to be used in our dialect of C
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CC} supports -Werror=vla, for CFLAGS" >&5
diff --git a/configure.in b/configure.in
index 4efb912..c1edc8c 100644
--- a/configure.in
+++ b/configure.in
@@ -476,8 +476,15 @@ if test "$GCC" = yes -a "$ICC" = no; then
   CFLAGS="-Wall -Wmissing-prototypes -Wpointer-arith"
   CXXFLAGS="-Wall -Wpointer-arith"
   # These work in some but not all gcc versions
+  save_CFLAGS=$CFLAGS
   PGAC_PROG_CC_CFLAGS_OPT([-Wdeclaration-after-statement])
-  # -Wdeclaration-after-statement isn't applicable for C++
+  # -Wdeclaration-after-statement isn't applicable for C++.  Specific C files
+  # disable it, so AC_SUBST the negative form.
+  PERMIT_DECLARATION_AFTER_STATEMENT=
+  if test x"$save_CFLAGS" != "$CFLAGS"; then
+    PERMIT_DECLARATION_AFTER_STATEMENT=-Wno-declaration-after-statement
+  fi
+  AC_SUBST(PERMIT_DECLARATION_AFTER_STATEMENT)
   # Really don't want VLAs to be used in our dialect of C
   PGAC_PROG_CC_CFLAGS_OPT([-Werror=vla])
   # -Wvla is not applicable for C++
diff --git a/contrib/pgcrypto/Makefile b/contrib/pgcrypto/Makefile
index 573bc6d..1313b66 100644
--- a/contrib/pgcrypto/Makefile
+++ b/contrib/pgcrypto/Makefile
@@ -59,6 +59,9 @@ SHLIB_LINK += $(filter -leay32, $(LIBS))
 SHLIB_LINK += -lws2_32
 endif
 
+# Upstream uses a larger subset of C99.
+imath.o: CFLAGS+=$(PERMIT_DECLARATION_AFTER_STATEMENT)
+
 rijndael.o: rijndael.tbl
 
 rijndael.tbl:
diff --git a/contrib/pgcrypto/imath.c b/contrib/pgcrypto/imath.c
index 96be668..c72f965 100644
--- a/contrib/pgcrypto/imath.c
+++ b/contrib/pgcrypto/imath.c
@@ -1,6 +1,27 @@
+/*-------------------------------------------------------------------------
+ *
+ * imath.c
+ *
+ * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
+ *
+ *
+ * IDENTIFICATION
+ *	  contrib/pgcrypto/imath.c
+ *
+ * Changes versus https://github.com/creachadair/imath are:
+ * - replace malloc(), realloc() and free() with px_ versions
+ * - redirect assert() to Assert()
+ * - #undef MIN, #undef MAX before defining them
+ * - remove includes covered by c.h
+ * - rename DEBUG to IMATH_DEBUG
+ *
+ * Last synchronized from version 1.29.  Upstream copyright terms follow.
+ *-------------------------------------------------------------------------
+ */
+
 /*
-  Name:     imath.c
-  Purpose:  Arbitrary precision integer arithmetic routines.
+  Name:		imath.c
+  Purpose:	Arbitrary precision integer arithmetic routines.
   Author:   M. J. Fromberger
 
   Copyright (C) 2002-2007 Michael J. Fromberger, All Rights Reserved.
@@ -24,12 +45,13 @@
   SOFTWARE.
  */
 
+#include "postgres.h"
+
 #include "imath.h"
+#include "px.h"
 
-#include <assert.h>
-#include <ctype.h>
-#include <stdlib.h>
-#include <string.h>
+#undef assert
+#define assert(TEST) Assert(TEST)
 
 const mp_result MP_OK = 0;		/* no error, all is well  */
 const mp_result MP_FALSE = 0;	/* boolean false          */
@@ -135,6 +157,8 @@ CLAMP(mp_int z_)
 }
 
 /* Select min/max. */
+#undef MIN
+#undef MAX
 static inline int
 MIN(int A, int B)
 {
@@ -435,7 +459,7 @@ mp_int_init(mp_int z)
 mp_int
 mp_int_alloc(void)
 {
-	mp_int		out = malloc(sizeof(mpz_t));
+	mp_int		out = px_alloc(sizeof(mpz_t));
 
 	if (out != NULL)
 		mp_int_init(out);
@@ -561,7 +585,7 @@ mp_int_free(mp_int z)
 	assert(z != NULL);
 
 	mp_int_clear(z);
-	free(z);					/* note: NOT s_free() */
+	px_free(z);					/* note: NOT s_free() */
 }
 
 mp_result
@@ -2162,18 +2186,18 @@ mp_error_string(mp_result res)
 /*------------------------------------------------------------------------*/
 /* Private functions for internal use.  These make assumptions.           */
 
-#if DEBUG
+#if IMATH_DEBUG
 static const mp_digit fill = (mp_digit) 0xdeadbeefabad1dea;
 #endif
 
 static mp_digit *
 s_alloc(mp_size num)
 {
-	mp_digit   *out = malloc(num * sizeof(mp_digit));
+	mp_digit   *out = px_alloc(num * sizeof(mp_digit));
 
 	assert(out != NULL);
 
-#if DEBUG
+#if IMATH_DEBUG
 	for (mp_size ix = 0; ix < num; ++ix)
 		out[ix] = fill;
 #endif
@@ -2183,7 +2207,7 @@ s_alloc(mp_size num)
 static mp_digit *
 s_realloc(mp_digit *old, mp_size osize, mp_size nsize)
 {
-#if DEBUG
+#if IMATH_DEBUG
 	mp_digit   *new = s_alloc(nsize);
 
 	assert(new != NULL);
@@ -2192,7 +2216,7 @@ s_realloc(mp_digit *old, mp_size osize, mp_size nsize)
 		new[ix] = fill;
 	memcpy(new, old, osize * sizeof(mp_digit));
 #else
-	mp_digit   *new = realloc(old, nsize * sizeof(mp_digit));
+	mp_digit   *new = px_realloc(old, nsize * sizeof(mp_digit));
 
 	assert(new != NULL);
 #endif
@@ -2203,7 +2227,7 @@ s_realloc(mp_digit *old, mp_size osize, mp_size nsize)
 static void
 s_free(void *ptr)
 {
-	free(ptr);
+	px_free(ptr);
 }
 
 static bool
diff --git a/contrib/pgcrypto/imath.h b/contrib/pgcrypto/imath.h
index ab9ced1..65be748 100644
--- a/contrib/pgcrypto/imath.h
+++ b/contrib/pgcrypto/imath.h
@@ -28,8 +28,6 @@
 #define IMATH_H_
 
 #include <limits.h>
-#include <stdbool.h>
-#include <stdint.h>
 
 typedef unsigned char mp_sign;
 typedef unsigned int mp_size;
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 758ea43..93ab893 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -261,6 +261,7 @@ CFLAGS = @CFLAGS@
 CFLAGS_VECTOR = @CFLAGS_VECTOR@
 CFLAGS_SSE42 = @CFLAGS_SSE42@
 CFLAGS_ARMV8_CRC32C = @CFLAGS_ARMV8_CRC32C@
+PERMIT_DECLARATION_AFTER_STATEMENT = @PERMIT_DECLARATION_AFTER_STATEMENT@
 CXXFLAGS = @CXXFLAGS@
 
 LLVM_CPPFLAGS = @LLVM_CPPFLAGS@
