diff -c -r pgsql.orig/configure.in pgsql/configure.in
*** pgsql.orig/configure.in	2006-08-21 10:41:01.000000000 +0200
--- pgsql/configure.in	2006-08-21 11:27:51.000000000 +0200
***************
*** 709,722 ****
  fi
  
  
- if test "$with_ldap" = yes ; then
-   if test "$PORTNAME" != "win32"; then
-      AC_CHECK_LIB(ldap,    ldap_bind, [], [AC_MSG_ERROR([library 'ldap' is required for LDAP])])
-   else
-      AC_CHECK_LIB(wldap32, ldap_bind, [], [AC_MSG_ERROR([library 'wldap32' is required for LDAP])])
-   fi
- fi
- 
  ##
  ## Header files
  ##
--- 709,714 ----
***************
*** 1109,1122 ****
  PGAC_FUNC_GETPWUID_R_5ARG
  PGAC_FUNC_STRERROR_R_INT
  
- # this will link libpq against libldap_r
- if test "$with_ldap" = yes ; then
-   if test "$PORTNAME" != "win32"; then
-     AC_CHECK_LIB(ldap_r,    ldap_simple_bind, [], [AC_MSG_ERROR([library 'ldap_r' is required for LDAP])])
-     PTHREAD_LIBS="$PTHREAD_LIBS -lldap_r"
-   fi
- fi
- 
  CFLAGS="$_CFLAGS"
  LIBS="$_LIBS"
  
--- 1101,1106 ----
***************
*** 1130,1135 ****
--- 1114,1137 ----
  AC_SUBST(PTHREAD_LIBS)
  
  
+ # We can test for libldap_r only after we know PTHREAD_LIBS
+ if test "$with_ldap" = yes ; then
+   if test "$PORTNAME" != "win32"; then
+     if test "$enable_thread_safety" = yes; then
+       # try without PTHREAD_LIBS first, different function to avoid cache
+       AC_CHECK_LIB(ldap_r, ldap_bind, [], [
+         AC_CHECK_LIB(ldap_r, ldap_simple_bind,
+           [LIBS="-lldap_r $PTHREAD_LIBS $LIBS"; HAVE_LIBLDAP_R=1],
+           [AC_MSG_ERROR([library 'ldap_r' is required for LDAP])], [$PTHREAD_LIBS])
+       ])
+     else
+       AC_CHECK_LIB(ldap,   ldap_bind, [], [AC_MSG_ERROR([library 'ldap' is required for LDAP])])
+     fi
+   else
+     AC_CHECK_LIB(wldap32,  ldap_bind, [], [AC_MSG_ERROR([library 'wldap32' is required for LDAP])])
+   fi
+ fi
+ 
  # This test makes sure that run tests work at all.  Sometimes a shared
  # library is found by the linker, but the runtime linker can't find it.
  # This check should come after all modifications of compiler or linker
diff -c -r pgsql.orig/src/backend/Makefile pgsql/src/backend/Makefile
*** pgsql.orig/src/backend/Makefile	2006-08-21 10:41:04.000000000 +0200
--- pgsql/src/backend/Makefile	2006-08-21 11:02:36.000000000 +0200
***************
*** 28,35 ****
  # We put libpgport into OBJS, so remove it from LIBS
  LIBS := $(filter-out -lpgport, $(LIBS))
  
  # The backend doesn't need everything that's in LIBS, however
! LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))
  
  ##########################################################################
  
--- 28,40 ----
  # We put libpgport into OBJS, so remove it from LIBS
  LIBS := $(filter-out -lpgport, $(LIBS))
  
+ # If libldap_r is in LIBS, use libldap instead for the backend
+ ifeq ($(filter -lldap_r, $(LIBS)),-lldap_r)
+ LIBS += -lldap
+ endif
+ 
  # The backend doesn't need everything that's in LIBS, however
! LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses -lldap_r $(PTHREAD_LIBS), $(LIBS))
  
  ##########################################################################
  
diff -c -r pgsql.orig/src/interfaces/libpq/Makefile pgsql/src/interfaces/libpq/Makefile
*** pgsql.orig/src/interfaces/libpq/Makefile	2006-08-21 10:41:10.000000000 +0200
--- pgsql/src/interfaces/libpq/Makefile	2006-08-21 11:35:38.000000000 +0200
***************
*** 57,68 ****
  # shared library link.  (The order in which you list them here doesn't
  # matter.)
  ifneq ($(PORTNAME), win32)
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl, $(LIBS)) $(PTHREAD_LIBS)
  else
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl $(PTHREAD_LIBS), $(LIBS))
  endif
  ifeq ($(PORTNAME), win32)
! SHLIB_LINK += -lshfolder -lwsock32 -lws2_32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32 -lwldap32, $(LIBS))
  endif
  
  
--- 57,68 ----
  # shared library link.  (The order in which you list them here doesn't
  # matter.)
  ifneq ($(PORTNAME), win32)
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl -lldap -lldap_r, $(LIBS)) $(PTHREAD_LIBS)
  else
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl -lwldap32 $(PTHREAD_LIBS), $(LIBS))
  endif
  ifeq ($(PORTNAME), win32)
! SHLIB_LINK += -lshfolder -lwsock32 -lws2_32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32, $(LIBS))
  endif
  
  
