diff --git a/configure b/configure
index 8176e99756..fec2f76085 100755
--- a/configure
+++ b/configure
@@ -699,8 +699,6 @@ with_gnu_ld
 LD
 LDFLAGS_SL
 LDFLAGS_EX
-LZ4_LIBS
-LZ4_CFLAGS
 with_lz4
 with_zlib
 with_system_tzdata
@@ -895,8 +893,6 @@ ICU_LIBS
 XML2_CONFIG
 XML2_CFLAGS
 XML2_LIBS
-LZ4_CFLAGS
-LZ4_LIBS
 LDFLAGS_EX
 LDFLAGS_SL
 PERL
@@ -1603,8 +1599,6 @@ Some influential environment variables:
   XML2_CONFIG path to xml2-config utility
   XML2_CFLAGS C compiler flags for XML2, overriding pkg-config
   XML2_LIBS   linker flags for XML2, overriding pkg-config
-  LZ4_CFLAGS  C compiler flags for LZ4, overriding pkg-config
-  LZ4_LIBS    linker flags for LZ4, overriding pkg-config
   LDFLAGS_EX  extra linker flags for linking executables only
   LDFLAGS_SL  extra linker flags for linking shared libraries only
   PERL        Perl program
@@ -8575,8 +8569,6 @@ fi
 #
 # LZ4
 #
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to build with LZ4 support" >&5
-$as_echo_n "checking whether to build with LZ4 support... " >&6; }
 
 
 
@@ -8585,9 +8577,7 @@ if test "${with_lz4+set}" = set; then :
   withval=$with_lz4;
   case $withval in
     yes)
-
-$as_echo "#define USE_LZ4 1" >>confdefs.h
-
+      :
       ;;
     no)
       :
@@ -8603,105 +8593,7 @@ else
 fi
 
 
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_lz4" >&5
-$as_echo "$with_lz4" >&6; }
-
-
-if test "$with_lz4" = yes; then
-
-pkg_failed=no
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for liblz4" >&5
-$as_echo_n "checking for liblz4... " >&6; }
-
-if test -n "$LZ4_CFLAGS"; then
-    pkg_cv_LZ4_CFLAGS="$LZ4_CFLAGS"
- elif test -n "$PKG_CONFIG"; then
-    if test -n "$PKG_CONFIG" && \
-    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"liblz4\""; } >&5
-  ($PKG_CONFIG --exists --print-errors "liblz4") 2>&5
-  ac_status=$?
-  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
-  test $ac_status = 0; }; then
-  pkg_cv_LZ4_CFLAGS=`$PKG_CONFIG --cflags "liblz4" 2>/dev/null`
-		      test "x$?" != "x0" && pkg_failed=yes
-else
-  pkg_failed=yes
-fi
- else
-    pkg_failed=untried
-fi
-if test -n "$LZ4_LIBS"; then
-    pkg_cv_LZ4_LIBS="$LZ4_LIBS"
- elif test -n "$PKG_CONFIG"; then
-    if test -n "$PKG_CONFIG" && \
-    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"liblz4\""; } >&5
-  ($PKG_CONFIG --exists --print-errors "liblz4") 2>&5
-  ac_status=$?
-  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
-  test $ac_status = 0; }; then
-  pkg_cv_LZ4_LIBS=`$PKG_CONFIG --libs "liblz4" 2>/dev/null`
-		      test "x$?" != "x0" && pkg_failed=yes
-else
-  pkg_failed=yes
-fi
- else
-    pkg_failed=untried
-fi
-
-
-
-if test $pkg_failed = yes; then
-        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-
-if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
-        _pkg_short_errors_supported=yes
-else
-        _pkg_short_errors_supported=no
-fi
-        if test $_pkg_short_errors_supported = yes; then
-	        LZ4_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "liblz4" 2>&1`
-        else
-	        LZ4_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "liblz4" 2>&1`
-        fi
-	# Put the nasty error message in config.log where it belongs
-	echo "$LZ4_PKG_ERRORS" >&5
-
-	as_fn_error $? "Package requirements (liblz4) were not met:
-
-$LZ4_PKG_ERRORS
 
-Consider adjusting the PKG_CONFIG_PATH environment variable if you
-installed software in a non-standard prefix.
-
-Alternatively, you may set the environment variables LZ4_CFLAGS
-and LZ4_LIBS to avoid the need to call pkg-config.
-See the pkg-config man page for more details." "$LINENO" 5
-elif test $pkg_failed = untried; then
-        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-	{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "The pkg-config script could not be found or is too old.  Make sure it
-is in your PATH or set the PKG_CONFIG environment variable to the full
-path to pkg-config.
-
-Alternatively, you may set the environment variables LZ4_CFLAGS
-and LZ4_LIBS to avoid the need to call pkg-config.
-See the pkg-config man page for more details.
-
-To get pkg-config, see <http://pkg-config.freedesktop.org/>.
-See \`config.log' for more details" "$LINENO" 5; }
-else
-	LZ4_CFLAGS=$pkg_cv_LZ4_CFLAGS
-	LZ4_LIBS=$pkg_cv_LZ4_LIBS
-        { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
-$as_echo "yes" >&6; }
-
-fi
-  LIBS="$LZ4_LIBS $LIBS"
-  CFLAGS="$LZ4_CFLAGS $CFLAGS"
-fi
 
 #
 # Assignments
@@ -12253,6 +12145,59 @@ fi
 
 fi
 
+if test "$with_lz4" = yes; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for LZ4_decompress_safe in -llz4" >&5
+$as_echo_n "checking for LZ4_decompress_safe in -llz4... " >&6; }
+if ${ac_cv_lib_lz4_LZ4_decompress_safe+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-llz4  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char LZ4_decompress_safe ();
+int
+main ()
+{
+return LZ4_decompress_safe ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_lib_lz4_LZ4_decompress_safe=yes
+else
+  ac_cv_lib_lz4_LZ4_decompress_safe=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_lz4_LZ4_decompress_safe" >&5
+$as_echo "$ac_cv_lib_lz4_LZ4_decompress_safe" >&6; }
+if test "x$ac_cv_lib_lz4_LZ4_decompress_safe" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_LIBLZ4 1
+_ACEOF
+
+  LIBS="-llz4 $LIBS"
+
+else
+  as_fn_error $? "lz4 library not found
+If you have liblz4 already installed, see config.log for details on the
+failure.  It is possible the compiler isn't looking in the proper directory.
+Use --without-lz4 to disable lz4 support." "$LINENO" 5
+fi
+
+fi
+
 if test "$enable_spinlocks" = yes; then
 
 $as_echo "#define HAVE_SPINLOCKS 1" >>confdefs.h
diff --git a/configure.ac b/configure.ac
index 54efbb22a3..98c94e7415 100644
--- a/configure.ac
+++ b/configure.ac
@@ -989,18 +989,9 @@ AC_SUBST(with_zlib)
 #
 # LZ4
 #
-AC_MSG_CHECKING([whether to build with LZ4 support])
-PGAC_ARG_BOOL(with, lz4, no, [build with LZ4 support],
-              [AC_DEFINE([USE_LZ4], 1, [Define to 1 to build with LZ4 support. (--with-lz4)])])
-AC_MSG_RESULT([$with_lz4])
+PGAC_ARG_BOOL(with, lz4, no, [build with LZ4 support])
 AC_SUBST(with_lz4)
 
-if test "$with_lz4" = yes; then
-  PKG_CHECK_MODULES(LZ4, liblz4)
-  LIBS="$LZ4_LIBS $LIBS"
-  CFLAGS="$LZ4_CFLAGS $CFLAGS"
-fi
-
 #
 # Assignments
 #
@@ -1192,6 +1183,14 @@ failure.  It is possible the compiler isn't looking in the proper directory.
 Use --without-zlib to disable zlib support.])])
 fi
 
+if test "$with_lz4" = yes; then
+  AC_CHECK_LIB(lz4, LZ4_decompress_safe, [],
+               [AC_MSG_ERROR([lz4 library not found
+If you have liblz4 already installed, see config.log for details on the
+failure.  It is possible the compiler isn't looking in the proper directory.
+Use --without-lz4 to disable lz4 support.])])
+fi
+
 if test "$enable_spinlocks" = yes; then
   AC_DEFINE(HAVE_SPINLOCKS, 1, [Define to 1 if you have spinlocks.])
 else
diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c
index a6f8b79a9e..6315e8d882 100644
--- a/src/backend/access/common/toast_compression.c
+++ b/src/backend/access/common/toast_compression.c
@@ -13,7 +13,7 @@
  */
 #include "postgres.h"
 
-#ifdef USE_LZ4
+#ifdef HAVE_LIBLZ4
 #include <lz4.h>
 #endif
 
@@ -133,7 +133,7 @@ pglz_decompress_datum_slice(const struct varlena *value,
 struct varlena *
 lz4_compress_datum(const struct varlena *value)
 {
-#ifndef USE_LZ4
+#ifndef HAVE_LIBLZ4
 	NO_LZ4_SUPPORT();
 #else
 	int32		valsize;
@@ -175,7 +175,7 @@ lz4_compress_datum(const struct varlena *value)
 struct varlena *
 lz4_decompress_datum(const struct varlena *value)
 {
-#ifndef USE_LZ4
+#ifndef HAVE_LIBLZ4
 	NO_LZ4_SUPPORT();
 #else
 	int32		rawsize;
@@ -207,7 +207,7 @@ lz4_decompress_datum(const struct varlena *value)
 struct varlena *
 lz4_decompress_datum_slice(const struct varlena *value, int32 slicelength)
 {
-#ifndef USE_LZ4
+#ifndef HAVE_LIBLZ4
 	NO_LZ4_SUPPORT();
 #else
 	int32		rawsize;
diff --git a/src/include/access/toast_compression.h b/src/include/access/toast_compression.h
index 514df0bed1..e0b164949a 100644
--- a/src/include/access/toast_compression.h
+++ b/src/include/access/toast_compression.h
@@ -85,7 +85,7 @@ CompressionNameToMethod(char *compression)
 		return TOAST_PGLZ_COMPRESSION;
 	else if (strcmp(compression, "lz4") == 0)
 	{
-#ifndef USE_LZ4
+#ifndef HAVE_LIBLZ4
 		NO_LZ4_SUPPORT();
 #endif
 		return TOAST_LZ4_COMPRESSION;
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 0a6422da4f..b204357c5a 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -319,6 +319,9 @@
 /* Define to 1 if you have the `ldap_r' library (-lldap_r). */
 #undef HAVE_LIBLDAP_R
 
+/* Define to 1 if you have the `lz4' library (-llz4). */
+#undef HAVE_LIBLZ4
+
 /* Define to 1 if you have the `m' library (-lm). */
 #undef HAVE_LIBM
 
@@ -358,6 +361,12 @@
 /* Define to 1 if `long long int' works and is 64 bits. */
 #undef HAVE_LONG_LONG_INT_64
 
+/* Define to 1 if you have the <lz4.h> header file. */
+#undef HAVE_LZ4_H
+
+/* Define to 1 if you have the <lz4/lz4.h> header file. */
+#undef HAVE_LZ4_LZ4_H
+
 /* Define to 1 if you have the <mbarrier.h> header file. */
 #undef HAVE_MBARRIER_H
 
@@ -899,9 +908,6 @@
 /* Define to 1 to build with LLVM based JIT support. (--with-llvm) */
 #undef USE_LLVM
 
-/* Define to 1 to build with LZ4 support (--with-lz4) */
-#undef USE_LZ4
-
 /* Define to select named POSIX semaphores. */
 #undef USE_NAMED_POSIX_SEMAPHORES
 
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index 14605371bb..2520bb2fb0 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -298,6 +298,7 @@ sub GenerateFiles
 		HAVE_LIBCRYPTO                              => undef,
 		HAVE_LIBLDAP                                => undef,
 		HAVE_LIBLDAP_R                              => undef,
+		HAVE_LIBLZ4                                 => undef,
 		HAVE_LIBM                                   => undef,
 		HAVE_LIBPAM                                 => undef,
 		HAVE_LIBREADLINE                            => undef,
@@ -485,7 +486,6 @@ sub GenerateFiles
 		USE_ICU => $self->{options}->{icu} ? 1 : undef,
 		USE_LIBXML                 => undef,
 		USE_LIBXSLT                => undef,
-		USE_LZ4                    => undef,
 		USE_LDAP                   => $self->{options}->{ldap} ? 1 : undef,
 		USE_LLVM                   => undef,
 		USE_NAMED_POSIX_SEMAPHORES => undef,
