diff --git a/src/interfaces/ecpg/ChangeLog b/src/interfaces/ecpg/ChangeLog
index f369cf89dd310761bbe6f25c44ada5ffd2e72997..0f2476d8c10b63eafc66db04b93665b1f4611a0b 100644
--- a/src/interfaces/ecpg/ChangeLog
+++ b/src/interfaces/ecpg/ChangeLog
@@ -1814,6 +1814,13 @@ Thu Jun 17 13:50:06 CEST 2004
 Sun Jun 20 12:44:01 CEST 2004
 
 	- Synced parser and keyword list.
+	
+Sun Jun 27 13:50:58 CEST 2004
+	
+	- Only use typedefs inside their scope.
+	- Variables that are out of scope, were not removed all the time.
+	- Make a varchar NULL set everything to 0 when not using indicators.
+	- Synced parser.
 	- Set pgtypes library version to 1.2.
 	- Set ecpg version to 3.2.0.
 	- Set compat library version to 1.2.
diff --git a/src/interfaces/ecpg/compatlib/informix.c b/src/interfaces/ecpg/compatlib/informix.c
index 973d7a8547d4efc3e3633e08f878e7f4843af2d6..ad55a542e28f0821de91efb057f88ea36a9c385d 100644
--- a/src/interfaces/ecpg/compatlib/informix.c
+++ b/src/interfaces/ecpg/compatlib/informix.c
@@ -1002,12 +1002,12 @@ ECPG_informix_get_var(int number)
 int
 rsetnull(int t, char *ptr)
 {
-	ECPGset_informix_null(t, ptr);
+	ECPGset_noind_null(t, ptr);
 	return 0;
 }
 
 int
 risnull(int t, char *ptr)
 {
-	return (ECPGis_informix_null(t, ptr));
+	return (ECPGis_noind_null(t, ptr));
 }
diff --git a/src/interfaces/ecpg/ecpglib/data.c b/src/interfaces/ecpg/ecpglib/data.c
index 5626b0debcfe681597b7682d4161c29cf00aa6fa..17d2f8ddf4eb0539f9702950acde0f6abf9397ef 100644
--- a/src/interfaces/ecpg/ecpglib/data.c
+++ b/src/interfaces/ecpg/ecpglib/data.c
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/data.c,v 1.23 2004/05/05 15:03:04 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/data.c,v 1.24 2004/06/27 12:28:39 meskes Exp $ */
 
 #define POSTGRES_ECPG_INTERNAL
 #include "postgres_fe.h"
@@ -76,7 +76,10 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 	 * and 0 if not
 	 */
 	if (PQgetisnull(results, act_tuple, act_field))
+	{
+		printf("MM NULL\n");
 		value_for_indicator = -1;
+	}
 
 	switch (ind_type)
 	{
@@ -107,7 +110,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 					 * Informix has an additional way to specify NULLs
 					 * note that this uses special values to denote NULL
 					 */
-					ECPGset_informix_null(type, var + offset * act_tuple);
+					ECPGset_noind_null(type, var + offset * act_tuple);
 				}
 				else
 				{
@@ -411,7 +414,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 						if (INFORMIX_MODE(compat))
 						{
 							/* Informix wants its own NULL value here instead of an error */
-							ECPGset_informix_null(ECPGt_numeric, nres);
+							ECPGset_noind_null(ECPGt_numeric, nres);
 						}
 						else
 						{
@@ -454,7 +457,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 						if (INFORMIX_MODE(compat))
 						{
 							/* Informix wants its own NULL value here instead of an error */
-							ECPGset_informix_null(ECPGt_interval, ires);
+							ECPGset_noind_null(ECPGt_interval, ires);
 						}
 						else
 						{
@@ -493,7 +496,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 						if (INFORMIX_MODE(compat))
 						{
 							/* Informix wants its own NULL value here instead of an error */
-							ECPGset_informix_null(ECPGt_date, &ddres);
+							ECPGset_noind_null(ECPGt_date, &ddres);
 						}
 						else
 						{
@@ -531,7 +534,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 						if (INFORMIX_MODE(compat))
 						{
 							/* Informix wants its own NULL value here instead of an error */
-							ECPGset_informix_null(ECPGt_timestamp, &tres);
+							ECPGset_noind_null(ECPGt_timestamp, &tres);
 						}
 						else
 						{
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index 2cee7c70c9c0e6929c99cc32d2033c091b64a558..c91e96d3a1a85d64d347fc5f045e831cde889f6d 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.33 2004/05/05 15:03:04 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.34 2004/06/27 12:28:40 meskes Exp $ */
 
 /*
  * The aim is to get a simpler inteface to the database routines.
@@ -532,7 +532,7 @@ ECPGstore_input(const struct statement * stmt, const struct variable * var,
 		case ECPGt_NO_INDICATOR:
 			if (stmt->force_indicator == false)
 			{
-				if (ECPGis_informix_null(var->type, var->value))
+				if (ECPGis_noind_null(var->type, var->value))
 					*tobeinserted_p = "null";
 			}
 			break;
diff --git a/src/interfaces/ecpg/ecpglib/misc.c b/src/interfaces/ecpg/ecpglib/misc.c
index d2f81a16282c4487d9b5922d3be0d08a5f5af636..8faf6b2fdffd22b1d5e421e83065ad0012ff5ddd 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.20 2004/03/14 14:47:33 momjian Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.21 2004/06/27 12:28:40 meskes Exp $ */
 
 #define POSTGRES_ECPG_INTERNAL
 #include "postgres_fe.h"
@@ -266,7 +266,7 @@ ECPGlog(const char *format,...)
 }
 
 void
-ECPGset_informix_null(enum ECPGttype type, void *ptr)
+ECPGset_noind_null(enum ECPGttype type, void *ptr)
 {
 	switch (type)
 	{
@@ -301,6 +301,7 @@ ECPGset_informix_null(enum ECPGttype type, void *ptr)
 			break;
 		case ECPGt_varchar:
 			*(((struct ECPGgeneric_varchar *) ptr)->arr) = 0x00;
+			((struct ECPGgeneric_varchar *) ptr)->len = 0;
 			break;
 		case ECPGt_decimal:
 			memset((char *) ptr, 0, sizeof(decimal));
@@ -331,7 +332,7 @@ _check(unsigned char *ptr, int length)
 }
 
 bool
-ECPGis_informix_null(enum ECPGttype type, void *ptr)
+ECPGis_noind_null(enum ECPGttype type, void *ptr)
 {
 	switch (type)
 	{
diff --git a/src/interfaces/ecpg/include/ecpglib.h b/src/interfaces/ecpg/include/ecpglib.h
index 6f9938aaf0d1d14d1a5d83d8f6c3559570538a5d..b76cd4d19c2bf48da8b22db3c6135e1560d16c4f 100644
--- a/src/interfaces/ecpg/include/ecpglib.h
+++ b/src/interfaces/ecpg/include/ecpglib.h
@@ -77,8 +77,8 @@ void		ECPGraise_backend(int line, PGresult *result, PGconn *conn, int compat);
 bool		ECPGget_desc_header(int, char *, int *);
 bool		ECPGget_desc(int, char *, int,...);
 
-void		ECPGset_informix_null(enum ECPGttype, void *);
-bool		ECPGis_informix_null(enum ECPGttype, void *);
+void		ECPGset_noind_null(enum ECPGttype, void *);
+bool		ECPGis_noind_null(enum ECPGttype, void *);
 bool		ECPGdescribe(int, bool, const char *,...);
 
 /* dynamic result allocation */
diff --git a/src/interfaces/ecpg/preproc/extern.h b/src/interfaces/ecpg/preproc/extern.h
index 3383682fc46620f1499f4ac7f2259f590b6d34c0..3d1088a20715ef57002cda9bbdecc0b2f4d6e481 100644
--- a/src/interfaces/ecpg/preproc/extern.h
+++ b/src/interfaces/ecpg/preproc/extern.h
@@ -82,6 +82,7 @@ extern struct typedefs *get_typedef(char *);
 extern void adjust_array(enum ECPGttype, char **, char **, char *, char *, int, bool);
 extern void reset_variables(void);
 extern void check_indicator(struct ECPGtype *);
+extern void remove_typedefs(int);
 extern void remove_variables(int);
 extern struct variable *new_variable(const char *, struct ECPGtype *, int);
 extern ScanKeyword *ScanKeywordLookup(char *text);
diff --git a/src/interfaces/ecpg/preproc/preproc.y b/src/interfaces/ecpg/preproc/preproc.y
index d02a3d0976205b8bec45c5fc99f729bd8b415a4c..b4cf02e6e266f94f085424f9aed6b846f0000c36 100644
--- a/src/interfaces/ecpg/preproc/preproc.y
+++ b/src/interfaces/ecpg/preproc/preproc.y
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.288 2004/06/20 10:45:47 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.289 2004/06/27 12:28:42 meskes Exp $ */
 
 /* Copyright comment */
 %{
@@ -542,7 +542,7 @@ add_additional_variables(char *name, bool insert)
 %type  <str>	inf_val_list inf_col_list using_descriptor into_descriptor 
 %type  <str>	ecpg_into_using prepared_name struct_union_type_with_symbol
 %type  <str>	ECPGunreserved ECPGunreserved_interval cvariable
-%type  <str>	AlterDbOwnerStmt OptTableSpaceOwner CreateTableSpaceStmt
+%type  <str>	AlterOwnerStmt OptTableSpaceOwner CreateTableSpaceStmt
 %type  <str>	DropTableSpaceStmt indirection indirection_el
 
 %type  <struct_union> s_struct_union_symbol
@@ -578,7 +578,7 @@ statement: ecpgstart opt_at stmt ';'	{ connection = NULL; }
 		| c_thing		{ fprintf(yyout, "%s", $1); free($1); }
 		| CPP_LINE		{ fprintf(yyout, "%s", $1); free($1); }
 		| '{'			{ braces_open++; fputs("{", yyout); }
-		| '}'			{ remove_variables(braces_open--); fputs("}", yyout); }
+		| '}'			{ remove_typedefs(braces_open); remove_variables(braces_open--); fputs("}", yyout); }
 		;
 
 opt_at: AT connection_target
@@ -594,9 +594,9 @@ opt_at: AT connection_target
 		};
 
 stmt:  AlterDatabaseSetStmt		{ output_statement($1, 0, connection); }
-		| AlterDbOwnerStmt	{ output_statement($1, 0, connection); }
 		| AlterDomainStmt	{ output_statement($1, 0, connection); }
 		| AlterGroupStmt	{ output_statement($1, 0, connection); }
+		| AlterOwnerStmt	{ output_statement($1, 0, connection); }
 		| AlterSeqStmt		{ output_statement($1, 0, connection); }
 		| AlterTableStmt	{ output_statement($1, 0, connection); }
 		| AlterUserSetStmt	{ output_statement($1, 0, connection); }
@@ -2385,12 +2385,42 @@ RenameStmt:  ALTER AGGREGATE func_name '(' aggr_argtype ')' RENAME TO name
 			{ $$ = cat_str(6, make_str("alter trigger"), $3, make_str("on"), $5, make_str("rename to"), $8); }
 	| ALTER USER UserId RENAME TO UserId
 			{ $$ = cat_str(4, make_str("alter user"), $3, make_str("rename to"), $6); }
+	| ALTER TABLESPACE name RENAME TO name
+			{ $$ = cat_str(4, make_str("alter tablespace"), $3, make_str("rename to"), $6); }
 		;
 
 opt_column:  COLUMN			{ $$ = make_str("column"); }
 		| /*EMPTY*/			{ $$ = EMPTY; }
 		;
 
+/*****************************************************************************
+ *
+ * ALTER THING name OWNER TO newname.
+ *
+ *****************************************************************************/
+
+AlterOwnerStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' OWNER TO UserId
+			{ $$ = cat_str(6, make_str("alter aggregate"), $3, make_str("("), $5, make_str(") owner to"), $9); }
+		| ALTER CONVERSION_P any_name OWNER TO UserId
+			{ $$ = cat_str(4, make_str("alter conversion"), $3, make_str("owner to"), $6); }
+		| ALTER DATABASE database_name OWNER TO UserId
+			{ $$ = cat_str(4, make_str("alter database"), $3, make_str("owner to"), $6); }
+		| ALTER DOMAIN_P database_name OWNER TO UserId
+			{ $$ = cat_str(4, make_str("alter domain"), $3, make_str("owner to"), $6); }
+		| ALTER FUNCTION func_name func_args OWNER TO UserId
+			{ $$ = cat_str(5, make_str("alter function"), $3, $4, make_str("owner to"), $7); }
+		| ALTER OPERATOR any_operator '(' oper_argtypes ')' OWNER TO UserId
+			{ $$ = cat_str(6, make_str("alter operator"), $3, make_str("("), $5, make_str(") owner to"), $9); }
+		| ALTER OPERATOR CLASS any_name USING access_method OWNER TO UserId
+			{ $$ = cat_str(6, make_str("alter operator class"), $4, make_str("using"), $6, make_str("owner to"), $9); }
+		| ALTER SCHEMA name OWNER TO UserId
+			{ $$ = cat_str(4, make_str("alter schema"), $3, make_str("owner to"), $6); }
+		| ALTER TYPE_P any_name OWNER TO UserId
+			{ $$ = cat_str(4, make_str("alter type"), $3, make_str("owner to"), $6); }
+		| ALTER TABLESPACE name OWNER TO UserId
+			{ $$ = cat_str(4, make_str("alter tablespace"), $3, make_str("owner to"), $6); }
+		;
+
 
 /*****************************************************************************
  *
@@ -2589,8 +2619,6 @@ opt_equal: '='					{ $$ = make_str("="); }
  *
  *****************************************************************************/
 
-AlterDbOwnerStmt: ALTER DATABASE database_name OWNER TO UserId
-		      	{ $$ = cat_str(4, make_str("alter database"), $3, make_str("owner to"), $6); }
 AlterDatabaseSetStmt: ALTER DATABASE database_name SET set_rest
 			{ $$ = cat_str(4, make_str("alter database"), $3, make_str("set"), $5); }
 		| ALTER DATABASE database_name VariableResetStmt
@@ -2632,8 +2660,6 @@ AlterDomainStmt:
 		{ $$ = cat_str(4, make_str("alter domain"), $3, make_str("add"), $5); }
 	| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
 		{ $$ = cat_str(5, make_str("alter domain"), $3, make_str("drop constraint"), $6, $7); }
-	| ALTER DOMAIN_P any_name OWNER TO UserId
-		{ $$ = cat_str(4, make_str("alter domain"), $3, make_str("owner to"), $6); }
 	;
 	
 opt_as:	AS	{$$ = make_str("as"); }
@@ -4582,6 +4608,7 @@ type_declaration: S_TYPEDEF
 	        	/* initial definition */
 		        this->next = types;
 	        	this->name = $5;
+	        	this->brace_level = braces_open;
 			this->type = (struct this_type *) mm_alloc(sizeof(struct this_type));
 			this->type->type_enum = $3.type_enum;
 			this->type->type_str = mm_strdup($5);
@@ -4897,6 +4924,7 @@ struct_union_type_with_symbol: s_struct_union_symbol
                         /* initial definition */
                         this->next = types;
 			this->name = mm_strdup(su_type.type_str);
+			this->brace_level = braces_open;
                         this->type = (struct this_type *) mm_alloc(sizeof(struct this_type));
                         this->type->type_enum = su_type.type_enum;
                         this->type->type_str = mm_strdup(su_type.type_str);
@@ -5417,6 +5445,7 @@ ECPGTypedef: TYPE_P
 				/* initial definition */
 				this->next = types;
 				this->name = $3;
+				this->brace_level = braces_open;
 				this->type = (struct this_type *) mm_alloc(sizeof(struct this_type));
 				this->type->type_enum = $5.type_enum;
 				this->type->type_str = mm_strdup($3);
diff --git a/src/interfaces/ecpg/preproc/type.h b/src/interfaces/ecpg/preproc/type.h
index a528b11040acfebd93916600e055ddec808cc918..3b1f4c657b1e7ed94976c39dae021a64cf8052c7 100644
--- a/src/interfaces/ecpg/preproc/type.h
+++ b/src/interfaces/ecpg/preproc/type.h
@@ -123,26 +123,27 @@ struct cursor
 
 struct typedefs
 {
-	char	   *name;
-	struct this_type *type;
-	struct ECPGstruct_member *struct_member_list;
-	struct typedefs *next;
+	char				*name;
+	struct this_type		*type;
+	struct ECPGstruct_member 	*struct_member_list;
+	int				brace_level;
+	struct typedefs 		*next;
 };
 
 struct _defines
 {
-	char	   *old;
-	char	   *new;
-	int			pertinent;
+	char	   	*old;
+	char	   	*new;
+	int		pertinent;
 	struct _defines *next;
 };
 
 /* This is a linked list of the variable names and types. */
 struct variable
 {
-	char	   *name;
+	char	   	*name;
 	struct ECPGtype *type;
-	int			brace_level;
+	int		brace_level;
 	struct variable *next;
 };
 
diff --git a/src/interfaces/ecpg/preproc/variable.c b/src/interfaces/ecpg/preproc/variable.c
index 9663e29ef03efbf353e2a80404f54ad57694437e..f13f7375cb0f0a84e14d4f3692e8535e1d8092de 100644
--- a/src/interfaces/ecpg/preproc/variable.c
+++ b/src/interfaces/ecpg/preproc/variable.c
@@ -275,13 +275,47 @@ find_variable(char *name)
 	return (p);
 }
 
+void
+remove_typedefs(int brace_level)
+{
+	struct typedefs *p,
+			   *prev;
+
+	for (p = prev = types; p;)
+	{
+		if (p->brace_level >= brace_level)
+		{
+			/* remove it */
+			if (p == types)
+				prev = types = p->next;
+			else
+				prev->next = p->next;
+
+			if (p->type->type_enum == ECPGt_struct || p->type->type_enum == ECPGt_union) 
+				free(p->struct_member_list);
+			free(p->type);
+			free(p->name);
+			free(p);
+			if (prev == types)
+				p = types;
+			else
+				p = prev ? prev->next : NULL;
+		}
+		else
+		{
+			prev = p;
+			p = prev->next;
+		}
+	}
+}
+
 void
 remove_variables(int brace_level)
 {
 	struct variable *p,
 			   *prev;
 
-	for (p = prev = allvariables; p; p = p ? p->next : NULL)
+	for (p = prev = allvariables; p;)
 	{
 		if (p->brace_level >= brace_level)
 		{
@@ -326,10 +360,16 @@ remove_variables(int brace_level)
 			ECPGfree_type(p->type);
 			free(p->name);
 			free(p);
-			p = prev;
+			if (prev == allvariables)
+				p = allvariables;
+			else
+				p = prev ? prev->next : NULL;
 		}
 		else
+		{
 			prev = p;
+			p = prev->next;
+		}
 	}
 }