diff --git a/src/interfaces/ecpg/ChangeLog b/src/interfaces/ecpg/ChangeLog
index 52557ee51665ebcc552b940df0aa1ab9a55c322f..3364b71dd30c33a289bda9e5078676fd1865bb2f 100644
--- a/src/interfaces/ecpg/ChangeLog
+++ b/src/interfaces/ecpg/ChangeLog
@@ -2177,4 +2177,10 @@ Th Mar 15 08:27:53 CET 2007
 	- Synced parser and keyword lists.
 	- Copied two token parsing from backend parser to ecpg parser.
 	- Also added a test case for this.
+
+Thu, 29 Mar 2007 11:18:39 +0200
+
+	- Added patch by Magnus Hagander <magnus@hagander.net> to use native
+	  win32 threads.
+	- Fixed regression tests to run threading tests.
 	- Set ecpg version to 4.3.1.
diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c
index 92c7ba8cc883586e5f88bf2cb42d6cf0dccd7fba..650c4e503c03007db098647778a3c1a2c9fc49aa 100644
--- a/src/interfaces/ecpg/ecpglib/connect.c
+++ b/src/interfaces/ecpg/ecpglib/connect.c
@@ -1,10 +1,14 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.40 2007/03/17 19:25:22 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.41 2007/03/29 12:02:24 meskes Exp $ */
 
 #define POSTGRES_ECPG_INTERNAL
 #include "postgres_fe.h"
 
 #ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
 #include <pthread.h>
+#else
+#include "ecpg-pthread-win32.h"
+#endif
 #endif
 #include "ecpgtype.h"
 #include "ecpglib.h"
@@ -13,9 +17,14 @@
 #include "sqlca.h"
 
 #ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
 static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_key_t actual_connection_key;
 static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT;
+#else
+static HANDLE connections_mutex = INVALID_HANDLE_VALUE;
+static DWORD actual_connection_key;
+#endif /* WIN32 */
 #endif
 static struct connection *actual_connection = NULL;
 static struct connection *all_connections = NULL;
@@ -30,7 +39,13 @@ ecpg_actual_connection_init(void)
 void
 ecpg_pthreads_init(void)
 {
+#ifndef WIN32
 	pthread_once(&actual_connection_key_once, ecpg_actual_connection_init);
+#else
+	static long has_run = 0;
+	if (InterlockedCompareExchange(&has_run, 1, 0) == 0)
+		ecpg_actual_connection_init();
+#endif
 }
 #endif
 
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index bf72920bdcc3497dc4558d1aeaa7ca0171d4efac..e1326d3e3e0474929c19474af222b10a583d9486 100644
--- a/src/interfaces/ecpg/ecpglib/execute.c
+++ b/src/interfaces/ecpg/ecpglib/execute.c
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.64 2007/02/11 15:18:17 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.65 2007/03/29 12:02:24 meskes Exp $ */
 
 /*
  * The aim is to get a simpler inteface to the database routines.
@@ -39,7 +39,6 @@ static char *
 quote_postgres(char *arg, bool quote, int lineno)
 {
 	char	*res;
-	int	error;
 	size_t  length;
 	size_t  escaped_len;
 	size_t  buffer_len;
@@ -58,13 +57,7 @@ quote_postgres(char *arg, bool quote, int lineno)
 		if (!res)
 			return (res);
 
-		error = 0;
 		escaped_len = PQescapeString(res+1, arg, buffer_len);
-		if (error)
-		{
-			ECPGfree(res);
-			return NULL;
-		}
 		if (length == escaped_len)
 		{
 			res[0] = res[escaped_len+1] = '\'';
diff --git a/src/interfaces/ecpg/ecpglib/extern.h b/src/interfaces/ecpg/ecpglib/extern.h
index 45a182a0d46eb5fc723fccc96fb01e46ac352102..0fb83f4495f1137cf1059a3c56fe5d84736fd97e 100644
--- a/src/interfaces/ecpg/ecpglib/extern.h
+++ b/src/interfaces/ecpg/ecpglib/extern.h
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/extern.h,v 1.22 2007/01/25 16:45:25 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/extern.h,v 1.23 2007/03/29 12:02:24 meskes Exp $ */
 
 #ifndef _ECPG_LIB_EXTERN_H
 #define _ECPG_LIB_EXTERN_H
@@ -6,6 +6,7 @@
 #include "postgres_fe.h"
 #include "libpq-fe.h"
 #include "sqlca.h"
+#include "ecpg_config.h"
 
 enum COMPAT_MODE
 {
diff --git a/src/interfaces/ecpg/ecpglib/misc.c b/src/interfaces/ecpg/ecpglib/misc.c
index 4273819e79d8a391ca56cc50614acdf586e503f0..47d924fac86160df0e6e8aec58fe3b3afa2d54b0 100644
--- a/src/interfaces/ecpg/ecpglib/misc.c
+++ b/src/interfaces/ecpg/ecpglib/misc.c
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.34 2007/01/12 10:00:13 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.35 2007/03/29 12:02:24 meskes Exp $ */
 
 #define POSTGRES_ECPG_INTERNAL
 #include "postgres_fe.h"
@@ -6,7 +6,11 @@
 #include <limits.h>
 #include <unistd.h>
 #ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
 #include <pthread.h>
+#else
+#include "ecpg-pthread-win32.h"
+#endif
 #endif
 #include "ecpgtype.h"
 #include "ecpglib.h"
@@ -58,9 +62,13 @@ static struct sqlca_t sqlca_init =
 };
 
 #ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
 static pthread_key_t sqlca_key;
 static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
 #else
+static DWORD sqlca_key;
+#endif
+#else
 static struct sqlca_t sqlca =
 {
 	{
@@ -90,8 +98,13 @@ static struct sqlca_t sqlca =
 #endif
 
 #ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
+#else
+static HANDLE debug_mutex = INVALID_HANDLE_VALUE;
+static HANDLE debug_init_mutex = INVALID_HANDLE_VALUE;
+#endif /* WIN32 */
 #endif
 static int	simple_debug = 0;
 static FILE *debugstream = NULL;
@@ -138,8 +151,13 @@ ECPGget_sqlca(void)
 {
 #ifdef ENABLE_THREAD_SAFETY
 	struct sqlca_t *sqlca;
-
+#ifdef WIN32
+	static long has_run = 0;
+	if (InterlockedCompareExchange(&has_run, 1, 0) == 0)
+		ecpg_sqlca_key_init();
+#else
 	pthread_once(&sqlca_key_once, ecpg_sqlca_key_init);
+#endif
 
 	sqlca = pthread_getspecific(sqlca_key);
 	if (sqlca == NULL)
diff --git a/src/interfaces/ecpg/include/ecpg-pthread-win32.h b/src/interfaces/ecpg/include/ecpg-pthread-win32.h
new file mode 100644
index 0000000000000000000000000000000000000000..df076ac8e595e2ac877f0e292371eaa457a2a79f
--- /dev/null
+++ b/src/interfaces/ecpg/include/ecpg-pthread-win32.h
@@ -0,0 +1,16 @@
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/include/ecpg-pthread-win32.h,v 1.1 2007/03/29 12:02:24 meskes Exp $ */
+/*
+ * pthread mapping macros for win32 native thread implementation
+ */
+#ifndef _ECPG_PTHREAD_WIN32_H
+#define _ECPG_PTHREAD_WIN32_H
+#define pthread_mutex_lock(x) do { \
+	if (*x == INVALID_HANDLE_VALUE) \
+	   *x = CreateMutex(NULL, FALSE, NULL); \
+    WaitForSingleObject(*x, INFINITE); \
+} while (0);
+#define pthread_mutex_unlock(x) ReleaseMutex(*x)
+#define pthread_getspecific(x) TlsGetValue(x)
+#define pthread_setspecific(x,y) TlsSetValue(x,y)
+#define pthread_key_create(x,y) *x = TlsAlloc();
+#endif
diff --git a/src/interfaces/ecpg/include/ecpg_config.h.in b/src/interfaces/ecpg/include/ecpg_config.h.in
index e363cae6656a8eb9b166a50b64a172de6f75116c..52ed8c817360d1f892e5fc2ad7657350b1b075ce 100644
--- a/src/interfaces/ecpg/include/ecpg_config.h.in
+++ b/src/interfaces/ecpg/include/ecpg_config.h.in
@@ -11,3 +11,7 @@
    (--enable-integer-datetimes) */
 #undef USE_INTEGER_DATETIMES
 
+/* Define to 1 to build client libraries as thread-safe code.
+ *    (--enable-thread-safety) */
+#undef ENABLE_THREAD_SAFETY
+
diff --git a/src/interfaces/ecpg/test/Makefile b/src/interfaces/ecpg/test/Makefile
index e5934c6fb3229bc1b49bd243a940ac35187c6918..f68ae926da8ff3d4bce16b7ef50fbd4f131f84c7 100644
--- a/src/interfaces/ecpg/test/Makefile
+++ b/src/interfaces/ecpg/test/Makefile
@@ -1,4 +1,4 @@
-# $PostgreSQL: pgsql/src/interfaces/ecpg/test/Makefile,v 1.66 2007/02/09 15:55:59 petere Exp $
+# $PostgreSQL: pgsql/src/interfaces/ecpg/test/Makefile,v 1.67 2007/03/29 12:02:24 meskes Exp $
 
 subdir = src/interfaces/ecpg/test
 top_builddir = ../../../..
@@ -11,6 +11,9 @@ TEMP_PORT = 5$(DEF_PGPORT)
 # default encoding
 MULTIBYTE = SQL_ASCII
 
+# threading
+THREAD := $(shell grep -q "define ENABLE_THREAD_SAFETY" ../include/ecpg_config.h && echo "--enable-threading")
+
 # locale
 NOLOCALE =
 ifdef NO_LOCALE
@@ -75,11 +78,11 @@ endif
 
 
 check: all
-	sh ./pg_regress  --dbname=regress1 --temp-install --top-builddir=$(top_builddir) --temp-port=$(TEMP_PORT) --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE)
+	sh ./pg_regress  --dbname=regress1 --temp-install --top-builddir=$(top_builddir) --temp-port=$(TEMP_PORT) --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE) $(THREAD)
 
 # the same options, but with --listen-on-tcp
 checktcp: all
-	sh ./pg_regress  --dbname=regress1 --temp-install --top-builddir=$(top_builddir) --temp-port=$(TEMP_PORT) --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE) --listen-on-tcp
+	sh ./pg_regress  --dbname=regress1 --temp-install --top-builddir=$(top_builddir) --temp-port=$(TEMP_PORT) --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE) --listen-on-tcp $(THREAD)
 
 installcheck: all
 	sh ./pg_regress  --dbname=regress1 --top-builddir=$(top_builddir) --load-language=plpgsql $(NOLOCALE)
diff --git a/src/interfaces/ecpg/test/expected/thread-thread-thread.stdout b/src/interfaces/ecpg/test/expected/thread-thread-thread.stdout
new file mode 100644
index 0000000000000000000000000000000000000000..a9d787cc55cbb2d168495737dd11410b74e58b04
--- /dev/null
+++ b/src/interfaces/ecpg/test/expected/thread-thread-thread.stdout
@@ -0,0 +1 @@
+Success.
diff --git a/src/interfaces/ecpg/test/expected/thread-thread.c b/src/interfaces/ecpg/test/expected/thread-thread.c
index ae720f4d6e96264cbc18ebcd46cdb639ad0f44a9..5011640b327536abc8bf7f31237b5b4ea2a8b846 100644
--- a/src/interfaces/ecpg/test/expected/thread-thread.c
+++ b/src/interfaces/ecpg/test/expected/thread-thread.c
@@ -13,19 +13,18 @@
  *	by Philip Yarra & Lee Kindness.
  */
 #include <stdlib.h>
+#include "ecpg_config.h"
+
 #ifndef ENABLE_THREAD_SAFETY
 int
 main(void)
 {
-	printf("Success.\n");
+	printf("No threading enabled.\n");
 	return 0;
 }
 #else
 #include <pthread.h>
 
-#undef DEBUG
-
-
 
 #line 1 "regression.h"
 
@@ -34,7 +33,7 @@ main(void)
 
 
 
-#line 19 "thread.pgc"
+#line 18 "thread.pgc"
 
 
 void *test_thread(void *arg);
@@ -49,36 +48,34 @@ int main(int argc, char *argv[])
   /* exec sql begin declare section */
    
   
-#line 31 "thread.pgc"
+#line 30 "thread.pgc"
  int  l_rows    ;
 /* exec sql end declare section */
-#line 32 "thread.pgc"
-
+#line 31 "thread.pgc"
 
 
- /* Switch off debug output for regression tests. The threads get executed in
+ /* Do not switch on debug output for regression tests. The threads get executed in
   * more or less random order */
-  ECPGdebug(0, stderr);
-
+ /* ECPGdebug(1, stderr); */
 
   /* setup test_thread table */
   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
-#line 41 "thread.pgc"
+#line 38 "thread.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, "drop table test_thread ", ECPGt_EOIT, ECPGt_EORT);}
-#line 42 "thread.pgc"
+#line 39 "thread.pgc"
  /* DROP might fail */
   { ECPGtrans(__LINE__, NULL, "commit");}
-#line 43 "thread.pgc"
+#line 40 "thread.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, "create  table test_thread ( tstamp timestamp    not null default cast( timeofday () as timestamp   ) , thread TEXT    not null , iteration integer   not null , primary key( thread , iteration )   )    ", ECPGt_EOIT, ECPGt_EORT);}
-#line 48 "thread.pgc"
+#line 45 "thread.pgc"
 
   { ECPGtrans(__LINE__, NULL, "commit");}
-#line 49 "thread.pgc"
+#line 46 "thread.pgc"
 
   { ECPGdisconnect(__LINE__, "CURRENT");}
-#line 50 "thread.pgc"
+#line 47 "thread.pgc"
 
 
   /* create, and start, threads */
@@ -102,18 +99,18 @@ int main(int argc, char *argv[])
 
   /* and check results */
   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
-#line 72 "thread.pgc"
+#line 69 "thread.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, "select  count (*)  from test_thread   ", ECPGt_EOIT, 
 	ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int), 
 	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
-#line 73 "thread.pgc"
+#line 70 "thread.pgc"
 
   { ECPGtrans(__LINE__, NULL, "commit");}
-#line 74 "thread.pgc"
+#line 71 "thread.pgc"
 
   { ECPGdisconnect(__LINE__, "CURRENT");}
-#line 75 "thread.pgc"
+#line 72 "thread.pgc"
 
   if( l_rows == (nthreads * iterations) )
     printf("Success.\n");
@@ -130,25 +127,25 @@ void *test_thread(void *arg)
     
    
   
-#line 88 "thread.pgc"
+#line 85 "thread.pgc"
  int  l_i    ;
  
-#line 89 "thread.pgc"
+#line 86 "thread.pgc"
  char  l_connection [ 128 ]    ;
 /* exec sql end declare section */
-#line 90 "thread.pgc"
+#line 87 "thread.pgc"
 
 
   /* build up connection name, and connect to database */
   snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
   /* exec sql whenever sqlerror  sqlprint ; */
-#line 94 "thread.pgc"
+#line 91 "thread.pgc"
 
   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , l_connection, 0); 
-#line 95 "thread.pgc"
+#line 92 "thread.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 95 "thread.pgc"
+#line 92 "thread.pgc"
 
   if( sqlca.sqlcode != 0 )
     {
@@ -156,52 +153,42 @@ if (sqlca.sqlcode < 0) sqlprint();}
       return( NULL );
     }
   { ECPGtrans(__LINE__, l_connection, "begin transaction ");
-#line 101 "thread.pgc"
+#line 98 "thread.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 101 "thread.pgc"
+#line 98 "thread.pgc"
 
 
   /* insert into test_thread table */
   for( l_i = 1; l_i <= iterations; l_i++ )
     {
-#ifdef DEBUG
-      printf("%s: inserting %d\n", l_connection, l_i);
-#endif
       { ECPGdo(__LINE__, 0, 1, l_connection, "insert into test_thread ( thread  , iteration  ) values (  ? ,  ? ) ", 
 	ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char), 
 	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
 	ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int), 
 	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
-#line 109 "thread.pgc"
+#line 103 "thread.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 109 "thread.pgc"
+#line 103 "thread.pgc"
 
-#ifdef DEBUG
-      if( sqlca.sqlcode == 0 )
-	printf("%s: insert done\n", l_connection);
-      else
+      if( sqlca.sqlcode != 0 )
 	printf("%s: ERROR: insert failed!\n", l_connection);
-#endif
     }
 
   /* all done */
   { ECPGtrans(__LINE__, l_connection, "commit");
-#line 119 "thread.pgc"
+#line 109 "thread.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 119 "thread.pgc"
+#line 109 "thread.pgc"
 
   { ECPGdisconnect(__LINE__, l_connection);
-#line 120 "thread.pgc"
+#line 110 "thread.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 120 "thread.pgc"
+#line 110 "thread.pgc"
 
-#ifdef DEBUG
-  printf("%s: done!\n", l_connection);
-#endif
   return( NULL );
 }
 #endif /* ENABLE_THREAD_SAFETY */
diff --git a/src/interfaces/ecpg/test/expected/thread-thread.stdout b/src/interfaces/ecpg/test/expected/thread-thread.stdout
index a9d787cc55cbb2d168495737dd11410b74e58b04..75fe16bb36bb2580d8365d62780f9c21ad7c259c 100644
--- a/src/interfaces/ecpg/test/expected/thread-thread.stdout
+++ b/src/interfaces/ecpg/test/expected/thread-thread.stdout
@@ -1 +1 @@
-Success.
+No threading enabled.
diff --git a/src/interfaces/ecpg/test/expected/thread-thread_implicit-thread.stdout b/src/interfaces/ecpg/test/expected/thread-thread_implicit-thread.stdout
new file mode 100644
index 0000000000000000000000000000000000000000..a9d787cc55cbb2d168495737dd11410b74e58b04
--- /dev/null
+++ b/src/interfaces/ecpg/test/expected/thread-thread_implicit-thread.stdout
@@ -0,0 +1 @@
+Success.
diff --git a/src/interfaces/ecpg/test/expected/thread-thread_implicit.c b/src/interfaces/ecpg/test/expected/thread-thread_implicit.c
index c004a7d11672abf9cb709e3ec6381968b0e2f879..1fd3faa48d550ba3c0d29d04302db4a88863e831 100644
--- a/src/interfaces/ecpg/test/expected/thread-thread_implicit.c
+++ b/src/interfaces/ecpg/test/expected/thread-thread_implicit.c
@@ -14,19 +14,18 @@
  */
 
 #include <stdlib.h>
+#include "ecpg_config.h"
+
 #ifndef ENABLE_THREAD_SAFETY
 int
 main(void)
 {
-	printf("Success.\n");
+	printf("No threading enabled.\n");
 	return 0;
 }
 #else
 #include <pthread.h>
 
-#undef DEBUG
-
-
 
 #line 1 "regression.h"
 
@@ -35,7 +34,7 @@ main(void)
 
 
 
-#line 20 "thread_implicit.pgc"
+#line 19 "thread_implicit.pgc"
 
 
 void *test_thread(void *arg);
@@ -50,36 +49,34 @@ int main(int argc, char *argv[])
   /* exec sql begin declare section */
    
   
-#line 32 "thread_implicit.pgc"
+#line 31 "thread_implicit.pgc"
  int  l_rows    ;
 /* exec sql end declare section */
-#line 33 "thread_implicit.pgc"
-
+#line 32 "thread_implicit.pgc"
 
 
- /* Switch off debug output for regression tests. The threads get executed in
+ /* Do not switch on debug output for regression tests. The threads get executed in
   * more or less random order */
-  ECPGdebug(0, stderr);
-
+ /* ECPGdebug(1, stderr); */
 
   /* setup test_thread table */
   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
-#line 42 "thread_implicit.pgc"
+#line 39 "thread_implicit.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, "drop table test_thread ", ECPGt_EOIT, ECPGt_EORT);}
-#line 43 "thread_implicit.pgc"
+#line 40 "thread_implicit.pgc"
  /* DROP might fail */
   { ECPGtrans(__LINE__, NULL, "commit");}
-#line 44 "thread_implicit.pgc"
+#line 41 "thread_implicit.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, "create  table test_thread ( tstamp timestamp    not null default cast( timeofday () as timestamp   ) , thread TEXT    not null , iteration integer   not null , primary key( thread , iteration )   )    ", ECPGt_EOIT, ECPGt_EORT);}
-#line 49 "thread_implicit.pgc"
+#line 46 "thread_implicit.pgc"
 
   { ECPGtrans(__LINE__, NULL, "commit");}
-#line 50 "thread_implicit.pgc"
+#line 47 "thread_implicit.pgc"
 
   { ECPGdisconnect(__LINE__, "CURRENT");}
-#line 51 "thread_implicit.pgc"
+#line 48 "thread_implicit.pgc"
 
 
   /* create, and start, threads */
@@ -103,18 +100,18 @@ int main(int argc, char *argv[])
 
   /* and check results */
   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
-#line 73 "thread_implicit.pgc"
+#line 70 "thread_implicit.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, "select  count (*)  from test_thread   ", ECPGt_EOIT, 
 	ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int), 
 	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
-#line 74 "thread_implicit.pgc"
+#line 71 "thread_implicit.pgc"
 
   { ECPGtrans(__LINE__, NULL, "commit");}
-#line 75 "thread_implicit.pgc"
+#line 72 "thread_implicit.pgc"
 
   { ECPGdisconnect(__LINE__, "CURRENT");}
-#line 76 "thread_implicit.pgc"
+#line 73 "thread_implicit.pgc"
 
   if( l_rows == (nthreads * iterations) )
     printf("Success.\n");
@@ -131,25 +128,25 @@ void *test_thread(void *arg)
     
    
   
-#line 89 "thread_implicit.pgc"
+#line 86 "thread_implicit.pgc"
  int  l_i    ;
  
-#line 90 "thread_implicit.pgc"
+#line 87 "thread_implicit.pgc"
  char  l_connection [ 128 ]    ;
 /* exec sql end declare section */
-#line 91 "thread_implicit.pgc"
+#line 88 "thread_implicit.pgc"
 
 
   /* build up connection name, and connect to database */
   snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
   /* exec sql whenever sqlerror  sqlprint ; */
-#line 95 "thread_implicit.pgc"
+#line 92 "thread_implicit.pgc"
 
   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , l_connection, 0); 
-#line 96 "thread_implicit.pgc"
+#line 93 "thread_implicit.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 96 "thread_implicit.pgc"
+#line 93 "thread_implicit.pgc"
 
   if( sqlca.sqlcode != 0 )
     {
@@ -157,52 +154,42 @@ if (sqlca.sqlcode < 0) sqlprint();}
       return( NULL );
     }
   { ECPGtrans(__LINE__, NULL, "begin transaction ");
-#line 102 "thread_implicit.pgc"
+#line 99 "thread_implicit.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 102 "thread_implicit.pgc"
+#line 99 "thread_implicit.pgc"
 
 
   /* insert into test_thread table */
   for( l_i = 1; l_i <= iterations; l_i++ )
     {
-#ifdef DEBUG
-      printf("%s: inserting %d\n", l_connection, l_i);
-#endif
       { ECPGdo(__LINE__, 0, 1, NULL, "insert into test_thread ( thread  , iteration  ) values (  ? ,  ? ) ", 
 	ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char), 
 	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
 	ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int), 
 	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
-#line 110 "thread_implicit.pgc"
+#line 104 "thread_implicit.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 110 "thread_implicit.pgc"
+#line 104 "thread_implicit.pgc"
 
-#ifdef DEBUG
-      if( sqlca.sqlcode == 0 )
-	printf("%s: insert done\n", l_connection);
-      else
+      if( sqlca.sqlcode != 0 )
 	printf("%s: ERROR: insert failed!\n", l_connection);
-#endif
     }
 
   /* all done */
   { ECPGtrans(__LINE__, NULL, "commit");
-#line 120 "thread_implicit.pgc"
+#line 110 "thread_implicit.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 120 "thread_implicit.pgc"
+#line 110 "thread_implicit.pgc"
 
   { ECPGdisconnect(__LINE__, l_connection);
-#line 121 "thread_implicit.pgc"
+#line 111 "thread_implicit.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
-#line 121 "thread_implicit.pgc"
+#line 111 "thread_implicit.pgc"
 
-#ifdef DEBUG
-  printf("%s: done!\n", l_connection);
-#endif
   return( NULL );
 }
 #endif /* ENABLE_THREAD_SAFETY */
diff --git a/src/interfaces/ecpg/test/expected/thread-thread_implicit.stdout b/src/interfaces/ecpg/test/expected/thread-thread_implicit.stdout
index a9d787cc55cbb2d168495737dd11410b74e58b04..75fe16bb36bb2580d8365d62780f9c21ad7c259c 100644
--- a/src/interfaces/ecpg/test/expected/thread-thread_implicit.stdout
+++ b/src/interfaces/ecpg/test/expected/thread-thread_implicit.stdout
@@ -1 +1 @@
-Success.
+No threading enabled.
diff --git a/src/interfaces/ecpg/test/pg_regress.sh b/src/interfaces/ecpg/test/pg_regress.sh
index 8b748bf8013aeaf5a9a43f9c77148b4373222b10..a164953ed8c91d29c050d49a651b449ab330186e 100644
--- a/src/interfaces/ecpg/test/pg_regress.sh
+++ b/src/interfaces/ecpg/test/pg_regress.sh
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $PostgreSQL: pgsql/src/interfaces/ecpg/test/pg_regress.sh,v 1.18 2007/01/21 09:23:29 petere Exp $
+# $PostgreSQL: pgsql/src/interfaces/ecpg/test/pg_regress.sh,v 1.19 2007/03/29 12:02:24 meskes Exp $
 
 me=`basename $0`
 
@@ -34,6 +34,7 @@ Options for \`temp-install' mode:
   --top-builddir=DIR        (relative) path to top level build directory
   --temp-port=PORT          port number to start temp postmaster on
   --listen-on-tcp           listen on the tcp port as well
+  --enable-threading	    expect threading to be enabled	
 
 Options for using an existing installation:
   --host=HOST               use postmaster running on HOST
@@ -78,6 +79,7 @@ init_vars(){
 	temp_port=65432
 	load_langs=""
 	listen_on_tcp=no
+	enable_threading=no
 
 	: ${GMAKE='@GMAKE@'}
 }
@@ -108,6 +110,9 @@ do
         --listen-on-tcp)
                 listen_on_tcp=yes
                 shift;;
+	--enable-threading)
+		enable_threading=yes
+		shift;;
         --load-language=*)
                 lang=`expr "x$1" : "x--load-language=\(.*\)"`
                 load_langs="$load_langs $lang"
@@ -751,14 +756,21 @@ for i in \
 	cat "$outfile_source.tmp" | sed -e 's,^\(#line [0-9]*\) ".*/\([^/]*\)",\1 "\2",' > "$outfile_source"
 	rm "$outfile_source.tmp"
 
+	if [ "$enable_threading" = yes ] && [ "${i%%/*}" = "thread" ]; then
+		expectedoutprg="expected/$outprg-thread"
+	else
+		expectedoutprg="expected/$outprg"
+	fi
+
+	expected_stdout="$expectedoutprg$PLATFORM_TAG.stdout"
+	if [ ! -f "$expected_stdout" ]; then
+		expected_stdout="$expectedoutprg.stdout"
+	fi
+	# threading has log output disabled
 	expected_stderr="expected/$outprg$PLATFORM_TAG.stderr"
 	if [ ! -f "$expected_stderr" ]; then
 		expected_stderr="expected/$outprg.stderr"
 	fi
-	expected_stdout="expected/$outprg$PLATFORM_TAG.stdout"
-	if [ ! -f "$expected_stdout" ]; then
-		expected_stdout="expected/$outprg.stdout"
-	fi
 	# the source should be identical on all platforms
 	expected_source="expected/$outprg.c"
 
diff --git a/src/interfaces/ecpg/test/thread/thread.pgc b/src/interfaces/ecpg/test/thread/thread.pgc
index 58763564abf1b5eb5382d8f14896823a011c1e66..e7f0b4d1dcaa69f868a78ceaaed5ba6dbfec25bf 100644
--- a/src/interfaces/ecpg/test/thread/thread.pgc
+++ b/src/interfaces/ecpg/test/thread/thread.pgc
@@ -3,19 +3,18 @@
  *	by Philip Yarra & Lee Kindness.
  */
 #include <stdlib.h>
+#include "ecpg_config.h"
+
 #ifndef ENABLE_THREAD_SAFETY
 int
 main(void)
 {
-	printf("Success.\n");
+	printf("No threading enabled.\n");
 	return 0;
 }
 #else
 #include <pthread.h>
 
-#undef DEBUG
-
-
 exec sql include ../regression;
 
 void *test_thread(void *arg);
@@ -31,11 +30,9 @@ int main(int argc, char *argv[])
   int l_rows;
   EXEC SQL END DECLARE SECTION;
 
-
- /* Switch off debug output for regression tests. The threads get executed in
+ /* Do not switch on debug output for regression tests. The threads get executed in
   * more or less random order */
-  ECPGdebug(0, stderr);
-
+ /* ECPGdebug(1, stderr); */
 
   /* setup test_thread table */
   EXEC SQL CONNECT TO REGRESSDB1;
@@ -103,24 +100,14 @@ void *test_thread(void *arg)
   /* insert into test_thread table */
   for( l_i = 1; l_i <= iterations; l_i++ )
     {
-#ifdef DEBUG
-      printf("%s: inserting %d\n", l_connection, l_i);
-#endif
       EXEC SQL AT :l_connection INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
-#ifdef DEBUG
-      if( sqlca.sqlcode == 0 )
-	printf("%s: insert done\n", l_connection);
-      else
+      if( sqlca.sqlcode != 0 )
 	printf("%s: ERROR: insert failed!\n", l_connection);
-#endif
     }
 
   /* all done */
   EXEC SQL AT :l_connection COMMIT;
   EXEC SQL DISCONNECT :l_connection;
-#ifdef DEBUG
-  printf("%s: done!\n", l_connection);
-#endif
   return( NULL );
 }
 #endif /* ENABLE_THREAD_SAFETY */
diff --git a/src/interfaces/ecpg/test/thread/thread_implicit.pgc b/src/interfaces/ecpg/test/thread/thread_implicit.pgc
index 9353cf14be63d5c582e6b99219b7a68540880484..e40338496002fcd5795f35f38f08c8f201e2fb15 100644
--- a/src/interfaces/ecpg/test/thread/thread_implicit.pgc
+++ b/src/interfaces/ecpg/test/thread/thread_implicit.pgc
@@ -4,19 +4,18 @@
  */
 
 #include <stdlib.h>
+#include "ecpg_config.h"
+
 #ifndef ENABLE_THREAD_SAFETY
 int
 main(void)
 {
-	printf("Success.\n");
+	printf("No threading enabled.\n");
 	return 0;
 }
 #else
 #include <pthread.h>
 
-#undef DEBUG
-
-
 exec sql include ../regression;
 
 void *test_thread(void *arg);
@@ -32,11 +31,9 @@ int main(int argc, char *argv[])
   int l_rows;
   EXEC SQL END DECLARE SECTION;
 
-
- /* Switch off debug output for regression tests. The threads get executed in
+ /* Do not switch on debug output for regression tests. The threads get executed in
   * more or less random order */
-  ECPGdebug(0, stderr);
-
+ /* ECPGdebug(1, stderr); */
 
   /* setup test_thread table */
   EXEC SQL CONNECT TO REGRESSDB1;
@@ -104,24 +101,14 @@ void *test_thread(void *arg)
   /* insert into test_thread table */
   for( l_i = 1; l_i <= iterations; l_i++ )
     {
-#ifdef DEBUG
-      printf("%s: inserting %d\n", l_connection, l_i);
-#endif
       EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
-#ifdef DEBUG
-      if( sqlca.sqlcode == 0 )
-	printf("%s: insert done\n", l_connection);
-      else
+      if( sqlca.sqlcode != 0 )
 	printf("%s: ERROR: insert failed!\n", l_connection);
-#endif
     }
 
   /* all done */
   EXEC SQL COMMIT;
   EXEC SQL DISCONNECT :l_connection;
-#ifdef DEBUG
-  printf("%s: done!\n", l_connection);
-#endif
   return( NULL );
 }
 #endif /* ENABLE_THREAD_SAFETY */