diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index a35254874855c58da257d81a5a54762cdc8ecb0a..100bf9a7b49e92cb42d1851235791ff4eac0a8f1 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.115 2002/04/16 23:08:11 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.116 2002/04/28 00:49:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1062,8 +1062,8 @@ exprIsLengthCoercion(Node *expr, int32 *coercedTypmod)
 	}
 
 	/*
-	 * Furthermore, the name of the function must be the same as the
-	 * argument/result type's name.
+	 * Furthermore, the name and namespace of the function must be the same
+	 * as its result type's name/namespace (cf. find_coercion_function).
 	 */
 	typeTuple = SearchSysCache(TYPEOID,
 							   ObjectIdGetDatum(procStruct->prorettype),
@@ -1072,9 +1072,9 @@ exprIsLengthCoercion(Node *expr, int32 *coercedTypmod)
 		elog(ERROR, "cache lookup for type %u failed",
 			 procStruct->prorettype);
 	typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
-	if (strncmp(NameStr(procStruct->proname),
-				NameStr(typeStruct->typname),
-				NAMEDATALEN) != 0)
+	if (strcmp(NameStr(procStruct->proname),
+			   NameStr(typeStruct->typname)) != 0 ||
+		procStruct->pronamespace != typeStruct->typnamespace)
 	{
 		ReleaseSysCache(procTuple);
 		ReleaseSysCache(typeTuple);
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 580abead5d0c226576033aa9e7a0b86bbdc79c84..e763b359bb823e69024a515a716fb8a8ca4ca566 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3,7 +3,7 @@
  *				back to source text
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.99 2002/04/25 02:56:55 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.100 2002/04/28 00:49:13 tgl Exp $
  *
  *	  This software is copyrighted by Jan Wieck - Hamburg.
  *
@@ -1805,23 +1805,12 @@ get_rule_expr(Node *node, deparse_context *context)
 		case T_RelabelType:
 			{
 				RelabelType *relabel = (RelabelType *) node;
-				HeapTuple	typetup;
-				Form_pg_type typeStruct;
-				char	   *extval;
 
 				appendStringInfoChar(buf, '(');
 				get_rule_expr(relabel->arg, context);
-				typetup = SearchSysCache(TYPEOID,
-								   ObjectIdGetDatum(relabel->resulttype),
-										 0, 0, 0);
-				if (!HeapTupleIsValid(typetup))
-					elog(ERROR, "cache lookup of type %u failed",
-						 relabel->resulttype);
-				typeStruct = (Form_pg_type) GETSTRUCT(typetup);
-				extval = pstrdup(NameStr(typeStruct->typname));
-				appendStringInfo(buf, ")::%s", quote_identifier(extval));
-				pfree(extval);
-				ReleaseSysCache(typetup);
+				appendStringInfo(buf, ")::%s",
+							format_type_with_typemod(relabel->resulttype,
+													 relabel->resulttypmod));
 			}
 			break;
 
@@ -2095,13 +2084,15 @@ strip_type_coercion(Node *expr, Oid resultType)
 			elog(ERROR, "cache lookup for proc %u failed", func->funcid);
 		procStruct = (Form_pg_proc) GETSTRUCT(procTuple);
 		/* Double-check func has one arg and correct result type */
+		/* Also, it must be an implicit coercion function */
 		if (procStruct->pronargs != 1 ||
-			procStruct->prorettype != resultType)
+			procStruct->prorettype != resultType ||
+			!procStruct->proimplicit)
 		{
 			ReleaseSysCache(procTuple);
 			return expr;
 		}
-		/* See if function has same name as its result type */
+		/* See if function has same name/namespace as its result type */
 		typeTuple = SearchSysCache(TYPEOID,
 								ObjectIdGetDatum(procStruct->prorettype),
 								   0, 0, 0);
@@ -2109,9 +2100,9 @@ strip_type_coercion(Node *expr, Oid resultType)
 			elog(ERROR, "cache lookup for type %u failed",
 				 procStruct->prorettype);
 		typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
-		if (strncmp(NameStr(procStruct->proname),
-					NameStr(typeStruct->typname),
-					NAMEDATALEN) != 0)
+		if (strcmp(NameStr(procStruct->proname),
+				   NameStr(typeStruct->typname)) != 0 ||
+			procStruct->pronamespace != typeStruct->typnamespace)
 		{
 			ReleaseSysCache(procTuple);
 			ReleaseSysCache(typeTuple);
@@ -2179,14 +2170,6 @@ get_const_expr(Const *constval, deparse_context *context)
 	char	   *extval;
 	char	   *valptr;
 
-	typetup = SearchSysCache(TYPEOID,
-							 ObjectIdGetDatum(constval->consttype),
-							 0, 0, 0);
-	if (!HeapTupleIsValid(typetup))
-		elog(ERROR, "cache lookup of type %u failed", constval->consttype);
-
-	typeStruct = (Form_pg_type) GETSTRUCT(typetup);
-
 	if (constval->constisnull)
 	{
 		/*
@@ -2194,13 +2177,19 @@ get_const_expr(Const *constval, deparse_context *context)
 		 * prevents misdecisions about the type, but it ensures that our
 		 * output is a valid b_expr.
 		 */
-		extval = pstrdup(NameStr(typeStruct->typname));
-		appendStringInfo(buf, "NULL::%s", quote_identifier(extval));
-		pfree(extval);
-		ReleaseSysCache(typetup);
+		appendStringInfo(buf, "NULL::%s",
+						 format_type_with_typemod(constval->consttype, -1));
 		return;
 	}
 
+	typetup = SearchSysCache(TYPEOID,
+							 ObjectIdGetDatum(constval->consttype),
+							 0, 0, 0);
+	if (!HeapTupleIsValid(typetup))
+		elog(ERROR, "cache lookup of type %u failed", constval->consttype);
+
+	typeStruct = (Form_pg_type) GETSTRUCT(typetup);
+
 	extval = DatumGetCString(OidFunctionCall3(typeStruct->typoutput,
 											  constval->constvalue,
 								   ObjectIdGetDatum(typeStruct->typelem),
@@ -2251,9 +2240,9 @@ get_const_expr(Const *constval, deparse_context *context)
 			/* These types can be left unlabeled */
 			break;
 		default:
-			extval = pstrdup(NameStr(typeStruct->typname));
-			appendStringInfo(buf, "::%s", quote_identifier(extval));
-			pfree(extval);
+			appendStringInfo(buf, "::%s",
+							 format_type_with_typemod(constval->consttype,
+													  -1));
 			break;
 	}
 
@@ -2583,8 +2572,8 @@ const char *
 quote_identifier(const char *ident)
 {
 	/*
-	 * Can avoid quoting if ident starts with a lowercase letter and
-	 * contains only lowercase letters, digits, and underscores, *and* is
+	 * Can avoid quoting if ident starts with a lowercase letter or underscore
+	 * and contains only lowercase letters, digits, and underscores, *and* is
 	 * not any SQL keyword.  Otherwise, supply quotes.
 	 */
 	bool		safe;
@@ -2594,7 +2583,7 @@ quote_identifier(const char *ident)
 	 * would like to use <ctype.h> macros here, but they might yield
 	 * unwanted locale-specific results...
 	 */
-	safe = (ident[0] >= 'a' && ident[0] <= 'z');
+	safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_');
 	if (safe)
 	{
 		const char *ptr;
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index 0816fc83e7ce34c82a688781752ad90de70790e5..bbda19decaa9e1d6cff4552cbb1526300fb0d775 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -1264,8 +1264,8 @@ drop table cchild;
 -- Check that ruleutils are working
 --
 SELECT viewname, definition FROM pg_views ORDER BY viewname;
-         viewname         |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               definition                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+         viewname         |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           definition                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  iexit                    | SELECT ih.name, ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
  pg_indexes               | SELECT c.relname AS tablename, i.relname AS indexname, pg_get_indexdef(x.indexrelid) AS indexdef FROM pg_index x, pg_class c, pg_class i WHERE ((((c.relkind = 'r'::"char") AND (i.relkind = 'i'::"char")) AND (c.oid = x.indrelid)) AND (i.oid = x.indexrelid));
  pg_rules                 | SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename, pg_get_ruledef(r.oid) AS definition FROM ((pg_rewrite r JOIN pg_class c ON ((c.oid = r.ev_class))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (r.rulename <> '_RETURN'::name);
@@ -1286,7 +1286,7 @@ SELECT viewname, definition FROM pg_views ORDER BY viewname;
  pg_statio_user_indexes   | SELECT pg_statio_all_indexes.relid, pg_statio_all_indexes.indexrelid, pg_statio_all_indexes.relname, pg_statio_all_indexes.indexrelname, pg_statio_all_indexes.idx_blks_read, pg_statio_all_indexes.idx_blks_hit FROM pg_statio_all_indexes WHERE (pg_statio_all_indexes.relname !~ '^pg_'::text);
  pg_statio_user_sequences | SELECT pg_statio_all_sequences.relid, pg_statio_all_sequences.relname, pg_statio_all_sequences.blks_read, pg_statio_all_sequences.blks_hit FROM pg_statio_all_sequences WHERE (pg_statio_all_sequences.relname !~ '^pg_'::text);
  pg_statio_user_tables    | SELECT pg_statio_all_tables.relid, pg_statio_all_tables.relname, pg_statio_all_tables.heap_blks_read, pg_statio_all_tables.heap_blks_hit, pg_statio_all_tables.idx_blks_read, pg_statio_all_tables.idx_blks_hit, pg_statio_all_tables.toast_blks_read, pg_statio_all_tables.toast_blks_hit, pg_statio_all_tables.tidx_blks_read, pg_statio_all_tables.tidx_blks_hit FROM pg_statio_all_tables WHERE (pg_statio_all_tables.relname !~ '^pg_'::text);
- pg_stats                 | SELECT c.relname AS tablename, a.attname, s.stanullfrac AS null_frac, s.stawidth AS avg_width, s.stadistinct AS n_distinct, CASE WHEN (1 = s.stakind1) THEN s.stavalues1 WHEN (1 = s.stakind2) THEN s.stavalues2 WHEN (1 = s.stakind3) THEN s.stavalues3 WHEN (1 = s.stakind4) THEN s.stavalues4 ELSE NULL::"_text" END AS most_common_vals, CASE WHEN (1 = s.stakind1) THEN s.stanumbers1 WHEN (1 = s.stakind2) THEN s.stanumbers2 WHEN (1 = s.stakind3) THEN s.stanumbers3 WHEN (1 = s.stakind4) THEN s.stanumbers4 ELSE NULL::"_float4" END AS most_common_freqs, CASE WHEN (2 = s.stakind1) THEN s.stavalues1 WHEN (2 = s.stakind2) THEN s.stavalues2 WHEN (2 = s.stakind3) THEN s.stavalues3 WHEN (2 = s.stakind4) THEN s.stavalues4 ELSE NULL::"_text" END AS histogram_bounds, CASE WHEN (3 = s.stakind1) THEN s.stanumbers1[1] WHEN (3 = s.stakind2) THEN s.stanumbers2[1] WHEN (3 = s.stakind3) THEN s.stanumbers3[1] WHEN (3 = s.stakind4) THEN s.stanumbers4[1] ELSE NULL::float4 END AS correlation FROM pg_class c, pg_attribute a, pg_statistic s WHERE ((((c.oid = s.starelid) AND (c.oid = a.attrelid)) AND (a.attnum = s.staattnum)) AND has_table_privilege(c.oid, 'select'::text));
+ pg_stats                 | SELECT c.relname AS tablename, a.attname, s.stanullfrac AS null_frac, s.stawidth AS avg_width, s.stadistinct AS n_distinct, CASE WHEN (1 = s.stakind1) THEN s.stavalues1 WHEN (1 = s.stakind2) THEN s.stavalues2 WHEN (1 = s.stakind3) THEN s.stavalues3 WHEN (1 = s.stakind4) THEN s.stavalues4 ELSE NULL::text[] END AS most_common_vals, CASE WHEN (1 = s.stakind1) THEN s.stanumbers1 WHEN (1 = s.stakind2) THEN s.stanumbers2 WHEN (1 = s.stakind3) THEN s.stanumbers3 WHEN (1 = s.stakind4) THEN s.stanumbers4 ELSE NULL::real[] END AS most_common_freqs, CASE WHEN (2 = s.stakind1) THEN s.stavalues1 WHEN (2 = s.stakind2) THEN s.stavalues2 WHEN (2 = s.stakind3) THEN s.stavalues3 WHEN (2 = s.stakind4) THEN s.stavalues4 ELSE NULL::text[] END AS histogram_bounds, CASE WHEN (3 = s.stakind1) THEN s.stanumbers1[1] WHEN (3 = s.stakind2) THEN s.stanumbers2[1] WHEN (3 = s.stakind3) THEN s.stanumbers3[1] WHEN (3 = s.stakind4) THEN s.stanumbers4[1] ELSE NULL::real END AS correlation FROM pg_class c, pg_attribute a, pg_statistic s WHERE ((((c.oid = s.starelid) AND (c.oid = a.attrelid)) AND (a.attnum = s.staattnum)) AND has_table_privilege(c.oid, 'select'::text));
  pg_tables                | SELECT c.relname AS tablename, pg_get_userbyid(c.relowner) AS tableowner, c.relhasindex AS hasindexes, c.relhasrules AS hasrules, (c.reltriggers > 0) AS hastriggers FROM pg_class c WHERE ((c.relkind = 'r'::"char") OR (c.relkind = 's'::"char"));
  pg_user                  | SELECT pg_shadow.usename, pg_shadow.usesysid, pg_shadow.usecreatedb, pg_shadow.usetrace, pg_shadow.usesuper, pg_shadow.usecatupd, '********'::text AS passwd, pg_shadow.valuntil, pg_shadow.useconfig FROM pg_shadow;
  pg_views                 | SELECT n.nspname AS schemaname, c.relname AS viewname, pg_get_userbyid(c.relowner) AS viewowner, pg_get_viewdef(c.oid) AS definition FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char");
@@ -1308,8 +1308,8 @@ SELECT viewname, definition FROM pg_views ORDER BY viewname;
 
 SELECT tablename, rulename, definition FROM pg_rules 
 	ORDER BY tablename, rulename;
-   tablename   |    rulename     |                                                                                                                          definition                                                                                                                           
----------------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+   tablename   |    rulename     |                                                                                                                                  definition                                                                                                                                   
+---------------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  rtest_emp     | rtest_emp_del   | CREATE RULE rtest_emp_del AS ON DELETE TO rtest_emp DO INSERT INTO rtest_emplog (ename, who, "action", newsal, oldsal) VALUES (old.ename, "current_user"(), 'fired     '::bpchar, '$0.00'::money, old.salary);
  rtest_emp     | rtest_emp_ins   | CREATE RULE rtest_emp_ins AS ON INSERT TO rtest_emp DO INSERT INTO rtest_emplog (ename, who, "action", newsal, oldsal) VALUES (new.ename, "current_user"(), 'hired     '::bpchar, new.salary, '$0.00'::money);
  rtest_emp     | rtest_emp_upd   | CREATE RULE rtest_emp_upd AS ON UPDATE TO rtest_emp WHERE (new.salary <> old.salary) DO INSERT INTO rtest_emplog (ename, who, "action", newsal, oldsal) VALUES (new.ename, "current_user"(), 'honored   '::bpchar, new.salary, old.salary);
@@ -1335,7 +1335,7 @@ SELECT tablename, rulename, definition FROM pg_rules
  shoelace      | shoelace_del    | CREATE RULE shoelace_del AS ON DELETE TO shoelace DO INSTEAD DELETE FROM shoelace_data WHERE (shoelace_data.sl_name = old.sl_name);
  shoelace      | shoelace_ins    | CREATE RULE shoelace_ins AS ON INSERT TO shoelace DO INSTEAD INSERT INTO shoelace_data (sl_name, sl_avail, sl_color, sl_len, sl_unit) VALUES (new.sl_name, new.sl_avail, new.sl_color, new.sl_len, new.sl_unit);
  shoelace      | shoelace_upd    | CREATE RULE shoelace_upd AS ON UPDATE TO shoelace DO INSTEAD UPDATE shoelace_data SET sl_name = new.sl_name, sl_avail = new.sl_avail, sl_color = new.sl_color, sl_len = new.sl_len, sl_unit = new.sl_unit WHERE (shoelace_data.sl_name = old.sl_name);
- shoelace_data | log_shoelace    | CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data WHERE (new.sl_avail <> old.sl_avail) DO INSERT INTO shoelace_log (sl_name, sl_avail, log_who, log_when) VALUES (new.sl_name, new.sl_avail, 'Al Bundy'::name, 'Thu Jan 01 00:00:00 1970'::"timestamp");
+ shoelace_data | log_shoelace    | CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data WHERE (new.sl_avail <> old.sl_avail) DO INSERT INTO shoelace_log (sl_name, sl_avail, log_who, log_when) VALUES (new.sl_name, new.sl_avail, 'Al Bundy'::name, 'Thu Jan 01 00:00:00 1970'::timestamp without time zone);
  shoelace_ok   | shoelace_ok_ins | CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok DO INSTEAD UPDATE shoelace SET sl_avail = (shoelace.sl_avail + new.ok_quant) WHERE (shoelace.sl_name = new.ok_name);
 (27 rows)