diff --git a/configure b/configure
index 08fd1c8443a09d3013588d1138a331e71987fe7a..51d27d8c7d0bb5bdedf88a6ce80ef0df2ee69093 100755
--- a/configure
+++ b/configure
@@ -20758,6 +20758,23 @@ esac
 
 fi
 
+# mingw has adopted a GNU-centric interpretation of optind/optreset,
+# so always use our version on Windows.
+if test "$PORTNAME" = "win32"; then
+  case " $LIBOBJS " in
+  *" getopt.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS getopt.$ac_objext"
+ ;;
+esac
+
+  case " $LIBOBJS " in
+  *" getopt_long.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS getopt_long.$ac_objext"
+ ;;
+esac
+
+fi
+
 # Cygwin's erand48() is broken (always returns zero) in some releases,
 # so force use of ours.
 if test "$PORTNAME" = "cygwin"; then
@@ -20880,25 +20897,25 @@ fi
 done
 
 
-case " $LIBOBJS " in
+  case " $LIBOBJS " in
   *" kill.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS kill.$ac_objext"
  ;;
 esac
 
-case " $LIBOBJS " in
+  case " $LIBOBJS " in
   *" open.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS open.$ac_objext"
  ;;
 esac
 
-case " $LIBOBJS " in
+  case " $LIBOBJS " in
   *" win32env.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS win32env.$ac_objext"
  ;;
 esac
 
-case " $LIBOBJS " in
+  case " $LIBOBJS " in
   *" win32error.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS win32error.$ac_objext"
  ;;
diff --git a/configure.in b/configure.in
index 3a0d43f8084d70cb5ec0ff611422cddeee50aa15..b999b9481a54cc64c024ccd2e05fd619323903c3 100644
--- a/configure.in
+++ b/configure.in
@@ -1328,6 +1328,13 @@ if test "$PORTNAME" = "solaris"; then
   AC_LIBOBJ(getopt)
 fi
 
+# mingw has adopted a GNU-centric interpretation of optind/optreset,
+# so always use our version on Windows.
+if test "$PORTNAME" = "win32"; then
+  AC_LIBOBJ(getopt)
+  AC_LIBOBJ(getopt_long)
+fi
+
 # Cygwin's erand48() is broken (always returns zero) in some releases,
 # so force use of ours.
 if test "$PORTNAME" = "cygwin"; then
@@ -1336,13 +1343,13 @@ fi
 
 # Win32 support
 if test "$PORTNAME" = "win32"; then
-AC_REPLACE_FUNCS(gettimeofday)
-AC_LIBOBJ(kill)
-AC_LIBOBJ(open)
-AC_LIBOBJ(win32env)
-AC_LIBOBJ(win32error)
-AC_DEFINE([HAVE_SYMLINK], 1,
-          [Define to 1 if you have the `symlink' function.])
+  AC_REPLACE_FUNCS(gettimeofday)
+  AC_LIBOBJ(kill)
+  AC_LIBOBJ(open)
+  AC_LIBOBJ(win32env)
+  AC_LIBOBJ(win32error)
+  AC_DEFINE([HAVE_SYMLINK], 1,
+            [Define to 1 if you have the `symlink' function.])
 fi
 
 if test "$with_readline" = yes; then
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 90854f44d79a12e7997fffd3c2d8fdc6cf89463f..18a238d5edacdb050a3998801c7c3f763f83c50e 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -313,7 +313,8 @@ extern char *optarg;
 extern int	optind,
 			opterr;
 
-#ifdef HAVE_INT_OPTRESET
+/* If not HAVE_GETOPT, we are using src/port/getopt.c, which has optreset */
+#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
 extern int	optreset;			/* might not be declared by system headers */
 #endif
 
@@ -751,7 +752,7 @@ PostmasterMain(int argc, char *argv[])
 	 * getopt(3) library so that it will work correctly in subprocesses.
 	 */
 	optind = 1;
-#ifdef HAVE_INT_OPTRESET
+#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
 	optreset = 1;				/* some systems need this too */
 #endif
 
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index ff2e9bd0aaa87b22b2aa66098aa54a35d61aec7a..7657458693fd15d94562a4ac7bff2aaa2584d14b 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -78,7 +78,8 @@
 extern char *optarg;
 extern int	optind;
 
-#ifdef HAVE_INT_OPTRESET
+/* If not HAVE_GETOPT, we are using src/port/getopt.c, which has optreset */
+#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
 extern int	optreset;			/* might not be declared by system headers */
 #endif
 
@@ -3442,7 +3443,7 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
 	 * or when this function is called a second time with another array.
 	 */
 	optind = 1;
-#ifdef HAVE_INT_OPTRESET
+#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
 	optreset = 1;				/* some systems need this too */
 #endif