diff --git a/src/interfaces/ecpg/ChangeLog b/src/interfaces/ecpg/ChangeLog
index a7882c20a1e108dff3c987c8f398ec2b3e1f53c4..41ddeadf34e22799e06331bbaab377d14e2b2258 100644
--- a/src/interfaces/ecpg/ChangeLog
+++ b/src/interfaces/ecpg/ChangeLog
@@ -852,5 +852,10 @@ Thu Mar  2 17:42:16 CET 2000
 Fri Mar  3 10:47:06 CET 2000
 
 	- Fixed handling of double quote in C code.
+
+Tue Mar  7 10:58:21 CET 2000
+
+	- More cleanup in ecpglib.
+	- Fixed ecpg.c not not free variable list twice.
 	- Set library version to 3.1.0.
 	- Set ecpg version to 2.7.0.
diff --git a/src/interfaces/ecpg/include/ecpglib.h b/src/interfaces/ecpg/include/ecpglib.h
index 1e527ce369eaec80c40a475019466e1e83343472..03b09668ba9b42552f9c006dfe285bbed725e52c 100644
--- a/src/interfaces/ecpg/include/ecpglib.h
+++ b/src/interfaces/ecpg/include/ecpglib.h
@@ -24,22 +24,6 @@ extern		"C"
 	/* print an error message */
 	void		sqlprint(void);
 	
-#ifdef LIBPQ_FE_H
-	bool		ECPGsetdb(PGconn *);
-#endif
-
-/* Here are some methods used by the lib. */
-/* Returns a pointer to a string containing a simple type name. */
-	bool get_data(PGresult *, int, int, int, enum ECPGttype type,
-			enum ECPGttype, void *, void *, long, long, bool);
-	char *ecpg_alloc(long, int);
-	char *ecpg_strdup(const char *, int);
-	const char *ECPGtype_name(enum ECPGttype);
-	unsigned int ECPGDynamicType(Oid);
-	
-/* and some vars */
-	extern struct auto_mem *auto_allocs;
-
 /* define this for simplicity as well as compatibility */
 
 #define		  SQLCODE	 sqlca.sqlcode
@@ -59,5 +43,3 @@ extern		"C"
 }
 
 #endif
-
-#include <ecpgerrno.h>
diff --git a/src/interfaces/ecpg/include/ecpgtype.h b/src/interfaces/ecpg/include/ecpgtype.h
index 69c0e77a4e78011a5fbf2d9cd8da5e8974a03d70..40a944856d45a395e8af4812acea578f0cab3364 100644
--- a/src/interfaces/ecpg/include/ecpgtype.h
+++ b/src/interfaces/ecpg/include/ecpgtype.h
@@ -73,33 +73,6 @@ extern		"C"
 
 #define IS_SIMPLE_TYPE(type) ((type) >= ECPGt_char && (type) <= ECPGt_varchar2)
 
-	/* A generic varchar type. */
-	struct ECPGgeneric_varchar
-	{
-		int			len;
-		char		arr[1];
-	};
-
-/* keep a list of memory we allocated for the user */
-	struct auto_mem
-	{
-        	void       *pointer;
-                struct auto_mem *next;
-        };
-        
-/* structure to store one statement */
-        struct statement
-        {
-                int                     lineno;
-                char       *command;
-                struct connection *connection;
-                struct variable *inlist;
-                struct variable *outlist;
-        };
-                                                
-                
-/* define this for simplicity as well as compatibility */
-
 #ifdef __cplusplus
 }
 
diff --git a/src/interfaces/ecpg/lib/Makefile.in b/src/interfaces/ecpg/lib/Makefile.in
index 1640d970633d588bb8809672a8e8fac111f940bb..a462030ff5bb0338c30f097d13dd4cd9a1137a75 100644
--- a/src/interfaces/ecpg/lib/Makefile.in
+++ b/src/interfaces/ecpg/lib/Makefile.in
@@ -6,7 +6,7 @@
 # Copyright (c) 1994, Regents of the University of California
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/interfaces/ecpg/lib/Attic/Makefile.in,v 1.61 2000/02/25 11:11:15 meskes Exp $
+#    $Header: /cvsroot/pgsql/src/interfaces/ecpg/lib/Attic/Makefile.in,v 1.62 2000/03/07 15:10:52 meskes Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -23,7 +23,8 @@ ifdef KRBVERS
 CFLAGS+= $(KRBFLAGS)
 endif
 
-OBJS= ecpglib.o typename.o descriptor.o data.o error.o prepare.o memory.o
+OBJS= execute.o typename.o descriptor.o data.o error.o prepare.o memory.o \
+	connect.o misc.o
 
 SHLIB_LINK= -L../../libpq -lpq
 
diff --git a/src/interfaces/ecpg/lib/connect.c b/src/interfaces/ecpg/lib/connect.c
new file mode 100644
index 0000000000000000000000000000000000000000..0a4d915c3e932f8039023f40382cd00ec22b4b8d
--- /dev/null
+++ b/src/interfaces/ecpg/lib/connect.c
@@ -0,0 +1,180 @@
+#include <ecpgtype.h>
+#include <ecpglib.h>
+#include <ecpgerrno.h>
+#include "extern.h"
+#include <sqlca.h>
+
+static struct connection *all_connections = NULL, *actual_connection = NULL;
+
+struct connection *
+get_connection(const char *connection_name)
+{
+	struct connection *con = all_connections;
+
+	if (connection_name == NULL || strcmp(connection_name, "CURRENT") == 0)
+		return actual_connection;
+
+	for (; con && strcmp(connection_name, con->name) != 0; con = con->next);
+	if (con)
+		return con;
+	else
+		return NULL;
+}
+
+static void
+ecpg_finish(struct connection * act)
+{
+	if (act != NULL)
+	{
+		ECPGlog("ecpg_finish: finishing %s.\n", act->name);
+		PQfinish(act->connection);
+
+		/* remove act from the list */
+		if (act == all_connections)
+			all_connections = act->next;
+		else
+		{
+			struct connection *con;
+
+			for (con = all_connections; con->next && con->next != act; con = con->next);
+			if (con->next)
+				con->next = act->next;
+		}
+
+		if (actual_connection == act)
+			actual_connection = all_connections;
+
+		free(act->name);
+		free(act);
+	}
+	else
+		ECPGlog("ecpg_finish: called an extra time.\n");
+}
+
+bool
+ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
+{
+	struct connection *con = get_connection(connection_name);
+	PGresult   *results;
+
+	if (!ecpg_init(con, connection_name, lineno))
+		return(false);
+
+	ECPGlog("ECPGsetcommit line %d action = %s connection = %s\n", lineno, mode, con->name);
+	
+	if (con->autocommit == true && strncmp(mode, "off", strlen("off")) == 0)
+	{
+		if (con->committed)
+		{
+			if ((results = PQexec(con->connection, "begin transaction")) == NULL)
+			{
+				ECPGraise(lineno, ECPG_TRANS, NULL);
+				return false;
+			}
+			PQclear(results);
+			con->committed = false;
+		}
+		con->autocommit = false;
+	}
+	else if (con->autocommit == false && strncmp(mode, "on", strlen("on")) == 0)
+	{
+		if (!con->committed)
+		{
+			if ((results = PQexec(con->connection, "commit")) == NULL)
+			{
+				ECPGraise(lineno, ECPG_TRANS, NULL);
+				return false;
+			}
+			PQclear(results);
+			con->committed = true;
+		}
+		con->autocommit = true;
+	}
+
+	return true;
+}
+
+bool
+ECPGsetconn(int lineno, const char *connection_name)
+{
+	struct connection *con = get_connection(connection_name);
+
+	if (!ecpg_init(con, connection_name, lineno))
+		return(false);
+
+	actual_connection = con;
+	return true;
+}
+
+bool
+ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd, const char *connection_name, int autocommit)
+{
+	struct connection *this;
+
+	init_sqlca();
+	
+	if ((this = (struct connection *) ecpg_alloc(sizeof(struct connection), lineno)) == NULL)
+		return false;
+
+	if (dbname == NULL && connection_name == NULL)
+		connection_name = "DEFAULT";
+
+	/* add connection to our list */
+	if (connection_name != NULL)
+		this->name = ecpg_strdup(connection_name, lineno);
+	else
+		this->name = ecpg_strdup(dbname, lineno);
+
+	if (all_connections == NULL)
+		this->next = NULL;
+	else
+		this->next = all_connections;
+
+	actual_connection = all_connections = this;
+
+	ECPGlog("ECPGconnect: opening database %s %s%s\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "");
+
+	this->connection = PQsetdbLogin(NULL, NULL, NULL, NULL, dbname, user, passwd);
+
+	if (PQstatus(this->connection) == CONNECTION_BAD)
+	{
+		ecpg_finish(this);
+		ECPGlog("connect: could not open database %s %s%s in line %d\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "", lineno);
+		ECPGraise(lineno, ECPG_CONNECT, dbname ? dbname : "<DEFAULT>");
+		return false;
+	}
+
+	this->committed = true;
+	this->autocommit = autocommit;
+
+	return true;
+}
+
+bool
+ECPGdisconnect(int lineno, const char *connection_name)
+{
+	struct connection *con;
+
+	if (strcmp(connection_name, "ALL") == 0)
+	{
+		init_sqlca();
+		for (con = all_connections; con;)
+		{
+			struct connection *f = con;
+
+			con = con->next;
+			ecpg_finish(f);
+		}
+	}
+	else
+	{
+		con = get_connection(connection_name);
+
+		if (!ecpg_init(con, connection_name, lineno))
+		        return(false);
+		else
+			ecpg_finish(con);
+	}
+
+	return true;
+}
diff --git a/src/interfaces/ecpg/lib/data.c b/src/interfaces/ecpg/lib/data.c
index af3c78ce591d4d57e38c655c2570a49210df19e5..e9b01c385e940a0c335c32c0ad2c90ab902be8a2 100644
--- a/src/interfaces/ecpg/lib/data.c
+++ b/src/interfaces/ecpg/lib/data.c
@@ -1,8 +1,9 @@
 #include <stdlib.h>
 
-#include <libpq/pqcomm.h>
 #include <ecpgtype.h>
 #include <ecpglib.h>
+#include <ecpgerrno.h>
+#include "extern.h"
 #include <sqlca.h>
 
 bool
diff --git a/src/interfaces/ecpg/lib/descriptor.c b/src/interfaces/ecpg/lib/descriptor.c
index a27915ce5c129ad076e4bbddb63ef052b03b403d..b1fe2b79d0498dc6e88b62fe66f2b4d5727a36a8 100644
--- a/src/interfaces/ecpg/lib/descriptor.c
+++ b/src/interfaces/ecpg/lib/descriptor.c
@@ -1,6 +1,7 @@
 #include <ecpgtype.h>
 #include <ecpglib.h>
-
+#include <ecpgerrno.h>
+#include "extern.h"
 #include <sql3types.h>
 
 struct descriptor
diff --git a/src/interfaces/ecpg/lib/error.c b/src/interfaces/ecpg/lib/error.c
index 54e48a55271aa8e44b8b6fa43a5efb38c325ec68..5aa4b771566b3f7f35e4589acfa7fac367cbdf12 100644
--- a/src/interfaces/ecpg/lib/error.c
+++ b/src/interfaces/ecpg/lib/error.c
@@ -3,13 +3,12 @@
 #include <ecpgerrno.h>
 #include <ecpgtype.h>
 #include <ecpglib.h>
+#include "extern.h"
 #include <sqlca.h>
 
 void
 ECPGraise(int line, int code, const char *str)
 {
-	struct auto_mem *am;
-	       
 	sqlca.sqlcode = code;
 	switch (code)
 	{ 
@@ -142,14 +141,7 @@ ECPGraise(int line, int code, const char *str)
 	sqlca.sqlerrm.sqlerrml = strlen(sqlca.sqlerrm.sqlerrmc);
 	
         /* free all memory we have allocated for the user */
-        for (am = auto_allocs; am;)
-        {
-        	struct auto_mem *act = am;
-	        
-	        am = am->next;
-	        free(act->pointer);
-	        free(act);
-	}
+        free_auto_mem();
 }
 
 /* print out an error message */
diff --git a/src/interfaces/ecpg/lib/ecpglib.c b/src/interfaces/ecpg/lib/execute.c
similarity index 79%
rename from src/interfaces/ecpg/lib/ecpglib.c
rename to src/interfaces/ecpg/lib/execute.c
index fddb36c3caa26973064eb1b535ee70c366c74bb9..adc1351e4e7aa145f3e5e1fc37cf4400cadcbb05 100644
--- a/src/interfaces/ecpg/lib/ecpglib.c
+++ b/src/interfaces/ecpg/lib/execute.c
@@ -13,32 +13,16 @@
    on Feb. 5th, 1998 */
 
 #include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdarg.h>
-#include <string.h>
-#include <ctype.h>
 #include <locale.h>
 
-#include <libpq/pqcomm.h>
 #include <ecpgtype.h>
 #include <ecpglib.h>
+#include <ecpgerrno.h>
+#include "extern.h"
 #include <sqlca.h>
 #include <sql3types.h>
 
 /* variables visible to the programs */
-static struct sqlca sqlca_init =
-{
-	{'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},
-	sizeof(struct sqlca),
-	0,
-	{0, {0}},
-	{'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '},
-	{0, 0, 0, 0, 0, 0},
-	{0, 0, 0, 0, 0, 0, 0, 0},
-	{0, 0, 0, 0, 0, 0, 0, 0}
-};
-
 struct sqlca sqlca =
 {
 	{'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},
@@ -51,15 +35,6 @@ struct sqlca sqlca =
 	{0, 0, 0, 0, 0, 0, 0, 0}
 };
 
-static struct connection
-{
-	char	   *name;
-	PGconn	   *connection;
-	bool		committed;
-	int			autocommit;
-	struct connection *next;
-}		   *all_connections = NULL, *actual_connection = NULL;
-
 struct variable
 {
 	enum ECPGttype type;
@@ -76,69 +51,12 @@ struct variable
 	struct variable *next;
 };
 
-struct auto_mem *auto_allocs;
-
-static int	simple_debug = 0;
-static FILE *debugstream = NULL;
-
-static struct connection *
-get_connection(const char *connection_name)
-{
-	struct connection *con = all_connections;
-
-	if (connection_name == NULL || strcmp(connection_name, "CURRENT") == 0)
-		return actual_connection;
-
-	for (; con && strcmp(connection_name, con->name) != 0; con = con->next);
-	if (con)
-		return con;
-	else
-		return NULL;
-}
-
-static bool
-ecpg_init(const struct connection *con, const char * connection_name, const int lineno)
-{
-	memcpy((char *) &sqlca, (char *) &sqlca_init, sizeof(sqlca));
-	if (con == NULL)
-	{
-		ECPGraise(lineno, ECPG_NO_CONN, connection_name ? connection_name : "NULL");
-		return (false);
-	}
-	
-	auto_allocs = NULL;
-	
-	return (true);
-}
-
-static void
-ecpg_finish(struct connection * act)
+/* keep a list of memory we allocated for the user */
+static struct auto_mem
 {
-	if (act != NULL)
-	{
-		ECPGlog("ecpg_finish: finishing %s.\n", act->name);
-		PQfinish(act->connection);
-		/* remove act from the list */
-		if (act == all_connections)
-			all_connections = act->next;
-		else
-		{
-			struct connection *con;
-
-			for (con = all_connections; con->next && con->next != act; con = con->next);
-			if (con->next)
-				con->next = act->next;
-		}
-
-		if (actual_connection == act)
-			actual_connection = all_connections;
-
-		free(act->name);
-		free(act);
-	}
-	else
-		ECPGlog("ecpg_finish: called an extra time.\n");
-}
+       	void       *pointer;
+        struct auto_mem *next;
+} *auto_allocs = NULL;
 
 static void
 add_mem(void *ptr, int lineno)
@@ -149,6 +67,23 @@ add_mem(void *ptr, int lineno)
 	auto_allocs = am;
 }
 
+void free_auto_mem(void)
+{
+	struct auto_mem *am;
+	
+        /* free all memory we have allocated for the user */
+        for (am = auto_allocs; am;)
+        {
+        	struct auto_mem *act = am;
+	        
+	        am = am->next;
+	        free(act->pointer);
+	        free(act);
+	}
+	
+	auto_allocs = NULL;
+}
+
 /* This function returns a newly malloced string that has the  \
    in the argument quoted with \ and the ' quote with ' as SQL92 says.
  */
@@ -875,223 +810,11 @@ ECPGdo(int lineno, const char *connection_name, char *query, ...)
 	return (status);
 }
 
-bool
-ECPGstatus(int lineno, const char *connection_name)
-{
-	struct connection *con = get_connection(connection_name);
-
-	if (!ecpg_init(con, connection_name, lineno))
-		return(false);
-
-	/* are we connected? */
-	if (con->connection == NULL)
-	{
-		ECPGlog("ECPGdo: not connected to %s\n", con->name);
-		ECPGraise(lineno, ECPG_NOT_CONN, NULL);
-		return false;
-	}
-
-	return (true);
-}
-
-bool
-ECPGtrans(int lineno, const char *connection_name, const char *transaction)
-{
-	PGresult   *res;
-	struct connection *con = get_connection(connection_name);
-
-	if (!ecpg_init(con, connection_name, lineno))
-		return(false);
-
-	ECPGlog("ECPGtrans line %d action = %s connection = %s\n", lineno, transaction, con->name);
-
-	/* if we have no connection we just simulate the command */
-	if (con && con->connection)
-	{
-		if ((res = PQexec(con->connection, transaction)) == NULL)
-		{
-			ECPGraise(lineno, ECPG_TRANS, NULL);
-			return FALSE;
-		}
-		PQclear(res);
-	}
-	
-	if (strcmp(transaction, "commit") == 0 || strcmp(transaction, "rollback") == 0)
-	{
-		con->committed = true;
-
-		/* deallocate all prepared statements */
-		if (!ECPGdeallocate_all(lineno))
-				return false;
-	}
-
-	return true;
-}
-
-bool
-ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
-{
-	struct connection *con = get_connection(connection_name);
-	PGresult   *results;
-
-	if (!ecpg_init(con, connection_name, lineno))
-		return(false);
-
-	ECPGlog("ECPGsetcommit line %d action = %s connection = %s\n", lineno, mode, con->name);
-	
-	if (con->autocommit == true && strncmp(mode, "off", strlen("off")) == 0)
-	{
-		if (con->committed)
-		{
-			if ((results = PQexec(con->connection, "begin transaction")) == NULL)
-			{
-				ECPGraise(lineno, ECPG_TRANS, NULL);
-				return false;
-			}
-			PQclear(results);
-			con->committed = false;
-		}
-		con->autocommit = false;
-	}
-	else if (con->autocommit == false && strncmp(mode, "on", strlen("on")) == 0)
-	{
-		if (!con->committed)
-		{
-			if ((results = PQexec(con->connection, "commit")) == NULL)
-			{
-				ECPGraise(lineno, ECPG_TRANS, NULL);
-				return false;
-			}
-			PQclear(results);
-			con->committed = true;
-		}
-		con->autocommit = true;
-	}
-
-	return true;
-}
-
-bool
-ECPGsetconn(int lineno, const char *connection_name)
-{
-	struct connection *con = get_connection(connection_name);
-
-	if (!ecpg_init(con, connection_name, lineno))
-		return(false);
-
-	actual_connection = con;
-	return true;
-}
-
-bool
-ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd, const char *connection_name, int autocommit)
-{
-	struct connection *this;
-
-
-	memcpy((char *) &sqlca, (char *) &sqlca_init, sizeof(sqlca));
-	
-	if ((this = (struct connection *) ecpg_alloc(sizeof(struct connection), lineno)) == NULL)
-		return false;
-
-	if (dbname == NULL && connection_name == NULL)
-		connection_name = "DEFAULT";
-
-	/* add connection to our list */
-	if (connection_name != NULL)
-		this->name = ecpg_strdup(connection_name, lineno);
-	else
-		this->name = ecpg_strdup(dbname, lineno);
-
-	if (all_connections == NULL)
-		this->next = NULL;
-	else
-		this->next = all_connections;
-
-	actual_connection = all_connections = this;
-
-	ECPGlog("ECPGconnect: opening database %s %s%s\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "");
-
-	this->connection = PQsetdbLogin(NULL, NULL, NULL, NULL, dbname, user, passwd);
-
-	if (PQstatus(this->connection) == CONNECTION_BAD)
-	{
-		ecpg_finish(this);
-		ECPGlog("connect: could not open database %s %s%s in line %d\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "", lineno);
-		ECPGraise(lineno, ECPG_CONNECT, dbname ? dbname : "<DEFAULT>");
-		return false;
-	}
-
-	this->committed = true;
-	this->autocommit = autocommit;
-
-	return true;
-}
-
-bool
-ECPGdisconnect(int lineno, const char *connection_name)
-{
-	struct connection *con;
-
-	if (strcmp(connection_name, "ALL") == 0)
-	{
-		memcpy((char *) &sqlca, (char *) &sqlca_init, sizeof(sqlca));
-		for (con = all_connections; con;)
-		{
-			struct connection *f = con;
-
-			con = con->next;
-			ecpg_finish(f);
-		}
-	}
-	else
-	{
-		con = get_connection(connection_name);
-
-		if (!ecpg_init(con, connection_name, lineno))
-		        return(false);
-		else
-			ecpg_finish(con);
-	}
-
-	return true;
-}
-
-void
-ECPGdebug(int n, FILE *dbgs)
-{
-	simple_debug = n;
-	debugstream = dbgs;
-	ECPGlog("ECPGdebug: set to %d\n", simple_debug);
-}
-
-void
-ECPGlog(const char *format,...)
-{
-	va_list		ap;
-
-	if (simple_debug)
-	{
-		char	   *f = (char *) malloc(strlen(format) + 100);
-
-		if (!f)
-			return;
-
-		sprintf(f, "[%d]: %s", (int) getpid(), format);
-
-		va_start(ap, format);
-		vfprintf(debugstream, f, ap);
-		va_end(ap);
-
-		free(f);
-	}
-}
-
 /* dynamic SQL support routines
  *
  * Copyright (c) 2000, Christof Petig <christof.petig@wtal.de>
  *
- * $Header: /cvsroot/pgsql/src/interfaces/ecpg/lib/Attic/ecpglib.c,v 1.62 2000/03/03 14:39:26 meskes Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/ecpg/lib/Attic/execute.c,v 1.1 2000/03/07 15:10:56 meskes Exp $
  */
 
 PGconn *ECPG_internal_get_connection(char *name);
diff --git a/src/interfaces/ecpg/lib/extern.h b/src/interfaces/ecpg/lib/extern.h
new file mode 100644
index 0000000000000000000000000000000000000000..ef9515d7c964d382fe52ee63094adaa4d9b96d2a
--- /dev/null
+++ b/src/interfaces/ecpg/lib/extern.h
@@ -0,0 +1,43 @@
+#include <postgres.h>
+#include <libpq-fe.h>
+
+/* Here are some methods used by the lib. */
+/* Returns a pointer to a string containing a simple type name. */
+void free_auto_mem(void);
+bool get_data(PGresult *, int, int, int, enum ECPGttype type,
+		enum ECPGttype, void *, void *, long, long, bool);
+struct connection *get_connection(const char *);
+void init_sqlca(void);
+char *ecpg_alloc(long, int);
+bool ecpg_init(const struct connection *, const char *, const int);
+char *ecpg_strdup(const char *, int);
+const char *ECPGtype_name(enum ECPGttype);
+unsigned int ECPGDynamicType(Oid);
+
+/* A generic varchar type. */
+struct ECPGgeneric_varchar
+{
+	int			len;
+	char		arr[1];
+};
+
+/* structure to store one statement */
+struct statement
+{
+        int                     lineno;
+        char       *command;
+        struct connection *connection;
+        struct variable *inlist;
+        struct variable *outlist;
+};
+
+/* structure to store connections */
+struct connection
+{
+       	char            *name;
+       	PGconn          *connection;
+        bool            committed;
+        int             autocommit;
+        struct connection *next;
+};
+                
diff --git a/src/interfaces/ecpg/lib/memory.c b/src/interfaces/ecpg/lib/memory.c
index 61c5d299f3767ee39a2b44ea8248440b5e7b58ee..463dad129fbd6654b88526aa51dcf601647e02c5 100644
--- a/src/interfaces/ecpg/lib/memory.c
+++ b/src/interfaces/ecpg/lib/memory.c
@@ -1,5 +1,7 @@
 #include <ecpgtype.h>
 #include <ecpglib.h>
+#include <ecpgerrno.h>
+#include "extern.h"
 
 char *
 ecpg_alloc(long size, int lineno)
diff --git a/src/interfaces/ecpg/lib/misc.c b/src/interfaces/ecpg/lib/misc.c
new file mode 100644
index 0000000000000000000000000000000000000000..55e0cfc81775aba9a0efbb74fe24f0412d4a2cf6
--- /dev/null
+++ b/src/interfaces/ecpg/lib/misc.c
@@ -0,0 +1,124 @@
+#include <unistd.h>
+#include <ecpgtype.h>
+#include <ecpglib.h>
+#include <ecpgerrno.h>
+#include "extern.h"
+#include <sqlca.h>
+
+static struct sqlca sqlca_init =
+{
+	{'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},
+	sizeof(struct sqlca),
+	0,
+	{0, {0}},
+	{'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '},
+	{0, 0, 0, 0, 0, 0},
+	{0, 0, 0, 0, 0, 0, 0, 0},
+	{0, 0, 0, 0, 0, 0, 0, 0}
+};
+
+static int	simple_debug = 0;
+static FILE *debugstream = NULL;
+
+void
+init_sqlca(void)
+{
+	memcpy((char *) &sqlca, (char *) &sqlca_init, sizeof(sqlca));
+}
+
+bool
+ecpg_init(const struct connection *con, const char * connection_name, const int lineno)
+{
+	init_sqlca();
+	if (con == NULL)
+	{
+		ECPGraise(lineno, ECPG_NO_CONN, connection_name ? connection_name : "NULL");
+		return (false);
+	}
+	
+	return (true);
+}
+
+bool
+ECPGstatus(int lineno, const char *connection_name)
+{
+	struct connection *con = get_connection(connection_name);
+
+	if (!ecpg_init(con, connection_name, lineno))
+		return(false);
+
+	/* are we connected? */
+	if (con->connection == NULL)
+	{
+		ECPGlog("ECPGdo: not connected to %s\n", con->name);
+		ECPGraise(lineno, ECPG_NOT_CONN, NULL);
+		return false;
+	}
+
+	return (true);
+}
+
+bool
+ECPGtrans(int lineno, const char *connection_name, const char *transaction)
+{
+	PGresult   *res;
+	struct connection *con = get_connection(connection_name);
+
+	if (!ecpg_init(con, connection_name, lineno))
+		return(false);
+
+	ECPGlog("ECPGtrans line %d action = %s connection = %s\n", lineno, transaction, con->name);
+
+	/* if we have no connection we just simulate the command */
+	if (con && con->connection)
+	{
+		if ((res = PQexec(con->connection, transaction)) == NULL)
+		{
+			ECPGraise(lineno, ECPG_TRANS, NULL);
+			return FALSE;
+		}
+		PQclear(res);
+	}
+	
+	if (strcmp(transaction, "commit") == 0 || strcmp(transaction, "rollback") == 0)
+	{
+		con->committed = true;
+
+		/* deallocate all prepared statements */
+		if (!ECPGdeallocate_all(lineno))
+				return false;
+	}
+
+	return true;
+}
+
+
+void
+ECPGdebug(int n, FILE *dbgs)
+{
+	simple_debug = n;
+	debugstream = dbgs;
+	ECPGlog("ECPGdebug: set to %d\n", simple_debug);
+}
+
+void
+ECPGlog(const char *format,...)
+{
+	va_list		ap;
+
+	if (simple_debug)
+	{
+		char	   *f = (char *) malloc(strlen(format) + 100);
+
+		if (!f)
+			return;
+
+		sprintf(f, "[%d]: %s", (int) getpid(), format);
+
+		va_start(ap, format);
+		vfprintf(debugstream, f, ap);
+		va_end(ap);
+
+		free(f);
+	}
+}
diff --git a/src/interfaces/ecpg/lib/prepare.c b/src/interfaces/ecpg/lib/prepare.c
index 1b78aef6039d8eb421edb68775ca88c05dfcd409..2155177edec0615e8f15616b4f7fbe6ace6ce5bb 100644
--- a/src/interfaces/ecpg/lib/prepare.c
+++ b/src/interfaces/ecpg/lib/prepare.c
@@ -2,6 +2,8 @@
 
 #include <ecpgtype.h>
 #include <ecpglib.h>
+#include <ecpgerrno.h>
+#include "extern.h"
 #include <sqlca.h>
 
 static struct prepared_statement
diff --git a/src/interfaces/ecpg/lib/typename.c b/src/interfaces/ecpg/lib/typename.c
index 1999ab82d7519ed124402c8948259f3c00131a51..2b64c19a8ce20c3ac78acb48ed84b614ab7ad513 100644
--- a/src/interfaces/ecpg/lib/typename.c
+++ b/src/interfaces/ecpg/lib/typename.c
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <ecpgtype.h>
 #include <ecpglib.h>
+#include "extern.h"
 #include <sql3types.h>
 
 /*
diff --git a/src/interfaces/ecpg/preproc/ecpg.c b/src/interfaces/ecpg/preproc/ecpg.c
index 1c14fbbc0c22587250eeabeb97e26922f40f7cc6..ceb06fb744e6355e991ef32e9e0407d2eb548fed 100644
--- a/src/interfaces/ecpg/preproc/ecpg.c
+++ b/src/interfaces/ecpg/preproc/ecpg.c
@@ -249,7 +249,7 @@ main(int argc, char *const argv[])
 				lex_init();
 
 				/* we need two includes */
-				fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n#line 1 \"%s\"\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename);
+				fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These three include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#line 1 \"%s\"\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename);
 
 				/* and parse the source */
 				yyparse();
diff --git a/src/interfaces/ecpg/preproc/extern.h b/src/interfaces/ecpg/preproc/extern.h
index 1134e42c7ee7cb1cf80ac3352849c245237febac..f79c3b43a0c0fc6f8ccc1af10dd045f4fb0028d8 100644
--- a/src/interfaces/ecpg/preproc/extern.h
+++ b/src/interfaces/ecpg/preproc/extern.h
@@ -39,7 +39,7 @@ extern const char *get_dtype(enum ECPGdtype);
 extern void lex_init(void);
 extern char *make_str(const char *);
 extern void output_line_number(void);
-extern void output_statement(char *, int, char *, char *, struct arguments *, struct arguments *);
+extern void output_statement(char *, int, char *, char *);
 extern void output_simple_statement(char *);
 extern char *hashline_number(void);
 extern int	yyparse(void);
diff --git a/src/interfaces/ecpg/preproc/output.c b/src/interfaces/ecpg/preproc/output.c
index d67f9eae5af7d477e72d7dd204aa8fb35376c0ae..760751affd3a6bd48effda2b4643ea73ee529a9b 100644
--- a/src/interfaces/ecpg/preproc/output.c
+++ b/src/interfaces/ecpg/preproc/output.c
@@ -94,8 +94,7 @@ hashline_number(void)
 }
 
 void
-output_statement(char * stmt, int mode, char *descriptor,
-	char *con, struct arguments *insert, struct arguments *result)
+output_statement(char * stmt, int mode, char *descriptor, char *con)
 {
 	int i, j = strlen(stmt);
 
@@ -118,10 +117,11 @@ output_statement(char * stmt, int mode, char *descriptor,
 		fputs("\", ", yyout);
 		
 		/* dump variables to C file */
-		dump_variables(insert, 1);
+		dump_variables(argsinsert, 1);
 		fputs("ECPGt_EOIT, ", yyout);
-		dump_variables(result, 1);
+		dump_variables(argsresult, 1);
 		fputs("ECPGt_EORT);", yyout);
+		reset_variables();
 	}
 	else
 		fputs("\");", yyout);
diff --git a/src/interfaces/ecpg/preproc/preproc.y b/src/interfaces/ecpg/preproc/preproc.y
index 88591f831bc30ad5b16bed954a2ae9d356552d0a..e21a67b0800133da054cb1d2a97b0309cdf8f00c 100644
--- a/src/interfaces/ecpg/preproc/preproc.y
+++ b/src/interfaces/ecpg/preproc/preproc.y
@@ -392,64 +392,64 @@ statement: ecpgstart opt_at stmt ';'	{ connection = NULL; }
 
 opt_at:	SQL_AT connection_target	{ connection = $2; }
 
-stmt:  AlterTableStmt			{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| AlterGroupStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| AlterUserStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ClosePortalStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CommentStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CopyStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreateStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreateAsStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreateGroupStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreateSeqStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreatePLangStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreateTrigStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreateUserStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-  		| ClusterStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| DefineStmt 		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| DropStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| TruncateStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| DropGroupStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| DropPLangStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| DropTrigStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| DropUserStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ExtendStmt 		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ExplainStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| FetchStmt		{ output_statement($1, 1, NULL, connection, argsinsert, argsresult); }
-		| GrantStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| IndexStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ListenStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| UnlistenStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| LockStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ProcedureStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ReindexStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| RemoveAggrStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| RemoveOperStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| RemoveFuncStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| RemoveStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| RenameStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| RevokeStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
+stmt:  AlterTableStmt			{ output_statement($1, 0, NULL, connection); }
+		| AlterGroupStmt	{ output_statement($1, 0, NULL, connection); }
+		| AlterUserStmt		{ output_statement($1, 0, NULL, connection); }
+		| ClosePortalStmt	{ output_statement($1, 0, NULL, connection); }
+		| CommentStmt		{ output_statement($1, 0, NULL, connection); }
+		| CopyStmt		{ output_statement($1, 0, NULL, connection); }
+		| CreateStmt		{ output_statement($1, 0, NULL, connection); }
+		| CreateAsStmt		{ output_statement($1, 0, NULL, connection); }
+		| CreateGroupStmt	{ output_statement($1, 0, NULL, connection); }
+		| CreateSeqStmt		{ output_statement($1, 0, NULL, connection); }
+		| CreatePLangStmt	{ output_statement($1, 0, NULL, connection); }
+		| CreateTrigStmt	{ output_statement($1, 0, NULL, connection); }
+		| CreateUserStmt	{ output_statement($1, 0, NULL, connection); }
+  		| ClusterStmt		{ output_statement($1, 0, NULL, connection); }
+		| DefineStmt 		{ output_statement($1, 0, NULL, connection); }
+		| DropStmt		{ output_statement($1, 0, NULL, connection); }
+		| TruncateStmt		{ output_statement($1, 0, NULL, connection); }
+		| DropGroupStmt		{ output_statement($1, 0, NULL, connection); }
+		| DropPLangStmt		{ output_statement($1, 0, NULL, connection); }
+		| DropTrigStmt		{ output_statement($1, 0, NULL, connection); }
+		| DropUserStmt		{ output_statement($1, 0, NULL, connection); }
+		| ExtendStmt 		{ output_statement($1, 0, NULL, connection); }
+		| ExplainStmt		{ output_statement($1, 0, NULL, connection); }
+		| FetchStmt		{ output_statement($1, 1, NULL, connection); }
+		| GrantStmt		{ output_statement($1, 0, NULL, connection); }
+		| IndexStmt		{ output_statement($1, 0, NULL, connection); }
+		| ListenStmt		{ output_statement($1, 0, NULL, connection); }
+		| UnlistenStmt		{ output_statement($1, 0, NULL, connection); }
+		| LockStmt		{ output_statement($1, 0, NULL, connection); }
+		| ProcedureStmt		{ output_statement($1, 0, NULL, connection); }
+		| ReindexStmt		{ output_statement($1, 0, NULL, connection); }
+		| RemoveAggrStmt	{ output_statement($1, 0, NULL, connection); }
+		| RemoveOperStmt	{ output_statement($1, 0, NULL, connection); }
+		| RemoveFuncStmt	{ output_statement($1, 0, NULL, connection); }
+		| RemoveStmt		{ output_statement($1, 0, NULL, connection); }
+		| RenameStmt		{ output_statement($1, 0, NULL, connection); }
+		| RevokeStmt		{ output_statement($1, 0, NULL, connection); }
                 | OptimizableStmt	{
 						if (strncmp($1, "/* " , sizeof("/* ")-1) == 0)
 							output_simple_statement($1);
 						else
-							output_statement($1, 1, NULL, connection, argsinsert, argsresult);
+							output_statement($1, 1, NULL, connection);
 					}
-		| RuleStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
+		| RuleStmt		{ output_statement($1, 0, NULL, connection); }
 		| TransactionStmt	{
 						fprintf(yyout, "{ ECPGtrans(__LINE__, %s, \"%s\");", connection ? connection : "NULL", $1);
 						whenever_action(2);
 						free($1);
 					}
-		| ViewStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| LoadStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| CreatedbStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| DropdbStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| VacuumStmt		{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| VariableSetStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| VariableShowStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| VariableResetStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ConstraintsSetStmt	{ output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
+		| ViewStmt		{ output_statement($1, 0, NULL, connection); }
+		| LoadStmt		{ output_statement($1, 0, NULL, connection); }
+		| CreatedbStmt		{ output_statement($1, 0, NULL, connection); }
+		| DropdbStmt		{ output_statement($1, 0, NULL, connection); }
+		| VacuumStmt		{ output_statement($1, 0, NULL, connection); }
+		| VariableSetStmt	{ output_statement($1, 0, NULL, connection); }
+		| VariableShowStmt	{ output_statement($1, 0, NULL, connection); }
+		| VariableResetStmt	{ output_statement($1, 0, NULL, connection); }
+		| ConstraintsSetStmt	{ output_statement($1, 0, NULL, connection); }
 		| ECPGAllocateDescr	{	fprintf(yyout,"ECPGallocate_desc(__LINE__, \"%s\");",$1);
 								whenever_action(0);
 								free($1);
@@ -489,8 +489,8 @@ stmt:  AlterTableStmt			{ output_statement($1, 0, NULL, connection, argsinsert,
 						whenever_action(2);
 						free($1);
 					} 
-		| ECPGExecute		{	output_statement($1, 0, NULL, connection, argsinsert, argsresult); }
-		| ECPGFetchDescStmt	{ 	output_statement($1.str, 1, $1.name, connection, argsinsert, argsresult); }
+		| ECPGExecute		{	output_statement($1, 0, NULL, connection); }
+		| ECPGFetchDescStmt	{ 	output_statement($1.str, 1, $1.name, connection); }
 		| ECPGFree		{
 						fprintf(yyout, "{ ECPGdeallocate(__LINE__, \"%s\");", $1);
 
@@ -531,7 +531,9 @@ stmt:  AlterTableStmt			{ output_statement($1, 0, NULL, connection, argsinsert,
 						else
 							argsinsert = ptr->argsinsert;
 
-						output_statement(ptr->command, 0, NULL, ptr->connection, argsinsert, ptr->argsresult);
+						argsresult = ptr->argsresult;
+
+						output_statement(ptr->command, 0, NULL, ptr->connection);
 					}
 		| ECPGPrepare		{
 						if (connection)
diff --git a/src/interfaces/ecpg/preproc/variable.c b/src/interfaces/ecpg/preproc/variable.c
index 10b8af6cf95af7d0d880d8c3a0312b78e83790c3..98cf32576d752cfbefd75fd6baf06e64ecc4eb6f 100644
--- a/src/interfaces/ecpg/preproc/variable.c
+++ b/src/interfaces/ecpg/preproc/variable.c
@@ -189,7 +189,6 @@ reset_variables(void)
     argsresult = NULL;
 }
 
-
 /* Add a variable to a request. */
 void
 add_variable(struct arguments ** list, struct variable * var, struct variable * ind)