diff --git a/configure b/configure
index 3c23e6647e0d6ccb42ca85b698fb1bac7f349fef..b691160818c5a0c2fcdc45cab0d10f77d7ed2f6f 100755
--- a/configure
+++ b/configure
@@ -2502,7 +2502,7 @@ echo "$as_me: using CFLAGS=$CFLAGS" >&6;}
 
 # We already have this in Makefile.win32, but configure needs it too
 if test "$PORTNAME" = "win32"; then
-  CPPFLAGS="$CPPFLAGS -I$srcdir/src/include/port/win32"
+  CPPFLAGS="$CPPFLAGS -I$srcdir/src/include/port/win32 -DEXEC_BACKEND"
 fi
 
 # Check if the compiler still works with the template settings
@@ -12079,6 +12079,7 @@ esac
 case $host_os in mingw*)
 LIBOBJS="$LIBOBJS dirmod.$ac_objext"
 LIBOBJS="$LIBOBJS copydir.$ac_objext"
+LIBOBJS="$LIBOBJS rand.$ac_objext"
 LIBOBJS="$LIBOBJS gettimeofday.$ac_objext"
 LIBOBJS="$LIBOBJS pipe.$ac_objext" ;;
 esac
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index f3a1d391820a3b520ec37c39c9771c57b1f0eb60..0a1f3fa70d0bc23660c5746d6a72ec80d6be1c01 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -1,5 +1,5 @@
 # -*-makefile-*-
-# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.173 2004/01/19 21:20:06 tgl Exp $
+# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.174 2004/02/02 00:11:30 momjian Exp $
 
 #------------------------------------------------------------------------------
 # All PostgreSQL makefiles include this file and use the variables it sets,
@@ -346,6 +346,11 @@ LIBS += -lpgport
 LDFLAGS := -L$(top_builddir)/src/port $(LDFLAGS)
 endif
 
+# to make ws2_32.lib the last library
+ifeq ($(PORTNAME),win32)
+LIBS += -lws2_32
+endif
+
 # Not really standard libc functions, used by the backend.
 TAS         = @TAS@
 
diff --git a/src/backend/Makefile b/src/backend/Makefile
index ce537537c3e88169b4f521afcccf9ce77c5589ce..5d342fa2735541c066b185bed56731ba5c2175dd 100644
--- a/src/backend/Makefile
+++ b/src/backend/Makefile
@@ -4,7 +4,7 @@
 #
 # Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/backend/Makefile,v 1.97 2003/11/29 19:51:39 pgsql Exp $
+# $PostgreSQL: pgsql/src/backend/Makefile,v 1.98 2004/02/02 00:11:30 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -32,12 +32,15 @@ endif
 all: submake-libpgport postgres $(POSTGRES_IMP)
 
 ifneq ($(PORTNAME), cygwin)
+ifneq ($(PORTNAME), win32)
 
 postgres: $(OBJS)
 	$(CC) $(CFLAGS) $(LDFLAGS) $(export_dynamic) $^ $(LIBS) -o $@
 
-else # cygwin
+endif
+endif
 
+ifeq ($(PORTNAME), cygwin)
 postgres: $(OBJS) $(DLLINIT) postgres.def libpostgres.a
 	$(DLLTOOL) --dllname $@$(X) --output-exp $@.exp --def postgres.def
 	$(CC) $(CFLAGS) $(LDFLAGS) -o $@$(X) -Wl,--base-file,$@.base $@.exp $(OBJS) $(LIBS)
@@ -53,6 +56,24 @@ libpostgres.a: postgres.def
 
 endif # cygwin
 
+ifeq ($(PORTNAME), win32)
+
+postgres: $(OBJS) $(DLLINIT) postgres.def libpostgres.a
+	$(DLLTOOL) --dllname $@$(X) --output-exp $@.exp --def postgres.def
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@$(X) -Wl,--base-file,$@.base $@.exp $(OBJS) $(LIBS)
+	$(DLLTOOL) --dllname $@$(X) --base-file $@.base --output-exp $@.exp --def postgres.def
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@$(X) $@.exp $(OBJS) $(LIBS)
+	rm -f $@.exp $@.base
+
+postgres.def: $(OBJS)
+	$(DLLTOOL) --export-all --output-def $@ $^
+
+libpostgres.a: postgres.def
+	$(DLLTOOL) --dllname postgres.exe --def postgres.def --output-lib $@
+
+endif # win32
+
+
 ifeq ($(PORTNAME), aix)
 
 postgres: $(POSTGRES_IMP)
@@ -130,6 +151,11 @@ ifeq ($(PORTNAME), cygwin)
 ifeq ($(MAKE_DLL), true)
 	$(INSTALL_DATA) libpostgres.a $(DESTDIR)$(libdir)/libpostgres.a
 endif
+endif
+ifeq ($(PORTNAME), win32)
+ifeq ($(MAKE_DLL), true)
+	$(INSTALL_DATA) libpostgres.a $(DESTDIR)$(libdir)/libpostgres.a
+endif
 endif
 	$(MAKE) -C catalog install-data
 	$(INSTALL_DATA) $(srcdir)/libpq/pg_hba.conf.sample $(DESTDIR)$(datadir)/pg_hba.conf.sample
@@ -157,6 +183,11 @@ ifeq ($(MAKE_DLL), true)
 	$(mkinstalldirs) $(DESTDIR)$(libdir)
 endif
 endif
+ifeq ($(PORTNAME), win32)
+ifeq ($(MAKE_DLL), true)
+	$(mkinstalldirs) $(DESTDIR)$(libdir)
+endif
+endif
 ifeq ($(MAKE_EXPORTS), true)
 	$(mkinstalldirs) $(DESTDIR)$(pkglibdir)
 endif
@@ -173,6 +204,11 @@ ifeq ($(PORTNAME), cygwin)
 ifeq ($(MAKE_DLL), true)
 	rm -f $(DESTDIR)$(libdir)/libpostgres.a
 endif
+endif
+ifeq ($(PORTNAME), win32)
+ifeq ($(MAKE_DLL), true)
+	rm -f $(DESTDIR)$(libdir)/libpostgres.a
+endif
 endif
 	$(MAKE) -C catalog uninstall-data
 	rm -f $(DESTDIR)$(datadir)/pg_hba.conf.sample \
@@ -189,6 +225,9 @@ clean:
 		$(top_builddir)/src/include/utils/fmgroids.h
 ifeq ($(PORTNAME), cygwin)
 	rm -f postgres.dll postgres.def libpostgres.a
+endif
+ifeq ($(PORTNAME), win32)
+	rm -f postgres.dll postgres.def libpostgres.a
 endif
 	for i in $(DIRS); do $(MAKE) -C $$i clean || exit; done
 
diff --git a/src/backend/main/main.c b/src/backend/main/main.c
index 90a1a35a3a5252414d6103533dfc8e3c9a92588c..a4a9c84ce04f94ac447e400a67571ddc3597e449 100644
--- a/src/backend/main/main.c
+++ b/src/backend/main/main.c
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/main/main.c,v 1.72 2004/01/27 00:45:26 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/main/main.c,v 1.73 2004/02/02 00:11:31 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -227,7 +227,11 @@ main(int argc, char *argv[])
 	 */
 	len = strlen(new_argv[0]);
 
-	if (len >= 10 && strcmp(new_argv[0] + len - 10, "postmaster") == 0)
+	if ((len >= 10 && strcmp(new_argv[0] + len - 10, "postmaster") == 0)
+#ifdef WIN32
+		|| (len >= 14 && strcmp(new_argv[0] + len - 14, "postmaster.exe") == 0)
+#endif
+		)
 	{
 		/* Called as "postmaster" */
 		exit(PostmasterMain(argc, new_argv));
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 18b4a193f3f9ef2664b9918e4e3aa2052d5b48a6..02847698e6062903ecfbc96a301c109a9b0be12d 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -10,7 +10,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.29 2004/01/27 00:45:26 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.30 2004/02/02 00:11:31 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -140,7 +140,12 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size)
 	/* use intimate shared memory on Solaris */
 	memAddress = shmat(shmid, 0, SHM_SHARE_MMU);
 #else
+
+#ifdef EXEC_BACKEND
+	memAddress = shmat(shmid, UsedShmemSegAddr, 0);
+#else
 	memAddress = shmat(shmid, 0, 0);
+#endif
 #endif
 
 	if (memAddress == (void *) -1)
@@ -244,18 +249,32 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
 	PGShmemHeader *hdr;
 	IpcMemoryId shmid;
 
-	/* Room for a header? */
-	Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
-
+#ifdef EXEC_BACKEND
 	/* If Exec case, just attach and return the pointer */
-	if (ExecBackend && UsedShmemSegAddr != NULL && !makePrivate)
+	if (UsedShmemSegAddr != NULL && !makePrivate)
 	{
+		void* origUsedShmemSegAddr = UsedShmemSegAddr;
+
+#ifdef CYGWIN
+		/* cygipc (currently) appears to not detach on exec. */
+		PGSharedMemoryDetach();
+		UsedShmemSegAddr = origUsedShmemSegAddr;
+#endif
+		elog(DEBUG3,"Attaching to %x",UsedShmemSegAddr);
 		hdr = PGSharedMemoryAttach((IpcMemoryKey) UsedShmemSegID, &shmid);
 		if (hdr == NULL)
-			elog(FATAL, "could not attach to proper memory at fixed address: shmget(key=%lu, addr=%p) failed: %m",
-				 UsedShmemSegID, UsedShmemSegAddr);
+			elog(FATAL, "could not attach to proper memory at fixed address: shmget(key=%d, addr=%p) failed: %m",
+				 (int) UsedShmemSegID, UsedShmemSegAddr);
+		if (hdr != origUsedShmemSegAddr)
+			elog(FATAL,"attaching to shared mem returned unexpected address (got %p, expected %p)",
+				 hdr,UsedShmemSegAddr);
+		UsedShmemSegAddr = hdr;
 		return hdr;
 	}
+#endif
+
+	/* Room for a header? */
+	Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
 
 	/* Make sure PGSharedMemoryAttach doesn't fail without need */
 	UsedShmemSegAddr = NULL;
@@ -354,12 +373,18 @@ PGSharedMemoryDetach(void)
 {
 	if (UsedShmemSegAddr != NULL)
 	{
-		if (shmdt(UsedShmemSegAddr) < 0)
+		if ((shmdt(UsedShmemSegAddr) < 0)
+#if (defined(EXEC_BACKEND) && defined(CYGWIN))
+			/* Work-around for cygipc exec bug */
+			&& shmdt(NULL) < 0
+#endif
+			)
 			elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
 		UsedShmemSegAddr = NULL;
 	}
 }
 
+
 /*
  * Attach to shared memory and make sure it has a Postgres header
  *
diff --git a/src/backend/port/win32/shmem.c b/src/backend/port/win32/shmem.c
index e3ab1f724de2129882ce090c955e7eede7c0d253..0048d7554ad32279adfd340f024e0ef8bdb72fa4 100644
--- a/src/backend/port/win32/shmem.c
+++ b/src/backend/port/win32/shmem.c
@@ -32,7 +32,7 @@ shmat(int memId, void *shmaddr, int flag)
 	/* TODO -- shmat needs to count # attached to shared mem */
 	void	   *lpmem = MapViewOfFileEx((HANDLE) memId,
 										FILE_MAP_WRITE | FILE_MAP_READ,
-				 0, 0, /* (DWORD)pshmdsc->segsize */ s_segsize, shmaddr);
+				 0, 0, /* (DWORD)pshmdsc->segsize */ 0 /* s_segsize */, shmaddr);
 
 	if (lpmem == NULL)
 	{
diff --git a/src/backend/utils/init/findbe.c b/src/backend/utils/init/findbe.c
index 2c8a45703af291f09ef7162c5fc69c31940bc9c9..2ef63872fa13aee8cb2164b50b5bbafdef25eb5e 100644
--- a/src/backend/utils/init/findbe.c
+++ b/src/backend/utils/init/findbe.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/init/findbe.c,v 1.40 2003/11/29 19:52:01 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/init/findbe.c,v 1.41 2004/02/02 00:11:31 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -205,9 +205,9 @@ FindExec(char *full_path, const char *argv0, const char *binary_name)
 	{
 		elog(DEBUG2, "searching PATH for executable");
 		path = strdup(p);		/* make a modifiable copy */
-		for (startp = path, endp = strchr(path, ':');
+		for (startp = path, endp = strchr(path, PATHSEP);
 			 startp && *startp;
-			 startp = endp + 1, endp = strchr(startp, ':'))
+			 startp = endp + 1, endp = strchr(startp, PATHSEP))
 		{
 			if (startp == endp) /* it's a "::" */
 				continue;
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 81607877da02a2f8798b700599d0bf76cf3d92be..5a9139cf106ace84ab07e12cc528e6297758db33 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -43,7 +43,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  * Portions taken from FreeBSD.
  *
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.21 2004/01/31 22:10:00 neilc Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.22 2004/02/02 00:11:31 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -144,11 +144,6 @@ static const char *backend_options = "-F -O -c search_path=pg_catalog -c exit_on
 #define DEVNULL "/dev/null"
 #endif
 
-#ifdef WIN32
-#define PATHSEP ';'
-#else
-#define PATHSEP ':'
-#endif
 
 /* detected path to postgres and (we assume) friends */
 char	   *pgpath;
diff --git a/src/include/port.h b/src/include/port.h
index 166f1d7ae6dc68f45497fea3116f515daa59b6bf..cfc82b83680810addcac9f8d18aec78517c7b2e7 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/port.h,v 1.15 2003/11/29 22:40:53 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/port.h,v 1.16 2004/02/02 00:11:31 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -44,6 +44,10 @@ extern int	pgunlink(const char *path);
 
 extern int	copydir(char *fromdir, char *todir);
 
+/* Missing rand functions */
+extern long	lrand48(void);
+extern void	srand48(long seed);
+
 /* Last parameter not used */
 extern int	gettimeofday(struct timeval * tp, struct timezone * tzp);
 
@@ -122,3 +126,20 @@ extern int pqGethostbyname(const char *name,
 				char *buffer, size_t buflen,
 				struct hostent **result,
 				int *herrno);
+
+/* $PATH (or %PATH%) path separator */
+#ifdef WIN32
+#define PATHSEP ';'
+#else
+#define PATHSEP ':'
+#endif
+
+/* FIXME: [win32] Placeholder win32 replacements, to allow continued development */
+#ifdef WIN32
+#define fsync(a)	_commit(a)
+#define sync()		_flushall()
+#define WEXITSTATUS(w)  (((w) >> 8) & 0xff)
+#define WIFEXITED(w)    (((w) & 0xff) == 0)
+#define WIFSIGNALED(w)  (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
+#define WTERMSIG(w)     ((w) & 0x7f)
+#endif
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index bba7b377712002c31b7f07d705fdd4b87619b394..978aa24b138c6ea5a67a721be26e8cf8fcbce8bd 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -4,7 +4,7 @@
 #
 # Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/interfaces/libpq/Makefile,v 1.96 2003/11/30 06:09:50 momjian Exp $
+# $PostgreSQL: pgsql/src/interfaces/libpq/Makefile,v 1.97 2004/02/02 00:11:31 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -33,6 +33,9 @@ endif
 # shared library link.  (The order in which you list them here doesn't
 # matter.)
 SHLIB_LINK += $(filter -lcrypt -ldes -lkrb -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl, $(LIBS)) $(THREAD_LIBS)
+ifeq ($(PORTNAME), win32)
+SHLIB_LINK += -lwsock32 -lws2_32
+endif
 
 
 all: all-lib
diff --git a/src/interfaces/libpq/pqsignal.c b/src/interfaces/libpq/pqsignal.c
index 5f41d228103be972df7765e13e595c1e5e3b45dd..dca69cd727c0992a72e197c3af0a71499c79cbca 100644
--- a/src/interfaces/libpq/pqsignal.c
+++ b/src/interfaces/libpq/pqsignal.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/interfaces/libpq/pqsignal.c,v 1.19 2004/01/09 02:02:43 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/interfaces/libpq/pqsignal.c,v 1.20 2004/02/02 00:11:31 momjian Exp $
  *
  * NOTES
  *		This shouldn't be in libpq, but the monitor and some other
@@ -44,6 +44,7 @@ pqsignal(int signo, pqsigfunc func)
 pqsigfunc
 pqsignalinquire(int signo)
 {
+#ifndef WIN32
 #if !defined(HAVE_POSIX_SIGNALS)
 	pqsigfunc old_sigfunc;
 	int		old_sigmask;
@@ -61,4 +62,7 @@ pqsignalinquire(int signo)
        return SIG_ERR;
 	return oact.sa_handler;
 #endif   /* !HAVE_POSIX_SIGNALS */
+#else
+	return SIG_DFL;
+#endif
 }
diff --git a/src/interfaces/libpq/win32.c b/src/interfaces/libpq/win32.c
index de83c38e8589ce8290eda4535f0192394edf8969..96097bca5970e0aa16bf20ad8f2fea4b68332a1b 100644
--- a/src/interfaces/libpq/win32.c
+++ b/src/interfaces/libpq/win32.c
@@ -22,7 +22,9 @@
 #define WIN32_LEAN_AND_MEAN
 #define WIN32_EXTRA_LEAN
 #define VC_EXTRALEAN
+#ifndef __MINGW32__
 #define NOGDI
+#endif
 #define NOCRYPT
 
 #include <windows.h>
diff --git a/src/port/rand.c b/src/port/rand.c
new file mode 100644
index 0000000000000000000000000000000000000000..faeec88b289f539705d2597d0512a76b8d0250b0
--- /dev/null
+++ b/src/port/rand.c
@@ -0,0 +1,78 @@
+/*-------------------------------------------------------------------------
+ *
+ * rand.c
+ *	  Missing rand implementations for Win32
+ *
+ *-------------------------------------------------------------------------
+ */
+
+/*
+ * Copyright (c) 1993 Martin Birgmeier
+ * All rights reserved.
+ *
+ * You may redistribute unmodified or modified versions of this source
+ * code provided that the above copyright notice and this and the
+ * following conditions are retained.
+ *
+ * This software is provided ``as is'', and comes with no warranties
+ * of any kind. I shall in no event be liable for anything that happens
+ * to anyone/anything when using this software.
+ */
+#define	RAND48_SEED_0	(0x330e)
+#define	RAND48_SEED_1	(0xabcd)
+#define	RAND48_SEED_2	(0x1234)
+#define	RAND48_MULT_0	(0xe66d)
+#define	RAND48_MULT_1	(0xdeec)
+#define	RAND48_MULT_2	(0x0005)
+#define	RAND48_ADD		(0x000b)
+
+unsigned short _rand48_seed[3] = {
+	RAND48_SEED_0,
+	RAND48_SEED_1,
+	RAND48_SEED_2
+};
+unsigned short _rand48_mult[3] = {
+	RAND48_MULT_0,
+	RAND48_MULT_1,
+	RAND48_MULT_2
+};
+unsigned short _rand48_add = RAND48_ADD;
+
+void
+_dorand48(unsigned short xseed[3])
+{
+	unsigned long accu;
+	unsigned short temp[2];
+
+	accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] +
+	 (unsigned long) _rand48_add;
+	temp[0] = (unsigned short) accu;	/* lower 16 bits */
+	accu >>= sizeof(unsigned short) * 8;
+	accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] +
+	 (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0];
+	temp[1] = (unsigned short) accu;	/* middle 16 bits */
+	accu >>= sizeof(unsigned short) * 8;
+	accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0];
+	xseed[0] = temp[0];
+	xseed[1] = temp[1];
+	xseed[2] = (unsigned short) accu;
+}
+
+long
+lrand48(void)
+{
+	_dorand48(_rand48_seed);
+	return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >1);
+}
+
+void
+srand48(long seed)
+{
+	_rand48_seed[0] = RAND48_SEED_0;
+	_rand48_seed[1] = (unsigned short) seed;
+	_rand48_seed[2] = (unsigned short) (seed >16);
+	_rand48_mult[0] = RAND48_MULT_0;
+	_rand48_mult[1] = RAND48_MULT_1;
+	_rand48_mult[2] = RAND48_MULT_2;
+	_rand48_add = RAND48_ADD;
+}