diff --git a/src/backend/optimizer/geqo/geqo_erx.c b/src/backend/optimizer/geqo/geqo_erx.c
index fe0baa42f1c005e4eb4d560269d7f7892650ac11..86b471f1cd033e1dc335fa541919714ea92ae082 100644
--- a/src/backend/optimizer/geqo/geqo_erx.c
+++ b/src/backend/optimizer/geqo/geqo_erx.c
@@ -3,7 +3,7 @@
 * geqo_erx.c
 *	 edge recombination crossover [ER]
 *
-* $Id: geqo_erx.c,v 1.17 2002/03/02 21:39:26 momjian Exp $
+* $Id: geqo_erx.c,v 1.18 2003/07/25 00:01:06 tgl Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -333,7 +333,7 @@ gimme_gene(Edge edge, Edge *edge_table)
 			minimum_count = 1;
 		}
 		else if (minimum_count == -1)
-			elog(ERROR, "gimme_gene: Internal error - minimum_count not set");
+			elog(ERROR, "minimum_count not set");
 		else if (edge_table[(int) friend].unused_edges == minimum_edges)
 			minimum_count++;
 
@@ -359,7 +359,7 @@ gimme_gene(Edge edge, Edge *edge_table)
 	}
 
 	/* ... should never be reached */
-	elog(ERROR, "gimme_gene: neither shared nor minimum number nor random edge found");
+	elog(ERROR, "neither shared nor minimum number nor random edge found");
 	return 0;					/* to keep the compiler quiet */
 }
 
@@ -419,15 +419,11 @@ edge_failure(Gene *gene, int index, Edge *edge_table, int num_gene)
 			}
 		}
 
-		elog(LOG, "edge_failure(1): no edge found via random decision and total_edges == 4");
+		elog(LOG, "no edge found via random decision and total_edges == 4");
 	}
-
-	else
-/* random decision of the gene with remaining edges */
-
-	if (remaining_edges != 0)
+	else if (remaining_edges != 0)
 	{
-
+		/* random decision of the gene with remaining edges */
 		rand_decision = (int) geqo_randint(remaining_edges - 1, 0);
 
 		for (i = 1; i <= num_gene; i++)
@@ -444,7 +440,7 @@ edge_failure(Gene *gene, int index, Edge *edge_table, int num_gene)
 			}
 		}
 
-		elog(LOG, "edge_failure(2): no edge found via random decision and remainig edges");
+		elog(LOG, "no edge found via random decision with remaining edges");
 	}
 
 	/*
@@ -462,11 +458,11 @@ edge_failure(Gene *gene, int index, Edge *edge_table, int num_gene)
 			if (edge_table[i].unused_edges >= 0)
 				return (Gene) i;
 
-		elog(LOG, "edge_failure(3): no edge found via looking for the last ununsed point");
+		elog(LOG, "no edge found via looking for the last ununsed point");
 	}
 
 
-/* ... should never be reached */
-	elog(ERROR, "edge_failure: no edge detected");
+	/* ... should never be reached */
+	elog(ERROR, "no edge found");
 	return 0;					/* to keep the compiler quiet */
 }
diff --git a/src/backend/optimizer/geqo/geqo_eval.c b/src/backend/optimizer/geqo/geqo_eval.c
index 1be69e93f352b201f06ba27ca43e7f5d139d0946..ee06560ca2393b47586e037429fc9ac0f9065966 100644
--- a/src/backend/optimizer/geqo/geqo_eval.c
+++ b/src/backend/optimizer/geqo/geqo_eval.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Header: /cvsroot/pgsql/src/backend/optimizer/geqo/geqo_eval.c,v 1.62 2003/05/02 20:54:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/geqo/geqo_eval.c,v 1.63 2003/07/25 00:01:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -66,7 +66,7 @@ geqo_eval(Query *root, List *initial_rels, Gene *tour, int num_gene)
 	 * Since geqo_eval() will be called many times, we can't afford to let
 	 * all that memory go unreclaimed until end of statement.  Note we
 	 * make the temp context a child of the planner's normal context, so that
-	 * it will be freed even if we abort via elog(ERROR).
+	 * it will be freed even if we abort via ereport(ERROR).
 	 */
 	mycontext = AllocSetContextCreate(CurrentMemoryContext,
 									  "GEQO",
diff --git a/src/backend/optimizer/geqo/geqo_main.c b/src/backend/optimizer/geqo/geqo_main.c
index c4190ac4443c07d03c89cac9e2d419c4c765bf9f..f60fd7d46671aee6e2ef929f88445790259baa4d 100644
--- a/src/backend/optimizer/geqo/geqo_main.c
+++ b/src/backend/optimizer/geqo/geqo_main.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Header: /cvsroot/pgsql/src/backend/optimizer/geqo/geqo_main.c,v 1.35 2003/05/27 17:49:46 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/geqo/geqo_main.c,v 1.36 2003/07/25 00:01:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -120,30 +120,30 @@ geqo(Query *root, int number_of_rels, List *initial_rels)
 	daddy = alloc_chromo(pool->string_length);
 
 #if defined (ERX)
-	elog(DEBUG2, "geqo_main: using edge recombination crossover [ERX]");
+	elog(DEBUG2, "using edge recombination crossover [ERX]");
 /* allocate edge table memory */
 	edge_table = alloc_edge_table(pool->string_length);
 #elif defined(PMX)
-	elog(DEBUG2, "geqo_main: using partially matched crossover [PMX]");
+	elog(DEBUG2, "using partially matched crossover [PMX]");
 /* allocate chromosome kid memory */
 	kid = alloc_chromo(pool->string_length);
 #elif defined(CX)
-	elog(DEBUG2, "geqo_main: using cycle crossover [CX]");
+	elog(DEBUG2, "using cycle crossover [CX]");
 /* allocate city table memory */
 	kid = alloc_chromo(pool->string_length);
 	city_table = alloc_city_table(pool->string_length);
 #elif defined(PX)
-	elog(DEBUG2, "geqo_main: using position crossover [PX]");
+	elog(DEBUG2, "using position crossover [PX]");
 /* allocate city table memory */
 	kid = alloc_chromo(pool->string_length);
 	city_table = alloc_city_table(pool->string_length);
 #elif defined(OX1)
-	elog(DEBUG2, "geqo_main: using order crossover [OX1]");
+	elog(DEBUG2, "using order crossover [OX1]");
 /* allocate city table memory */
 	kid = alloc_chromo(pool->string_length);
 	city_table = alloc_city_table(pool->string_length);
 #elif defined(OX2)
-	elog(DEBUG2, "geqo_main: using order crossover [OX2]");
+	elog(DEBUG2, "using order crossover [OX2]");
 /* allocate city table memory */
 	kid = alloc_chromo(pool->string_length);
 	city_table = alloc_city_table(pool->string_length);
@@ -211,7 +211,7 @@ geqo(Query *root, int number_of_rels, List *initial_rels)
 		elog(LOG, "[GEQO] failures: %d, average: %d",
 			 edge_failures, (int) generation / edge_failures);
 	else
-		elog(LOG, "[GEQO] No edge failures detected.");
+		elog(LOG, "[GEQO] no edge failures detected");
 #endif
 
 
@@ -219,7 +219,7 @@ geqo(Query *root, int number_of_rels, List *initial_rels)
 	if (mutations != 0)
 		elog(LOG, "[GEQO] mutations: %d, generations: %d", mutations, generation);
 	else
-		elog(LOG, "[GEQO] No mutations processed.");
+		elog(LOG, "[GEQO] no mutations processed");
 #endif
 
 
@@ -239,7 +239,7 @@ geqo(Query *root, int number_of_rels, List *initial_rels)
 						  best_tour, pool->string_length);
 
 	if (best_rel == NULL)
-		elog(ERROR, "geqo: failed to make a valid plan");
+		elog(ERROR, "failed to make a valid plan");
 
 	/* DBG: show the query plan */
 #ifdef NOT_USED
diff --git a/src/backend/optimizer/geqo/geqo_misc.c b/src/backend/optimizer/geqo/geqo_misc.c
index 7cf3a5a14d55bc3e5391046ea8dfd623bb5829a0..acc9285d005424d6ae271feac12198f55e23e047 100644
--- a/src/backend/optimizer/geqo/geqo_misc.c
+++ b/src/backend/optimizer/geqo/geqo_misc.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Header: /cvsroot/pgsql/src/backend/optimizer/geqo/geqo_misc.c,v 1.36 2002/12/16 21:30:29 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/geqo/geqo_misc.c,v 1.37 2003/07/25 00:01:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -38,7 +38,7 @@ avg_pool(Pool *pool)
 	double		cumulative = 0.0;
 
 	if (pool->size <= 0)
-		elog(ERROR, "avg_pool: pool_size of zero");
+		elog(ERROR, "pool_size is zero");
 
 	/*
 	 * Since the pool may contain multiple occurrences of DBL_MAX, divide
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index cd04af9f821ee2feb35e6d939829cebda6fef630..486dede0fb9680e8a08a89bbdef5cab0057c2a05 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.103 2003/06/29 23:05:04 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.104 2003/07/25 00:01:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -209,7 +209,9 @@ set_inherited_rel_pathlist(Query *root, RelOptInfo *rel,
 	 * do better?
 	 */
 	if (intMember(parentRTindex, root->rowMarks))
-		elog(ERROR, "SELECT FOR UPDATE is not supported for inherit queries");
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("SELECT FOR UPDATE is not supported for inherit queries")));
 
 	/*
 	 * The executor will check the parent table's access permissions when
@@ -642,7 +644,7 @@ recurse_pushdown_safe(Node *setOp, Query *topquery,
 	}
 	else
 	{
-		elog(ERROR, "recurse_pushdown_safe: unexpected node %d",
+		elog(ERROR, "unrecognized node type: %d",
 			 (int) nodeTag(setOp));
 	}
 	return true;
@@ -839,7 +841,7 @@ recurse_push_qual(Node *setOp, Query *topquery,
 	}
 	else
 	{
-		elog(ERROR, "recurse_push_qual: unexpected node %d",
+		elog(ERROR, "unrecognized node type: %d",
 			 (int) nodeTag(setOp));
 	}
 }
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 3011f687c593f8130d469fea8115fa914593f1c1..e1754a7a694075af23a47fc38cff63d22c9f2f7f 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -49,7 +49,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.110 2003/07/14 22:35:54 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.111 2003/07/25 00:01:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1513,7 +1513,7 @@ cost_qual_eval_walker(Node *node, QualCost *total)
 	else if (IsA(node, SubLink))
 	{
 		/* This routine should not be applied to un-planned expressions */
-		elog(ERROR, "cost_qual_eval: can't handle unplanned sub-select");
+		elog(ERROR, "cannot handle unplanned sub-select");
 	}
 	else if (IsA(node, SubPlan))
 	{
@@ -1805,8 +1805,7 @@ set_joinrel_size_estimates(Query *root, RelOptInfo *rel,
 				temp = inner_rel->rows;
 			break;
 		default:
-			elog(ERROR, "set_joinrel_size_estimates: unsupported join type %d",
-				 (int) jointype);
+			elog(ERROR, "unrecognized join type: %d", (int) jointype);
 			temp = 0;			/* keep compiler quiet */
 			break;
 	}
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index fbbf983869cbb026257f5ae648220e19aed52360..fa19abe4717a4c7e4012d5575aa2142f4232caa4 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.144 2003/06/15 22:51:45 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.145 2003/07/25 00:01:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -490,7 +490,7 @@ extract_or_indexqual_conditions(RelOptInfo *rel,
 	} while (!DoneMatchingIndexKeys(classes));
 
 	if (FastListValue(&quals) == NIL)
-		elog(ERROR, "extract_or_indexqual_conditions: no matching clause");
+		elog(ERROR, "no matching OR clause");
 
 	return FastListValue(&quals);
 }
@@ -1245,7 +1245,7 @@ pred_test_simple_clause(Expr *predicate, Node *clause)
 	if (!OidIsValid(test_op))
 	{
 		/* This should not fail, else pg_amop entry is missing */
-		elog(ERROR, "Missing pg_amop entry for opclass %u strategy %d",
+		elog(ERROR, "missing pg_amop entry for opclass %u strategy %d",
 			 opclass_id, test_strategy);
 	}
 
@@ -1281,7 +1281,7 @@ pred_test_simple_clause(Expr *predicate, Node *clause)
 	if (isNull)
 	{
 		/* Treat a null result as false ... but it's a tad fishy ... */
-		elog(DEBUG2, "pred_test_simple_clause: null test result");
+		elog(DEBUG2, "null predicate test result");
 		return false;
 	}
 	return DatumGetBool(test_result);
@@ -2055,7 +2055,8 @@ prefix_quals(Node *leftop, Oid opclass,
 			break;
 
 		default:
-			elog(ERROR, "prefix_quals: unexpected opclass %u", opclass);
+			/* shouldn't get here */
+			elog(ERROR, "unexpected opclass: %u", opclass);
 			return NIL;
 	}
 
@@ -2078,7 +2079,7 @@ prefix_quals(Node *leftop, Oid opclass,
 															 prefix_const->constvalue));
 				break;
 			default:
-				elog(ERROR, "prefix_quals: unexpected consttype %u",
+				elog(ERROR, "unexpected const type: %u",
 					 prefix_const->consttype);
 				return NIL;
 		}
@@ -2093,7 +2094,7 @@ prefix_quals(Node *leftop, Oid opclass,
 	{
 		oproid = get_opclass_member(opclass, BTEqualStrategyNumber);
 		if (oproid == InvalidOid)
-			elog(ERROR, "prefix_quals: no operator = for opclass %u", opclass);
+			elog(ERROR, "no = operator for opclass %u", opclass);
 		expr = make_opclause(oproid, BOOLOID, false,
 							 (Expr *) leftop, (Expr *) prefix_const);
 		result = makeList1(expr);
@@ -2107,7 +2108,7 @@ prefix_quals(Node *leftop, Oid opclass,
 	 */
 	oproid = get_opclass_member(opclass, BTGreaterEqualStrategyNumber);
 	if (oproid == InvalidOid)
-		elog(ERROR, "prefix_quals: no operator >= for opclass %u", opclass);
+		elog(ERROR, "no >= operator for opclass %u", opclass);
 	expr = make_opclause(oproid, BOOLOID, false,
 						 (Expr *) leftop, (Expr *) prefix_const);
 	result = makeList1(expr);
@@ -2122,7 +2123,7 @@ prefix_quals(Node *leftop, Oid opclass,
 	{
 		oproid = get_opclass_member(opclass, BTLessStrategyNumber);
 		if (oproid == InvalidOid)
-			elog(ERROR, "prefix_quals: no operator < for opclass %u", opclass);
+			elog(ERROR, "no < operator for opclass %u", opclass);
 		expr = make_opclause(oproid, BOOLOID, false,
 							 (Expr *) leftop, (Expr *) greaterstr);
 		result = lappend(result, expr);
@@ -2167,8 +2168,7 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop)
 			is_eq = true;
 			break;
 		default:
-			elog(ERROR, "network_prefix_quals: unexpected operator %u",
-				 expr_op);
+			elog(ERROR, "unexpected operator: %u", expr_op);
 			return NIL;
 	}
 
@@ -2180,15 +2180,13 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop)
 	{
 		opr1oid = get_opclass_member(opclass, BTGreaterEqualStrategyNumber);
 		if (opr1oid == InvalidOid)
-			elog(ERROR, "network_prefix_quals: no >= operator for opclass %u",
-				 opclass);
+			elog(ERROR, "no >= operator for opclass %u", opclass);
 	}
 	else
 	{
 		opr1oid = get_opclass_member(opclass, BTGreaterStrategyNumber);
 		if (opr1oid == InvalidOid)
-			elog(ERROR, "network_prefix_quals: no > operator for opclass %u",
-				 opclass);
+			elog(ERROR, "no > operator for opclass %u", opclass);
 	}
 
 	opr1right = network_scan_first(rightop);
@@ -2203,8 +2201,7 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop)
 
 	opr2oid = get_opclass_member(opclass, BTLessEqualStrategyNumber);
 	if (opr2oid == InvalidOid)
-		elog(ERROR, "network_prefix_quals: no <= operator for opclass %u",
-			 opclass);
+		elog(ERROR, "no <= operator for opclass %u", opclass);
 
 	opr2right = network_scan_last(rightop);
 
diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c
index c8175b81e39aabe88760caaf1120aacb705b5647..cf7c4ee433133f40687eaae88892b4fa597c38d6 100644
--- a/src/backend/optimizer/path/joinpath.c
+++ b/src/backend/optimizer/path/joinpath.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinpath.c,v 1.78 2003/02/08 20:20:54 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinpath.c,v 1.79 2003/07/25 00:01:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -165,7 +165,7 @@ sort_inner_and_outer(Query *root,
 			useallclauses = true;
 			break;
 		default:
-			elog(ERROR, "sort_inner_and_outer: unexpected join type %d",
+			elog(ERROR, "unrecognized join type: %d",
 				 (int) jointype);
 			useallclauses = false;		/* keep compiler quiet */
 			break;
@@ -363,7 +363,7 @@ match_unsorted_outer(Query *root,
 			useallclauses = true;
 			break;
 		default:
-			elog(ERROR, "match_unsorted_outer: unexpected join type %d",
+			elog(ERROR, "unrecognized join type: %d",
 				 (int) jointype);
 			nestjoinOK = false; /* keep compiler quiet */
 			useallclauses = false;
@@ -815,7 +815,9 @@ select_mergejoin_clauses(RelOptInfo *joinrel,
 				case JOIN_FULL:
 					if (restrictinfo->left_relids == NULL ||
 						restrictinfo->mergejoinoperator == InvalidOid)
-						elog(ERROR, "FULL JOIN is only supported with mergejoinable join conditions");
+						ereport(ERROR,
+								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+								 errmsg("FULL JOIN is only supported with mergejoinable join conditions")));
 					break;
 				default:
 					/* otherwise, it's OK to have nonmergeable join quals */
diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c
index 260b0cf6cd04ca891219d7caf57bba9b339e5436..023bc397840d5c3018fc2fd07699d2f7980a68a7 100644
--- a/src/backend/optimizer/path/joinrels.c
+++ b/src/backend/optimizer/path/joinrels.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinrels.c,v 1.60 2003/02/08 20:20:54 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinrels.c,v 1.61 2003/07/25 00:01:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -227,8 +227,7 @@ make_rels_by_joins(Query *root, int level, List **joinrels)
 		}
 
 		if (result_rels == NIL)
-			elog(ERROR, "make_rels_by_joins: failed to build any %d-way joins",
-				 level);
+			elog(ERROR, "failed to build any %d-way joins", level);
 	}
 
 	return result_rels;
@@ -367,8 +366,8 @@ make_jointree_rel(Query *root, Node *jtnode)
 		/* Make this join rel */
 		rel = make_join_rel(root, lrel, rrel, j->jointype);
 
-		if (rel == NULL)
-			elog(ERROR, "make_jointree_rel: invalid join order!?");
+		if (rel == NULL)		/* oops */
+			elog(ERROR, "invalid join order");
 
 		/*
 		 * Since we are only going to consider this one way to do it,
@@ -385,8 +384,8 @@ make_jointree_rel(Query *root, Node *jtnode)
 		return rel;
 	}
 	else
-		elog(ERROR, "make_jointree_rel: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 	return NULL;				/* keep compiler quiet */
 }
 
@@ -571,7 +570,7 @@ make_join_rel(Query *root, RelOptInfo *rel1, RelOptInfo *rel2,
 								 restrictlist);
 			break;
 		default:
-			elog(ERROR, "make_join_rel: unsupported join type %d",
+			elog(ERROR, "unrecognized join type: %d",
 				 (int) jointype);
 			break;
 	}
diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c
index d4d1179d54576d49ebe9346937942fbb265344f3..9fec73e2603bd7ff77c8df602dd9b8c87899b821 100644
--- a/src/backend/optimizer/path/pathkeys.c
+++ b/src/backend/optimizer/path/pathkeys.c
@@ -11,7 +11,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/pathkeys.c,v 1.50 2003/06/29 23:05:04 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/pathkeys.c,v 1.51 2003/07/25 00:01:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1089,7 +1089,7 @@ make_pathkeys_for_mergeclauses(Query *root,
 		}
 		else
 		{
-			elog(ERROR, "make_pathkeys_for_mergeclauses: can't identify which side of mergeclause to use");
+			elog(ERROR, "could not identify which side of mergeclause to use");
 			pathkey = NIL;	/* keep compiler quiet */
 		}
 
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 79855b00c0f97594b78cd91e540d5021b8ae5151..9ac746d34a9cfec76a1bffe09ef95c1361adbe5c 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.148 2003/07/14 22:35:54 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.149 2003/07/25 00:01:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -155,8 +155,8 @@ create_plan(Query *root, Path *best_path)
 											   (UniquePath *) best_path);
 			break;
 		default:
-			elog(ERROR, "create_plan: unknown pathtype %d",
-				 best_path->pathtype);
+			elog(ERROR, "unrecognized node type: %d",
+				 (int) best_path->pathtype);
 			plan = NULL;		/* keep compiler quiet */
 			break;
 	}
@@ -252,8 +252,8 @@ create_scan_plan(Query *root, Path *best_path)
 			break;
 
 		default:
-			elog(ERROR, "create_scan_plan: unknown node type: %d",
-				 best_path->pathtype);
+			elog(ERROR, "unrecognized node type: %d",
+				 (int) best_path->pathtype);
 			plan = NULL;		/* keep compiler quiet */
 			break;
 	}
@@ -384,8 +384,8 @@ create_join_plan(Query *root, JoinPath *best_path)
 												 inner_plan);
 			break;
 		default:
-			elog(ERROR, "unsupported node type %d",
-				 best_path->path.pathtype);
+			elog(ERROR, "unrecognized node type: %d",
+				 (int) best_path->path.pathtype);
 			plan = NULL;		/* keep compiler quiet */
 			break;
 	}
@@ -1172,7 +1172,7 @@ fix_indxqual_sublist(List *indexqual,
 		Oid			opclass;
 
 		if (!IsA(clause, OpExpr) || length(clause->args) != 2)
-			elog(ERROR, "fix_indxqual_sublist: indexqual clause is not binary opclause");
+			elog(ERROR, "indexqual clause is not binary opclause");
 
 		/*
 		 * Make a copy that will become the fixed clause.
@@ -1287,7 +1287,7 @@ fix_indxqual_operand(Node *node, int baserelid, IndexOptInfo *index,
 	}
 
 	/* Ooops... */
-	elog(ERROR, "fix_indxqual_operand: node is not index attribute");
+	elog(ERROR, "node is not an index attribute");
 	return NULL;				/* keep compiler quiet */
 }
 
@@ -1804,7 +1804,7 @@ make_sort_from_pathkeys(Query *root, Plan *lefttree,
 					break;
 			}
 			if (!j)
-				elog(ERROR, "make_sort_from_pathkeys: cannot find pathkey item to sort");
+				elog(ERROR, "could not find pathkey item to sort");
 			/*
 			 * Do we need to insert a Result node?
 			 *
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index cbc1ff365a31b9ddd7c99c91d5248ebdfeb17bc8..02f6e0c4c500838c77162f6e2c5e767f837e19af 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.86 2003/06/29 23:05:04 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.87 2003/07/25 00:01:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -97,11 +97,13 @@ add_base_rels_to_query(Query *root, Node *jtnode)
 		 * Safety check: join RTEs should not be SELECT FOR UPDATE targets
 		 */
 		if (intMember(j->rtindex, root->rowMarks))
-			elog(ERROR, "SELECT FOR UPDATE cannot be applied to a join");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("SELECT FOR UPDATE cannot be applied to a join")));
 	}
 	else
-		elog(ERROR, "add_base_rels_to_query: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 }
 
 
@@ -284,11 +286,12 @@ distribute_quals_to_rels(Query *root, Node *jtnode)
 				 * This is where we fail if upper levels of planner
 				 * haven't rewritten UNION JOIN as an Append ...
 				 */
-				elog(ERROR, "UNION JOIN is not implemented yet");
+				ereport(ERROR,
+						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+						 errmsg("UNION JOIN is not implemented yet")));
 				break;
 			default:
-				elog(ERROR,
-					 "distribute_quals_to_rels: unsupported join type %d",
+				elog(ERROR, "unrecognized join type: %d",
 					 (int) j->jointype);
 				break;
 		}
@@ -302,8 +305,8 @@ distribute_quals_to_rels(Query *root, Node *jtnode)
 			mark_baserels_for_outer_join(root, nullable_rels, result);
 	}
 	else
-		elog(ERROR, "distribute_quals_to_rels: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 	return result;
 }
 
@@ -340,7 +343,9 @@ mark_baserels_for_outer_join(Query *root, Relids rels, Relids outerrels)
 		if (rel->outerjoinset == NULL)
 		{
 			if (intMember(relno, root->rowMarks))
-				elog(ERROR, "SELECT FOR UPDATE cannot be applied to the nullable side of an OUTER JOIN");
+				ereport(ERROR,
+						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+						 errmsg("SELECT FOR UPDATE cannot be applied to the nullable side of an OUTER JOIN")));
 		}
 
 		rel->outerjoinset = outerrels;
@@ -603,7 +608,7 @@ distribute_qual_to_rels(Query *root, Node *clause,
 			 * 'clause' references no rels, and therefore we have no place to
 			 * attach it.  Shouldn't get here if callers are working properly.
 			 */
-			elog(ERROR, "distribute_qual_to_rels: can't cope with variable-free clause");
+			elog(ERROR, "cannot cope with variable-free clause");
 			break;
 	}
 
@@ -745,8 +750,10 @@ process_implied_equality(Query *root,
 		 * we have no suitable equality operator for the combination of
 		 * datatypes?  NO, because sortkey selection may screw up anyway.
 		 */
-		elog(ERROR, "Unable to identify an equality operator for types '%s' and '%s'",
-			 format_type_be(ltype), format_type_be(rtype));
+		ereport(ERROR,
+				(errcode(ERRCODE_UNDEFINED_FUNCTION),
+				 errmsg("unable to identify an equality operator for types %s and %s",
+						format_type_be(ltype), format_type_be(rtype))));
 	}
 	pgopform = (Form_pg_operator) GETSTRUCT(eq_operator);
 
@@ -756,8 +763,10 @@ process_implied_equality(Query *root,
 	if (pgopform->oprlsortop != sortop1 ||
 		pgopform->oprrsortop != sortop2 ||
 		pgopform->oprresult != BOOLOID)
-		elog(ERROR, "Equality operator for types '%s' and '%s' should be mergejoinable, but isn't",
-			 format_type_be(ltype), format_type_be(rtype));
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
+				 errmsg("equality operator for types %s and %s should be mergejoinable, but isn't",
+						format_type_be(ltype), format_type_be(rtype))));
 
 	clause = make_opclause(oprid(eq_operator), /* opno */
 						   BOOLOID,	/* opresulttype */
diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c
index 97f6b76a8e48c61dd12414cfb1bfafa5fce605c8..8883310f66dabb6af8b7b72c3647b0d81153dc0b 100644
--- a/src/backend/optimizer/plan/planmain.c
+++ b/src/backend/optimizer/plan/planmain.c
@@ -14,7 +14,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.75 2003/03/10 03:53:50 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.76 2003/07/25 00:01:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -154,7 +154,7 @@ query_planner(Query *root, List *tlist, double tuple_fraction,
 	final_rel = make_one_rel(root);
 
 	if (!final_rel || !final_rel->cheapest_total_path)
-		elog(ERROR, "query_planner: failed to construct a relation");
+		elog(ERROR, "failed to construct the join relation");
 
 	/*
 	 * Now that we have an estimate of the final rel's size, we can
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index d058a44bb0d2d4570bb22f70adbc8c5a69c60439..1896982f02eacd96a81a4f3c9ed56ad3e362ddb7 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.156 2003/07/03 19:07:20 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.157 2003/07/25 00:01:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -448,8 +448,8 @@ preprocess_qual_conditions(Query *parse, Node *jtnode)
 		j->quals = preprocess_expression(parse, j->quals, EXPRKIND_QUAL);
 	}
 	else
-		elog(ERROR, "preprocess_qual_conditions: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 }
 
 /*--------------------
@@ -582,7 +582,9 @@ grouping_planner(Query *parse, double tuple_fraction)
 		 * already, but let's make sure).
 		 */
 		if (parse->rowMarks)
-			elog(ERROR, "SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT")));
 
 		/*
 		 * We set current_pathkeys NIL indicating we do not know sort
@@ -646,7 +648,9 @@ grouping_planner(Query *parse, double tuple_fraction)
 			 * level
 			 */
 			if (PlannerQueryLevel > 1)
-				elog(ERROR, "SELECT FOR UPDATE is not allowed in subselects");
+				ereport(ERROR,
+						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+						 errmsg("SELECT FOR UPDATE is not allowed in subselects")));
 
 			foreach(l, parse->rowMarks)
 			{
@@ -1491,7 +1495,7 @@ locate_grouping_columns(Query *parse,
 				break;
 		}
 		if (!sl)
-			elog(ERROR, "locate_grouping_columns: failed");
+			elog(ERROR, "failed to locate grouping columns");
 
 		groupColIdx[keyno++] = te->resdom->resno;
 	}
@@ -1504,7 +1508,7 @@ locate_grouping_columns(Query *parse,
  * We need to transpose sort key info from the orig_tlist into new_tlist.
  * NOTE: this would not be good enough if we supported resjunk sort keys
  * for results of set operations --- then, we'd need to project a whole
- * new tlist to evaluate the resjunk columns.  For now, just elog if we
+ * new tlist to evaluate the resjunk columns.  For now, just ereport if we
  * find any resjunk columns in orig_tlist.
  */
 static List *
@@ -1524,13 +1528,13 @@ postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
 		Assert(orig_tlist != NIL);
 		orig_tle = (TargetEntry *) lfirst(orig_tlist);
 		orig_tlist = lnext(orig_tlist);
-		if (orig_tle->resdom->resjunk)
-			elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");
+		if (orig_tle->resdom->resjunk) /* should not happen */
+			elog(ERROR, "resjunk output columns are not implemented");
 		Assert(new_tle->resdom->resno == orig_tle->resdom->resno);
 		Assert(new_tle->resdom->restype == orig_tle->resdom->restype);
 		new_tle->resdom->ressortgroupref = orig_tle->resdom->ressortgroupref;
 	}
 	if (orig_tlist != NIL)
-		elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");
+		elog(ERROR, "resjunk output columns are not implemented");
 	return new_tlist;
 }
diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index 1da186e0aaac6b89395f04a948893006225a257b..18395be022b6e2be465cd3b61fe908d83fee0e09 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.93 2003/06/29 00:33:43 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.94 2003/07/25 00:01:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -233,8 +233,8 @@ set_plan_references(Plan *plan, List *rtable)
 				set_plan_references((Plan *) lfirst(pl), rtable);
 			break;
 		default:
-			elog(ERROR, "set_plan_references: unknown plan type %d",
-				 nodeTag(plan));
+			elog(ERROR, "unrecognized node type: %d",
+				 (int) nodeTag(plan));
 			break;
 	}
 
@@ -584,7 +584,7 @@ join_references_mutator(Node *node,
 			return (Node *) copyObject(var);
 
 		/* No referent found for Var */
-		elog(ERROR, "join_references: variable not in subplan target lists");
+		elog(ERROR, "variable not found in subplan target lists");
 	}
 	/* Try matching more complex expressions too, if tlists have any */
 	if (context->tlists_have_non_vars)
@@ -680,7 +680,7 @@ replace_vars_with_subplan_refs_mutator(Node *node,
 
 		resdom = tlist_member((Node *) var, context->subplan_targetlist);
 		if (!resdom)
-			elog(ERROR, "replace_vars_with_subplan_refs: variable not in subplan target list");
+			elog(ERROR, "variable not found in subplan target list");
 		newvar = (Var *) copyObject(var);
 		newvar->varno = context->subvarno;
 		newvar->varattno = resdom->resno;
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index aee5f4a1a145ef4ddcf4a18d87babece8a6b1ec6..780bed6c2bfaea3aec47eeacb980dc5a3e845bbe 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.78 2003/06/25 21:30:30 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.79 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -359,7 +359,7 @@ make_subplan(SubLink *slink, List *lefthand, bool isTopQual)
 		Assert(!te->resdom->resjunk);
 		arraytype = get_array_type(te->resdom->restype);
 		if (!OidIsValid(arraytype))
-			elog(ERROR, "Cannot find array type for datatype %s",
+			elog(ERROR, "could not find array type for datatype %s",
 				 format_type_be(te->resdom->restype));
 		prm = generate_new_param(arraytype, -1);
 		node->setParam = makeListi1(prm->paramid);
@@ -1012,8 +1012,8 @@ finalize_plan(Plan *plan, List *rtable,
 			break;
 
 		default:
-			elog(ERROR, "finalize_plan: node %d unsupported",
-				 nodeTag(plan));
+			elog(ERROR, "unrecognized node type: %d",
+				 (int) nodeTag(plan));
 	}
 
 	/* Process left and right child plans, if any */
@@ -1032,7 +1032,7 @@ finalize_plan(Plan *plan, List *rtable,
 	/* Now we have all the paramids */
 
 	if (!bms_is_subset(context.paramids, valid_params))
-		elog(ERROR, "finalize_plan: plan shouldn't reference subplan's variable");
+		elog(ERROR, "plan should not reference subplan's variable");
 
 	plan->extParam = bms_intersect(context.paramids, outer_params);
 	plan->allParam = context.paramids;
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index a265623bfb705284e1bf6d0cb202d57458df50a9..6bddf04206fdec11616e2178387e48b7f3e5d3c7 100644
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -16,7 +16,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.7 2003/03/10 03:53:50 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.8 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -334,17 +334,19 @@ pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
 				 * This is where we fail if upper levels of planner
 				 * haven't rewritten UNION JOIN as an Append ...
 				 */
-				elog(ERROR, "UNION JOIN is not implemented yet");
+				ereport(ERROR,
+						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+						 errmsg("UNION JOIN is not implemented yet")));
 				break;
 			default:
-				elog(ERROR, "pull_up_subqueries: unexpected join type %d",
-					 j->jointype);
+				elog(ERROR, "unrecognized join type: %d",
+					 (int) j->jointype);
 				break;
 		}
 	}
 	else
-		elog(ERROR, "pull_up_subqueries: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 	return jtnode;
 }
 
@@ -363,7 +365,7 @@ is_simple_subquery(Query *subquery)
 		subquery->commandType != CMD_SELECT ||
 		subquery->resultRelation != 0 ||
 		subquery->into != NULL)
-		elog(ERROR, "is_simple_subquery: subquery is bogus");
+		elog(ERROR, "subquery is bogus");
 
 	/*
 	 * Can't currently pull up a query with setops. Maybe after querytree
@@ -478,8 +480,8 @@ resolvenew_in_jointree(Node *jtnode, int varno, List *subtlist)
 		 */
 	}
 	else
-		elog(ERROR, "resolvenew_in_jointree: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 }
 
 /*
@@ -524,7 +526,7 @@ reduce_outer_joins(Query *parse)
 
 	/* planner.c shouldn't have called me if no outer joins */
 	if (state == NULL || !state->contains_outer)
-		elog(ERROR, "reduce_outer_joins: so where are the outer joins?");
+		elog(ERROR, "so where are the outer joins?");
 
 	reduce_outer_joins_pass2((Node *) parse->jointree, state, parse, NULL);
 }
@@ -591,8 +593,8 @@ reduce_outer_joins_pass1(Node *jtnode)
 		result->sub_states = lappend(result->sub_states, sub_state);
 	}
 	else
-		elog(ERROR, "reduce_outer_joins_pass1: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 	return result;
 }
 
@@ -615,10 +617,10 @@ reduce_outer_joins_pass2(Node *jtnode,
 	 * because it's only called on subtrees marked as contains_outer.
 	 */
 	if (jtnode == NULL)
-		elog(ERROR, "reduce_outer_joins_pass2: reached empty jointree");
+		elog(ERROR, "reached empty jointree");
 	if (IsA(jtnode, RangeTblRef))
 	{
-		elog(ERROR, "reduce_outer_joins_pass2: reached base rel");
+		elog(ERROR, "reached base rel");
 	}
 	else if (IsA(jtnode, FromExpr))
 	{
@@ -735,8 +737,8 @@ reduce_outer_joins_pass2(Node *jtnode,
 		}
 	}
 	else
-		elog(ERROR, "reduce_outer_joins_pass2: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 }
 
 /*
@@ -972,8 +974,8 @@ simplify_jointree(Query *parse, Node *jtnode)
 		}
 	}
 	else
-		elog(ERROR, "simplify_jointree: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 	return jtnode;
 }
 
@@ -1043,8 +1045,8 @@ get_relids_in_jointree(Node *jtnode)
 		result = bms_join(result, get_relids_in_jointree(j->rarg));
 	}
 	else
-		elog(ERROR, "get_relids_in_jointree: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 	return result;
 }
 
@@ -1061,7 +1063,7 @@ get_relids_for_join(Query *parse, int joinrelid)
 
 	jtnode = find_jointree_node_for_rel((Node *) parse->jointree, joinrelid);
 	if (!jtnode)
-		elog(ERROR, "get_relids_for_join: join node %d not found", joinrelid);
+		elog(ERROR, "could not find join node %d", joinrelid);
 	return get_relids_in_jointree(jtnode);
 }
 
@@ -1108,7 +1110,7 @@ find_jointree_node_for_rel(Node *jtnode, int relid)
 			return jtnode;
 	}
 	else
-		elog(ERROR, "find_jointree_node_for_rel: unexpected node type %d",
-			 nodeTag(jtnode));
+		elog(ERROR, "unrecognized node type: %d",
+			 (int) nodeTag(jtnode));
 	return NULL;
 }
diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c
index 00132a53cde494c70df21733a91d02bd0dbb8f77..9c937f176029e86f73c1f0c702b686331c258fce 100644
--- a/src/backend/optimizer/prep/preptlist.c
+++ b/src/backend/optimizer/prep/preptlist.c
@@ -15,7 +15,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.61 2003/05/12 00:17:03 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.62 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -55,7 +55,7 @@ preprocess_targetlist(List *tlist,
 		RangeTblEntry *rte = rt_fetch(result_relation, range_table);
 
 		if (rte->subquery != NULL || rte->relid == InvalidOid)
-			elog(ERROR, "preprocess_targetlist: subquery cannot be result relation");
+			elog(ERROR, "subquery cannot be result relation");
 	}
 
 	/*
@@ -236,7 +236,8 @@ expand_targetlist(List *tlist, int command_type,
 					}
 					break;
 				default:
-					elog(ERROR, "expand_targetlist: unexpected command_type");
+					elog(ERROR, "unrecognized command_type: %d",
+						 (int) command_type);
 					new_expr = NULL;	/* keep compiler quiet */
 					break;
 			}
@@ -266,7 +267,7 @@ expand_targetlist(List *tlist, int command_type,
 		Resdom	   *resdom = old_tle->resdom;
 
 		if (!resdom->resjunk)
-			elog(ERROR, "expand_targetlist: targetlist is not sorted correctly");
+			elog(ERROR, "targetlist is not sorted correctly");
 		/* Get the resno right, but don't copy unnecessarily */
 		if (resdom->resno != attrno)
 		{
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index 029227a4ca62418b33b845f80c6e930eddc1f100..065f5d9ccacf6639bf9d6d6aaf781023a716afa2 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -14,7 +14,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.99 2003/07/20 21:56:34 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.100 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -195,7 +195,7 @@ recurse_set_operations(Node *setOp, Query *parse,
 	}
 	else
 	{
-		elog(ERROR, "recurse_set_operations: unexpected node %d",
+		elog(ERROR, "unrecognized node type: %d",
 			 (int) nodeTag(setOp));
 		return NULL;			/* keep compiler quiet */
 	}
@@ -312,7 +312,8 @@ generate_nonunion_plan(SetOperationStmt *op, Query *parse,
 			cmd = op->all ? SETOPCMD_EXCEPT_ALL : SETOPCMD_EXCEPT;
 			break;
 		default:
-			elog(ERROR, "generate_nonunion_plan: bogus operation code");
+			elog(ERROR, "unrecognized set operation code: %d",
+				 (int) op->op);
 			cmd = SETOPCMD_INTERSECT;	/* keep compiler quiet */
 			break;
 	}
diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index d35cbee92e7c263c509cfcf9a600a35cd330237c..5df7e1c3cb30a8c37b5476ff46e4395612b22f59 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.146 2003/07/03 19:07:25 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.147 2003/07/25 00:01:08 tgl Exp $
  *
  * HISTORY
  *	  AUTHOR			DATE			MAJOR EVENT
@@ -404,7 +404,9 @@ count_agg_clause_walker(Node *node, int *count)
 		 * nested agg functions are semantically nonsensical.
 		 */
 		if (contain_agg_clause((Node *) ((Aggref *) node)->target))
-			elog(ERROR, "Aggregate function calls may not be nested");
+			ereport(ERROR,
+					(errcode(ERRCODE_GROUPING_ERROR),
+					 errmsg("aggregate function calls may not be nested")));
 
 		/*
 		 * Having checked that, we need not recurse into the argument.
@@ -982,14 +984,15 @@ CommuteClause(OpExpr *clause)
 	Oid			opoid;
 	Node	   *temp;
 
+	/* Sanity checks: caller is at fault if these fail */
 	if (!is_opclause(clause) ||
 		length(clause->args) != 2)
-		elog(ERROR, "CommuteClause: applied to non-binary-operator clause");
+		elog(ERROR, "cannot commute non-binary-operator clause");
 
 	opoid = get_commutator(clause->opno);
 
 	if (!OidIsValid(opoid))
-		elog(ERROR, "CommuteClause: no commutator for operator %u",
+		elog(ERROR, "could not find commutator for operator %u",
 			 clause->opno);
 
 	/*
@@ -1346,7 +1349,7 @@ eval_const_expressions_mutator(Node *node, List *active_fns)
 				/* Else we still need a NOT node */
 				return (Node *) make_notclause(lfirst(args));
 			default:
-				elog(ERROR, "eval_const_expressions: unexpected boolop %d",
+				elog(ERROR, "unrecognized boolop: %d",
 					 (int) expr->boolop);
 				break;
 		}
@@ -1357,7 +1360,7 @@ eval_const_expressions_mutator(Node *node, List *active_fns)
 		 * Return a SubPlan unchanged --- too late to do anything
 		 * with it.
 		 *
-		 * XXX should we elog() here instead?  Probably this routine
+		 * XXX should we ereport() here instead?  Probably this routine
 		 * should never be invoked after SubPlan creation.
 		 */
 		return node;
@@ -1610,7 +1613,7 @@ simplify_function(Oid funcid, Oid result_type, List *args,
 								ObjectIdGetDatum(funcid),
 								0, 0, 0);
 	if (!HeapTupleIsValid(func_tuple))
-		elog(ERROR, "Function OID %u does not exist", funcid);
+		elog(ERROR, "cache lookup failed for function %u", funcid);
 
 	newexpr = evaluate_function(funcid, result_type, args, func_tuple);
 
@@ -1794,8 +1797,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
 						  Anum_pg_proc_prosrc,
 						  &isNull);
 	if (isNull)
-		elog(ERROR, "inline_function: null prosrc for procedure %u",
-			 funcid);
+		elog(ERROR, "null prosrc for function %u", funcid);
 	src = DatumGetCString(DirectFunctionCall1(textout, tmp));
 
 	/*
@@ -1961,9 +1963,9 @@ substitute_actual_parameters_mutator(Node *node,
 		Param	   *param = (Param *) node;
 
 		if (param->paramkind != PARAM_NUM)
-			elog(ERROR, "substitute_actual_parameters_mutator: unexpected paramkind");
+			elog(ERROR, "unexpected paramkind: %d", param->paramkind);
 		if (param->paramid <= 0 || param->paramid > context->nargs)
-			elog(ERROR, "substitute_actual_parameters_mutator: unexpected paramid");
+			elog(ERROR, "invalid paramid: %d", param->paramid);
 
 		/* Count usage of parameter */
 		context->usecounts[param->paramid - 1]++;
@@ -2350,8 +2352,8 @@ expression_tree_walker(Node *node,
 			}
 			break;
 		default:
-			elog(ERROR, "expression_tree_walker: Unexpected node type %d",
-				 nodeTag(node));
+			elog(ERROR, "unrecognized node type: %d",
+				 (int) nodeTag(node));
 			break;
 	}
 	return false;
@@ -2816,8 +2818,8 @@ expression_tree_mutator(Node *node,
 			}
 			break;
 		default:
-			elog(ERROR, "expression_tree_mutator: Unexpected node type %d",
-				 nodeTag(node));
+			elog(ERROR, "unrecognized node type: %d",
+				 (int) nodeTag(node));
 			break;
 	}
 	/* can't get here, but keep compiler happy */
diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c
index 19198bf2494c88c2495b1d2999e6f22b187bc296..61ab51747c13405027d7c3e5bc038589c0edccef 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/pathnode.c,v 1.92 2003/07/14 22:35:54 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/pathnode.c,v 1.93 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -133,7 +133,7 @@ set_cheapest(RelOptInfo *parent_rel)
 	Assert(IsA(parent_rel, RelOptInfo));
 
 	if (pathlist == NIL)
-		elog(ERROR, "Unable to devise a query plan for the given query");
+		elog(ERROR, "could not devise a query plan for the given query");
 
 	cheapest_startup_path = cheapest_total_path = (Path *) lfirst(pathlist);
 
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index 3f4fe661717dbd405a971aac095fcfdc1e20df89..6823f03267eba1fde2f89f5ff34ba4abfe6b10e4 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.84 2003/06/29 23:05:04 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.85 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -258,7 +258,7 @@ restriction_selectivity(Query *root,
 											 Int32GetDatum(varRelid)));
 
 	if (result < 0.0 || result > 1.0)
-		elog(ERROR, "restriction_selectivity: bad value %f", result);
+		elog(ERROR, "invalid restriction selectivity: %f", result);
 
 	return (Selectivity) result;
 }
@@ -293,7 +293,7 @@ join_selectivity(Query *root,
 											 Int16GetDatum(jointype)));
 
 	if (result < 0.0 || result > 1.0)
-		elog(ERROR, "join_selectivity: bad value %f", result);
+		elog(ERROR, "invalid join selectivity: %f", result);
 
 	return (Selectivity) result;
 }
@@ -365,7 +365,7 @@ has_subclass(Oid relationId)
 						   ObjectIdGetDatum(relationId),
 						   0, 0, 0);
 	if (!HeapTupleIsValid(tuple))
-		elog(ERROR, "has_subclass: Relation %u not found", relationId);
+		elog(ERROR, "cache lookup failed for relation %u", relationId);
 
 	result = ((Form_pg_class) GETSTRUCT(tuple))->relhassubclass;
 	ReleaseSysCache(tuple);
diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index 0b82569ba375ebde7478a436837aa1d09663e860..9a4a2069765f28d2ef9c439b5fb86ed90c67de2f 100644
--- a/src/backend/optimizer/util/relnode.c
+++ b/src/backend/optimizer/util/relnode.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/relnode.c,v 1.49 2003/06/29 23:05:04 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/relnode.c,v 1.50 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -55,7 +55,7 @@ build_base_rel(Query *root, int relid)
 	{
 		rel = (RelOptInfo *) lfirst(rels);
 		if (relid == rel->relid)
-			elog(ERROR, "build_base_rel: rel already exists");
+			elog(ERROR, "rel already exists");
 	}
 
 	/* It should not exist as an "other" rel, either */
@@ -63,7 +63,7 @@ build_base_rel(Query *root, int relid)
 	{
 		rel = (RelOptInfo *) lfirst(rels);
 		if (relid == rel->relid)
-			elog(ERROR, "build_base_rel: rel already exists as 'other' rel");
+			elog(ERROR, "rel already exists as \"other\" rel");
 	}
 
 	/* No existing RelOptInfo for this base rel, so make a new one */
@@ -98,7 +98,7 @@ build_other_rel(Query *root, int relid)
 	{
 		rel = (RelOptInfo *) lfirst(rels);
 		if (relid == rel->relid)
-			elog(ERROR, "build_other_rel: rel already exists as base rel");
+			elog(ERROR, "rel already exists as base rel");
 	}
 
 	/* No existing RelOptInfo for this other rel, so make a new one */
@@ -165,7 +165,7 @@ make_base_rel(Query *root, int relid)
 			rel->max_attr = length(rte->eref->colnames);
 			break;
 		default:
-			elog(ERROR, "make_base_rel: unsupported RTE kind %d",
+			elog(ERROR, "unrecognized RTE kind: %d",
 				 (int) rte->rtekind);
 			break;
 	}
@@ -211,7 +211,7 @@ find_base_rel(Query *root, int relid)
 			return rel;
 	}
 
-	elog(ERROR, "find_base_rel: no relation entry for relid %d", relid);
+	elog(ERROR, "no relation entry for relid %d", relid);
 
 	return NULL;				/* keep compiler quiet */
 }
diff --git a/src/backend/optimizer/util/tlist.c b/src/backend/optimizer/util/tlist.c
index 7a4422a3594195a30a53b5ba24b8d25b5a994810..26d7d6fb245d12e86805fefef43c8bc56e01ff3b 100644
--- a/src/backend/optimizer/util/tlist.c
+++ b/src/backend/optimizer/util/tlist.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/tlist.c,v 1.57 2003/06/29 23:05:05 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/tlist.c,v 1.58 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -183,7 +183,7 @@ get_sortgroupclause_tle(SortClause *sortClause,
 			return tle;
 	}
 
-	elog(ERROR, "get_sortgroupclause_tle: ORDER/GROUP BY expression not found in targetlist");
+	elog(ERROR, "ORDER/GROUP BY expression not found in targetlist");
 	return NULL;				/* keep compiler quiet */
 }
 
diff --git a/src/backend/rewrite/rewriteDefine.c b/src/backend/rewrite/rewriteDefine.c
index fb7412ca053f911cf4503b7fc6fcca0df8f7450b..df4e563eeb32e99a02d89a5518fe6bebdd6ccd34 100644
--- a/src/backend/rewrite/rewriteDefine.c
+++ b/src/backend/rewrite/rewriteDefine.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.82 2003/01/17 02:01:16 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.83 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -29,6 +29,7 @@
 #include "storage/smgr.h"
 #include "utils/acl.h"
 #include "utils/builtins.h"
+#include "utils/lsyscache.h"
 #include "utils/syscache.h"
 
 
@@ -97,8 +98,10 @@ InsertRule(char *rulname,
 	if (HeapTupleIsValid(oldtup))
 	{
 		if (!replace)
-			elog(ERROR, "Attempt to insert rule \"%s\" failed: already exists",
-				 rulname);
+			ereport(ERROR,
+					(errcode(ERRCODE_DUPLICATE_OBJECT),
+					 errmsg("rule \"%s\" for relation \"%s\" already exists",
+							rulname, get_rel_name(eventrel_oid))));
 
 		/*
 		 * When replacing, we don't need to replace every attribute
@@ -224,11 +227,15 @@ DefineQueryRewrite(RuleStmt *stmt)
 		if (query != getInsertSelectQuery(query, NULL))
 			continue;
 		if (query->resultRelation == PRS2_OLD_VARNO)
-			elog(ERROR, "rule actions on OLD currently not supported"
-				 "\n\tuse views or triggers instead");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("rule actions on OLD are not implemented"),
+					 errhint("Use views or triggers instead.")));
 		if (query->resultRelation == PRS2_NEW_VARNO)
-			elog(ERROR, "rule actions on NEW currently not supported"
-				 "\n\tuse triggers instead");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("rule actions on NEW are not implemented"),
+					 errhint("Use triggers instead.")));
 	}
 
 	/*
@@ -243,29 +250,35 @@ DefineQueryRewrite(RuleStmt *stmt)
 		 * So there cannot be INSTEAD NOTHING, ...
 		 */
 		if (length(action) == 0)
-		{
-			elog(ERROR, "instead nothing rules on select currently not supported"
-				 "\n\tuse views instead");
-		}
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("INSTEAD NOTHING rules on select are not implemented"),
+					 errhint("Use views instead.")));
 
 		/*
 		 * ... there cannot be multiple actions, ...
 		 */
 		if (length(action) > 1)
-			elog(ERROR, "multiple action rules on select currently not supported");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("multiple action rules on select are not implemented")));
 
 		/*
 		 * ... the one action must be a SELECT, ...
 		 */
 		query = (Query *) lfirst(action);
 		if (!is_instead || query->commandType != CMD_SELECT)
-			elog(ERROR, "only instead-select rules currently supported on select");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("only instead-select rules are currently supported on select")));
 
 		/*
 		 * ... there can be no rule qual, ...
 		 */
 		if (event_qual != NULL)
-			elog(ERROR, "event qualifications not supported for rules on select");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("event qualifications are not implemented for rules on select")));
 
 		/*
 		 * ... the targetlist of the SELECT action must exactly match the
@@ -283,7 +296,9 @@ DefineQueryRewrite(RuleStmt *stmt)
 				continue;
 			i++;
 			if (i > event_relation->rd_att->natts)
-				elog(ERROR, "select rule's target list has too many entries");
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+						 errmsg("select rule's target list has too many entries")));
 
 			attr = event_relation->rd_att->attrs[i - 1];
 			attname = NameStr(attr->attname);
@@ -297,13 +312,19 @@ DefineQueryRewrite(RuleStmt *stmt)
 			 * positions.
 			 */
 			if (attr->attisdropped)
-				elog(ERROR, "cannot convert relation containing dropped columns to view");
+				ereport(ERROR,
+						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+						 errmsg("cannot convert relation containing dropped columns to view")));
 
 			if (strcmp(resdom->resname, attname) != 0)
-				elog(ERROR, "select rule's target entry %d has different column name from %s", i, attname);
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+						 errmsg("select rule's target entry %d has different column name from \"%s\"", i, attname)));
 
 			if (attr->atttypid != resdom->restype)
-				elog(ERROR, "select rule's target entry %d has different type from attribute %s", i, attname);
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+						 errmsg("select rule's target entry %d has different type from attribute \"%s\"", i, attname)));
 
 			/*
 			 * Allow typmods to be different only if one of them is -1,
@@ -314,11 +335,15 @@ DefineQueryRewrite(RuleStmt *stmt)
 			 */
 			if (attr->atttypmod != resdom->restypmod &&
 				attr->atttypmod != -1 && resdom->restypmod != -1)
-				elog(ERROR, "select rule's target entry %d has different size from attribute %s", i, attname);
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+						 errmsg("select rule's target entry %d has different size from attribute \"%s\"", i, attname)));
 		}
 
 		if (i != event_relation->rd_att->natts)
-			elog(ERROR, "select rule's target list has too few entries");
+			ereport(ERROR,
+					(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+					 errmsg("select rule's target list has too few entries")));
 
 		/*
 		 * ... there must not be another ON SELECT rule already ...
@@ -331,8 +356,10 @@ DefineQueryRewrite(RuleStmt *stmt)
 
 				rule = event_relation->rd_rules->rules[i];
 				if (rule->event == CMD_SELECT)
-					elog(ERROR, "\"%s\" is already a view",
-						 RelationGetRelationName(event_relation));
+					ereport(ERROR,
+							(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+							 errmsg("\"%s\" is already a view",
+									RelationGetRelationName(event_relation))));
 			}
 		}
 
@@ -353,8 +380,10 @@ DefineQueryRewrite(RuleStmt *stmt)
 			if (strncmp(stmt->rulename, "_RET", 4) != 0 ||
 				strncmp(stmt->rulename + 4, event_obj->relname,
 						NAMEDATALEN - 4 - 4) != 0)
-				elog(ERROR, "view rule for \"%s\" must be named \"%s\"",
-					 event_obj->relname, ViewSelectRuleName);
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+						 errmsg("view rule for \"%s\" must be named \"%s\"",
+								event_obj->relname, ViewSelectRuleName)));
 			stmt->rulename = pstrdup(ViewSelectRuleName);
 		}
 
@@ -370,8 +399,10 @@ DefineQueryRewrite(RuleStmt *stmt)
 
 			scanDesc = heap_beginscan(event_relation, SnapshotNow, 0, NULL);
 			if (heap_getnext(scanDesc, ForwardScanDirection) != NULL)
-				elog(ERROR, "Relation \"%s\" is not empty. Cannot convert it to view",
-					 event_obj->relname);
+				ereport(ERROR,
+						(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						 errmsg("cannot convert non-empty table \"%s\" to a view",
+								event_obj->relname)));
 			heap_endscan(scanDesc);
 
 			RelisBecomingView = true;
@@ -509,12 +540,17 @@ RenameRewriteRule(Oid owningRel, const char *oldName,
 								 PointerGetDatum(oldName),
 								 0, 0);
 	if (!HeapTupleIsValid(ruletup))
-		elog(ERROR, "RenameRewriteRule: rule \"%s\" does not exist", oldName);
+		ereport(ERROR,
+				(errcode(ERRCODE_UNDEFINED_OBJECT),
+				 errmsg("rule \"%s\" for relation \"%s\" does not exist",
+						oldName, get_rel_name(owningRel))));
 
 	/* should not already exist */
 	if (IsDefinedRewriteRule(owningRel, newName))
-		elog(ERROR, "Attempt to rename rule \"%s\" failed: \"%s\" already exists",
-			 oldName, newName);
+		ereport(ERROR,
+				(errcode(ERRCODE_DUPLICATE_OBJECT),
+				 errmsg("rule \"%s\" for relation \"%s\" already exists",
+						newName, get_rel_name(owningRel))));
 
 	namestrcpy(&(((Form_pg_rewrite) GETSTRUCT(ruletup))->rulename), newName);
 
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
index 7ee28291e0d9e9ed58b7645fb67a12ff6d99f0f4..34b6ceece9bf814a75633c53e0be597e6897e7d3 100644
--- a/src/backend/rewrite/rewriteHandler.c
+++ b/src/backend/rewrite/rewriteHandler.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.123 2003/07/16 17:25:48 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.124 2003/07/25 00:01:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -168,7 +168,9 @@ rewriteRuleAction(Query *parsetree,
 			 * member statements of the setop?)
 			 */
 			if (sub_action->setOperations != NULL)
-				elog(ERROR, "Conditional UNION/INTERSECT/EXCEPT statements are not implemented");
+				ereport(ERROR,
+						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+						 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
 
 			sub_action->jointree->fromlist =
 				nconc(newjointree, sub_action->jointree->fromlist);
@@ -407,8 +409,7 @@ rewriteTargetList(Query *parsetree, Relation target_relation)
 		{
 			/* Let's just make sure we processed all the non-junk items */
 			if (resdom->resno < 1 || resdom->resno > numattrs)
-				elog(ERROR, "rewriteTargetList: bogus resno %d in targetlist",
-					 resdom->resno);
+				elog(ERROR, "bogus resno %d in targetlist", resdom->resno);
 		}
 	}
 
@@ -449,8 +450,10 @@ process_matched_tle(TargetEntry *src_tle,
 		((ArrayRef *) prior_tle->expr)->refassgnexpr == NULL ||
 		((ArrayRef *) src_tle->expr)->refrestype !=
 		((ArrayRef *) prior_tle->expr)->refrestype)
-		elog(ERROR, "Multiple assignments to same attribute \"%s\"",
-			 resdom->resname);
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("multiple assignments to same attribute \"%s\"",
+						resdom->resname)));
 
 	/*
 	 * Prior TLE could be a nest of ArrayRefs if we do this more than
@@ -461,8 +464,10 @@ process_matched_tle(TargetEntry *src_tle,
 		   ((ArrayRef *) priorbottom)->refassgnexpr != NULL)
 		priorbottom = (Node *) ((ArrayRef *) priorbottom)->refexpr;
 	if (!equal(priorbottom, ((ArrayRef *) src_tle->expr)->refexpr))
-		elog(ERROR, "Multiple assignments to same attribute \"%s\"",
-			 resdom->resname);
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("multiple assignments to same attribute \"%s\"",
+						resdom->resname)));
 
 	/*
 	 * Looks OK to nest 'em.
@@ -550,12 +555,14 @@ build_column_default(Relation rel, int attrno)
 	 * type when it was created ...
 	 */
 	if (expr == NULL)
-		elog(ERROR, "Column \"%s\" is of type %s"
-			 " but default expression is of type %s"
-			 "\n\tYou will need to rewrite or cast the expression",
-			 NameStr(att_tup->attname),
-			 format_type_be(atttype),
-			 format_type_be(exprtype));
+		ereport(ERROR,
+				(errcode(ERRCODE_DATATYPE_MISMATCH),
+				 errmsg("column \"%s\" is of type %s"
+						" but default expression is of type %s",
+						NameStr(att_tup->attname),
+						format_type_be(atttype),
+						format_type_be(exprtype)),
+				 errhint("You will need to rewrite or cast the expression.")));
 
 	return expr;
 }
@@ -619,11 +626,11 @@ ApplyRetrieveRule(Query *parsetree,
 			   *subrte;
 
 	if (length(rule->actions) != 1)
-		elog(ERROR, "ApplyRetrieveRule: expected just one rule action");
+		elog(ERROR, "expected just one rule action");
 	if (rule->qual != NULL)
-		elog(ERROR, "ApplyRetrieveRule: can't handle qualified ON SELECT rule");
+		elog(ERROR, "cannot handle qualified ON SELECT rule");
 	if (!relation_level)
-		elog(ERROR, "ApplyRetrieveRule: can't handle per-attribute ON SELECT rule");
+		elog(ERROR, "cannot handle per-attribute ON SELECT rule");
 
 	/*
 	 * Make a modifiable copy of the view query, and recursively expand
@@ -872,8 +879,10 @@ fireRIRrules(Query *parsetree, List *activeRIRs)
 			List	   *l;
 
 			if (oidMember(RelationGetRelid(rel), activeRIRs))
-				elog(ERROR, "Infinite recursion detected in rules for relation %s",
-					 RelationGetRelationName(rel));
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+						 errmsg("infinite recursion detected in rules for relation \"%s\"",
+								RelationGetRelationName(rel))));
 			newActiveRIRs = lconso(RelationGetRelid(rel), activeRIRs);
 
 			foreach(l, locks)
@@ -913,7 +922,7 @@ fireRIRrules(Query *parsetree, List *activeRIRs)
 		parsetree->hasAggs = checkExprHasAggs((Node *) parsetree);
 		if (parsetree->hasAggs)
 			if (checkExprHasAggs((Node *) parsetree->jointree))
-				elog(ERROR, "fireRIRrules: failed to remove aggs from qual");
+				elog(ERROR, "failed to remove aggregates from qual");
 	}
 	if (parsetree->hasSubLinks)
 		parsetree->hasSubLinks = checkExprHasSubLink((Node *) parsetree);
@@ -1151,8 +1160,10 @@ RewriteQuery(Query *parsetree, List *rewrite_events)
 					rev = (rewrite_event *) lfirst(n);
 					if (rev->relation == RelationGetRelid(rt_entry_relation) &&
 						rev->event == event)
-						elog(ERROR, "Infinite recursion detected in rules for relation %s",
-							 RelationGetRelationName(rt_entry_relation));
+						ereport(ERROR,
+								(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+								 errmsg("infinite recursion detected in rules for relation \"%s\"",
+										RelationGetRelationName(rt_entry_relation))));
 				}
 
 				rev = (rewrite_event *) palloc(sizeof(rewrite_event));
@@ -1259,19 +1270,25 @@ QueryRewrite(Query *parsetree)
 				switch (query->commandType)
 				{
 					case CMD_INSERT:
-						elog(ERROR, "Cannot insert into a view"
-							 "\n\tYou need an unconditional ON INSERT DO INSTEAD rule");
+						ereport(ERROR,
+								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+								 errmsg("cannot insert into a view"),
+								 errhint("You need an unconditional ON INSERT DO INSTEAD rule.")));
 						break;
 					case CMD_UPDATE:
-						elog(ERROR, "Cannot update a view"
-							 "\n\tYou need an unconditional ON UPDATE DO INSTEAD rule");
+						ereport(ERROR,
+								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+								 errmsg("cannot update a view"),
+								 errhint("You need an unconditional ON UPDATE DO INSTEAD rule.")));
 						break;
 					case CMD_DELETE:
-						elog(ERROR, "Cannot delete from a view"
-							 "\n\tYou need an unconditional ON DELETE DO INSTEAD rule");
+						ereport(ERROR,
+								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+								 errmsg("cannot delete from a view"),
+								 errhint("You need an unconditional ON DELETE DO INSTEAD rule.")));
 						break;
 					default:
-						elog(ERROR, "QueryRewrite: unexpected commandType %d",
+						elog(ERROR, "unrecognized commandType: %d",
 							 (int) query->commandType);
 						break;
 				}
diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c
index 1cd42a44f76fccd3125eb3168fb696d7cda5264f..f36f96c349167eb0e428eb4ade4b487815f47eb0 100644
--- a/src/backend/rewrite/rewriteManip.c
+++ b/src/backend/rewrite/rewriteManip.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.73 2003/07/16 17:25:48 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.74 2003/07/25 00:01:09 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -678,14 +678,14 @@ getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
 		return parsetree;
 	Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
 	if (length(parsetree->jointree->fromlist) != 1)
-		elog(ERROR, "getInsertSelectQuery: expected to find SELECT subquery");
+		elog(ERROR, "expected to find SELECT subquery");
 	rtr = (RangeTblRef *) lfirst(parsetree->jointree->fromlist);
 	Assert(IsA(rtr, RangeTblRef));
 	selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
 	selectquery = selectrte->subquery;
 	if (!(selectquery && IsA(selectquery, Query) &&
 		  selectquery->commandType == CMD_SELECT))
-		elog(ERROR, "getInsertSelectQuery: expected to find SELECT subquery");
+		elog(ERROR, "expected to find SELECT subquery");
 	if (length(selectquery->rtable) >= 2 &&
 	strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
 		   "*OLD*") == 0 &&
@@ -696,7 +696,7 @@ getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
 			*subquery_ptr = &(selectrte->subquery);
 		return selectquery;
 	}
-	elog(ERROR, "getInsertSelectQuery: can't find rule placeholders");
+	elog(ERROR, "could not find rule placeholders");
 	return NULL;				/* not reached */
 }
 
@@ -730,7 +730,9 @@ AddQual(Query *parsetree, Node *qual)
 		if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
 			return;
 		else
-			elog(ERROR, "Conditional utility statements are not implemented");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("conditional utility statements are not implemented")));
 	}
 
 	if (parsetree->setOperations != NULL)
@@ -740,7 +742,9 @@ AddQual(Query *parsetree, Node *qual)
 		 * (This could be fixed, but right now the planner simply ignores
 		 * any qual condition on a setop query.)
 		 */
-		elog(ERROR, "Conditional UNION/INTERSECT/EXCEPT statements are not implemented");
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
 	}
 
 	/* INTERSECT want's the original, but we need to copy - Jan */
@@ -780,7 +784,9 @@ AddHavingQual(Query *parsetree, Node *havingQual)
 		if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
 			return;
 		else
-			elog(ERROR, "Conditional utility statements are not implemented");
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("conditional utility statements are not implemented")));
 	}
 
 	if (parsetree->setOperations != NULL)
@@ -790,7 +796,9 @@ AddHavingQual(Query *parsetree, Node *havingQual)
 		 * (This could be fixed, but right now the planner simply ignores
 		 * any qual condition on a setop query.)
 		 */
-		elog(ERROR, "Conditional UNION/INTERSECT/EXCEPT statements are not implemented");
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
 	}
 
 	/* INTERSECT want's the original, but we need to copy - Jan */
@@ -905,7 +913,9 @@ ResolveNew_mutator(Node *node, ResolveNew_context *context)
 
 			/* band-aid: don't do the wrong thing with a whole-tuple Var */
 			if (var->varattno == InvalidAttrNumber)
-				elog(ERROR, "ResolveNew: can't handle whole-tuple reference");
+				ereport(ERROR,
+						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+						 errmsg("cannot handle whole-tuple reference")));
 
 			n = FindMatchingNew(context->targetlist, var->varattno);
 
diff --git a/src/backend/rewrite/rewriteRemove.c b/src/backend/rewrite/rewriteRemove.c
index deb46128cad177d7f2f3c16224afd959c0f66ae7..25cc119af674bcd5dca4f9073e0c6e7e14f439b7 100644
--- a/src/backend/rewrite/rewriteRemove.c
+++ b/src/backend/rewrite/rewriteRemove.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteRemove.c,v 1.53 2002/09/04 20:31:25 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteRemove.c,v 1.54 2003/07/25 00:01:09 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -54,7 +54,10 @@ RemoveRewriteRule(Oid owningRel, const char *ruleName, DropBehavior behavior)
 	 * complain if no rule with such name exists
 	 */
 	if (!HeapTupleIsValid(tuple))
-		elog(ERROR, "Rule \"%s\" not found", ruleName);
+		ereport(ERROR,
+				(errcode(ERRCODE_UNDEFINED_OBJECT),
+				 errmsg("rule \"%s\" for relation \"%s\" does not exist",
+						ruleName, get_rel_name(owningRel))));
 
 	/*
 	 * Verify user has appropriate permissions.
@@ -110,8 +113,7 @@ RemoveRewriteRuleById(Oid ruleOid)
 	tuple = systable_getnext(rcscan);
 
 	if (!HeapTupleIsValid(tuple))
-		elog(ERROR, "RemoveRewriteRuleById: Rule %u does not exist",
-			 ruleOid);
+		elog(ERROR, "could not find tuple for rule %u", ruleOid);
 
 	/*
 	 * We had better grab AccessExclusiveLock so that we know no other
diff --git a/src/backend/rewrite/rewriteSupport.c b/src/backend/rewrite/rewriteSupport.c
index e55ceb83488f557de04745bc95360cfc5a8b33e0..1823fc8f0a2150250603d9b4d53942dc24eeee39 100644
--- a/src/backend/rewrite/rewriteSupport.c
+++ b/src/backend/rewrite/rewriteSupport.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.54 2002/08/05 03:29:17 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.55 2003/07/25 00:01:09 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -65,7 +65,7 @@ SetRelationRuleStatus(Oid relationId, bool relHasRules,
 							   ObjectIdGetDatum(relationId),
 							   0, 0, 0);
 	if (!HeapTupleIsValid(tuple))
-		elog(ERROR, "SetRelationRuleStatus: cache lookup failed for relation %u", relationId);
+		elog(ERROR, "cache lookup failed for relation %u", relationId);
 	classForm = (Form_pg_class) GETSTRUCT(tuple);
 
 	if (classForm->relhasrules != relHasRules ||