diff -cpr HEAD/src/backend/commands/explain.c auto_explain/src/backend/commands/explain.c
*** HEAD/src/backend/commands/explain.c	Tue Oct  7 05:29:38 2008
--- auto_explain/src/backend/commands/explain.c	Thu Oct  9 18:10:23 2008
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 224,230 ****
  	QueryDesc  *queryDesc;
  	instr_time	starttime;
  	double		totaltime = 0;
- 	ExplainState *es;
  	StringInfoData buf;
  	int			eflags;
  
--- 224,229 ----
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 265,281 ****
  		totaltime += elapsed_time(&starttime);
  	}
  
- 	es = (ExplainState *) palloc0(sizeof(ExplainState));
- 
- 	es->printTList = stmt->verbose;
- 	es->printAnalyze = stmt->analyze;
- 	es->pstmt = queryDesc->plannedstmt;
- 	es->rtable = queryDesc->plannedstmt->rtable;
- 
  	initStringInfo(&buf);
! 	explain_outNode(&buf,
! 					queryDesc->plannedstmt->planTree, queryDesc->planstate,
! 					NULL, 0, es);
  
  	/*
  	 * If we ran the command, run any AFTER triggers it queued.  (Note this
--- 264,271 ----
  		totaltime += elapsed_time(&starttime);
  	}
  
  	initStringInfo(&buf);
! 	ExplainOneResult(&buf, queryDesc, stmt->analyze, stmt->verbose);
  
  	/*
  	 * If we ran the command, run any AFTER triggers it queued.  (Note this
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 290,296 ****
  	}
  
  	/* Print info about runtime of triggers */
! 	if (es->printAnalyze)
  	{
  		ResultRelInfo *rInfo;
  		bool		show_relname;
--- 280,286 ----
  	}
  
  	/* Print info about runtime of triggers */
! 	if (stmt->analyze)
  	{
  		ResultRelInfo *rInfo;
  		bool		show_relname;
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 335,341 ****
  	do_text_output_multiline(tstate, buf.data);
  
  	pfree(buf.data);
! 	pfree(es);
  }
  
  /*
--- 325,350 ----
  	do_text_output_multiline(tstate, buf.data);
  
  	pfree(buf.data);
! }
! 
! /*
!  * ExplainOneResult -
!  *	  converts a Plan node into ascii string and appends it to 'str'
!  */
! void
! ExplainOneResult(StringInfo str, QueryDesc *queryDesc,
! 				 bool analyze, bool verbose)
! {
! 	ExplainState	es = { 0 };
! 
! 	es.printTList = verbose;
! 	es.printAnalyze = analyze;
! 	es.pstmt = queryDesc->plannedstmt;
! 	es.rtable = queryDesc->plannedstmt->rtable;
! 
! 	explain_outNode(str,
! 					queryDesc->plannedstmt->planTree, queryDesc->planstate,
! 					NULL, 0, &es);
  }
  
  /*
diff -cpr HEAD/src/backend/tcop/pquery.c auto_explain/src/backend/tcop/pquery.c
*** HEAD/src/backend/tcop/pquery.c	Fri Aug  1 22:16:09 2008
--- auto_explain/src/backend/tcop/pquery.c	Thu Oct  9 18:10:23 2008
***************
*** 32,38 ****
   * if there are several).
   */
  Portal		ActivePortal = NULL;
! 
  
  static void ProcessQuery(PlannedStmt *plan,
  			 ParamListInfo params,
--- 32,38 ----
   * if there are several).
   */
  Portal		ActivePortal = NULL;
! bool		force_instrument = false;
  
  static void ProcessQuery(PlannedStmt *plan,
  			 ParamListInfo params,
*************** CreateQueryDesc(PlannedStmt *plannedstmt
*** 76,82 ****
  	qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
  	qd->dest = dest;			/* output dest */
  	qd->params = params;		/* parameter values passed into query */
! 	qd->doInstrument = doInstrument;	/* instrumentation wanted? */
  
  	/* null these fields until set by ExecutorStart */
  	qd->tupDesc = NULL;
--- 76,82 ----
  	qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
  	qd->dest = dest;			/* output dest */
  	qd->params = params;		/* parameter values passed into query */
! 	qd->doInstrument = force_instrument || doInstrument;	/* instrumentation wanted? */
  
  	/* null these fields until set by ExecutorStart */
  	qd->tupDesc = NULL;
diff -cpr HEAD/src/backend/utils/misc/guc.c auto_explain/src/backend/utils/misc/guc.c
*** HEAD/src/backend/utils/misc/guc.c	Mon Oct  6 22:05:36 2008
--- auto_explain/src/backend/utils/misc/guc.c	Thu Oct  9 18:10:23 2008
*************** static const char *assign_pgstat_temp_di
*** 169,174 ****
--- 169,175 ----
  
  static char *config_enum_get_options(struct config_enum *record, 
  									 const char *prefix, const char *suffix);
+ static void initialize_option(struct config_generic *gconf);
  
  
  /*
*************** InitializeGUCOptions(void)
*** 3177,3288 ****
  	for (i = 0; i < num_guc_variables; i++)
  	{
  		struct config_generic *gconf = guc_variables[i];
! 
! 		gconf->status = 0;
! 		gconf->reset_source = PGC_S_DEFAULT;
! 		gconf->source = PGC_S_DEFAULT;
! 		gconf->stack = NULL;
! 		gconf->sourcefile = NULL;
! 		gconf->sourceline = 0;
! 
! 		switch (gconf->vartype)
! 		{
! 			case PGC_BOOL:
! 				{
! 					struct config_bool *conf = (struct config_bool *) gconf;
! 
! 					if (conf->assign_hook)
! 						if (!(*conf->assign_hook) (conf->boot_val, true,
! 												   PGC_S_DEFAULT))
! 							elog(FATAL, "failed to initialize %s to %d",
! 								 conf->gen.name, (int) conf->boot_val);
! 					*conf->variable = conf->reset_val = conf->boot_val;
! 					break;
! 				}
! 			case PGC_INT:
! 				{
! 					struct config_int *conf = (struct config_int *) gconf;
! 
! 					Assert(conf->boot_val >= conf->min);
! 					Assert(conf->boot_val <= conf->max);
! 					if (conf->assign_hook)
! 						if (!(*conf->assign_hook) (conf->boot_val, true,
! 												   PGC_S_DEFAULT))
! 							elog(FATAL, "failed to initialize %s to %d",
! 								 conf->gen.name, conf->boot_val);
! 					*conf->variable = conf->reset_val = conf->boot_val;
! 					break;
! 				}
! 			case PGC_REAL:
! 				{
! 					struct config_real *conf = (struct config_real *) gconf;
! 
! 					Assert(conf->boot_val >= conf->min);
! 					Assert(conf->boot_val <= conf->max);
! 					if (conf->assign_hook)
! 						if (!(*conf->assign_hook) (conf->boot_val, true,
! 												   PGC_S_DEFAULT))
! 							elog(FATAL, "failed to initialize %s to %g",
! 								 conf->gen.name, conf->boot_val);
! 					*conf->variable = conf->reset_val = conf->boot_val;
! 					break;
! 				}
! 			case PGC_STRING:
! 				{
! 					struct config_string *conf = (struct config_string *) gconf;
! 					char	   *str;
! 
! 					*conf->variable = NULL;
! 					conf->reset_val = NULL;
! 
! 					if (conf->boot_val == NULL)
! 					{
! 						/* leave the value NULL, do not call assign hook */
! 						break;
! 					}
! 
! 					str = guc_strdup(FATAL, conf->boot_val);
! 					conf->reset_val = str;
! 
! 					if (conf->assign_hook)
! 					{
! 						const char *newstr;
! 
! 						newstr = (*conf->assign_hook) (str, true,
! 													   PGC_S_DEFAULT);
! 						if (newstr == NULL)
! 						{
! 							elog(FATAL, "failed to initialize %s to \"%s\"",
! 								 conf->gen.name, str);
! 						}
! 						else if (newstr != str)
! 						{
! 							free(str);
! 
! 							/*
! 							 * See notes in set_config_option about casting
! 							 */
! 							str = (char *) newstr;
! 							conf->reset_val = str;
! 						}
! 					}
! 					*conf->variable = str;
! 					break;
! 				}
! 			case PGC_ENUM:
! 				{
! 					struct config_enum *conf = (struct config_enum *) gconf;
! 
! 					if (conf->assign_hook)
! 						if (!(*conf->assign_hook) (conf->boot_val, true,
! 												   PGC_S_DEFAULT))
! 							elog(FATAL, "failed to initialize %s to %s",
! 								 conf->gen.name, 
! 								 config_enum_lookup_by_value(conf, conf->boot_val));
! 					*conf->variable = conf->reset_val = conf->boot_val;
! 					break;
! 				}
! 		}
  	}
  
  	guc_dirty = false;
--- 3178,3184 ----
  	for (i = 0; i < num_guc_variables; i++)
  	{
  		struct config_generic *gconf = guc_variables[i];
! 		initialize_option(gconf);
  	}
  
  	guc_dirty = false;
*************** InitializeGUCOptions(void)
*** 3338,3343 ****
--- 3234,3348 ----
  	}
  }
  
+ static void
+ initialize_option(struct config_generic *gconf)
+ {
+ 	gconf->status = 0;
+ 	gconf->reset_source = PGC_S_DEFAULT;
+ 	gconf->source = PGC_S_DEFAULT;
+ 	gconf->stack = NULL;
+ 	gconf->sourcefile = NULL;
+ 	gconf->sourceline = 0;
+ 
+ 	switch (gconf->vartype)
+ 	{
+ 		case PGC_BOOL:
+ 			{
+ 				struct config_bool *conf = (struct config_bool *) gconf;
+ 
+ 				if (conf->assign_hook)
+ 					if (!(*conf->assign_hook) (conf->boot_val, true,
+ 											   PGC_S_DEFAULT))
+ 						elog(FATAL, "failed to initialize %s to %d",
+ 							 conf->gen.name, (int) conf->boot_val);
+ 				*conf->variable = conf->reset_val = conf->boot_val;
+ 				break;
+ 			}
+ 		case PGC_INT:
+ 			{
+ 				struct config_int *conf = (struct config_int *) gconf;
+ 
+ 				Assert(conf->boot_val >= conf->min);
+ 				Assert(conf->boot_val <= conf->max);
+ 				if (conf->assign_hook)
+ 					if (!(*conf->assign_hook) (conf->boot_val, true,
+ 											   PGC_S_DEFAULT))
+ 						elog(FATAL, "failed to initialize %s to %d",
+ 							 conf->gen.name, conf->boot_val);
+ 				*conf->variable = conf->reset_val = conf->boot_val;
+ 				break;
+ 			}
+ 		case PGC_REAL:
+ 			{
+ 				struct config_real *conf = (struct config_real *) gconf;
+ 
+ 				Assert(conf->boot_val >= conf->min);
+ 				Assert(conf->boot_val <= conf->max);
+ 				if (conf->assign_hook)
+ 					if (!(*conf->assign_hook) (conf->boot_val, true,
+ 											   PGC_S_DEFAULT))
+ 						elog(FATAL, "failed to initialize %s to %g",
+ 							 conf->gen.name, conf->boot_val);
+ 				*conf->variable = conf->reset_val = conf->boot_val;
+ 				break;
+ 			}
+ 		case PGC_STRING:
+ 			{
+ 				struct config_string *conf = (struct config_string *) gconf;
+ 				char	   *str;
+ 
+ 				*conf->variable = NULL;
+ 				conf->reset_val = NULL;
+ 
+ 				if (conf->boot_val == NULL)
+ 				{
+ 					/* leave the value NULL, do not call assign hook */
+ 					break;
+ 				}
+ 
+ 				str = guc_strdup(FATAL, conf->boot_val);
+ 				conf->reset_val = str;
+ 
+ 				if (conf->assign_hook)
+ 				{
+ 					const char *newstr;
+ 
+ 					newstr = (*conf->assign_hook) (str, true,
+ 												   PGC_S_DEFAULT);
+ 					if (newstr == NULL)
+ 					{
+ 						elog(FATAL, "failed to initialize %s to \"%s\"",
+ 							 conf->gen.name, str);
+ 					}
+ 					else if (newstr != str)
+ 					{
+ 						free(str);
+ 
+ 						/*
+ 						 * See notes in set_config_option about casting
+ 						 */
+ 						str = (char *) newstr;
+ 						conf->reset_val = str;
+ 					}
+ 				}
+ 				*conf->variable = str;
+ 				break;
+ 			}
+ 		case PGC_ENUM:
+ 			{
+ 				struct config_enum *conf = (struct config_enum *) gconf;
+ 
+ 				if (conf->assign_hook)
+ 					if (!(*conf->assign_hook) (conf->boot_val, true,
+ 											   PGC_S_DEFAULT))
+ 						elog(FATAL, "failed to initialize %s to %s",
+ 							 conf->gen.name, 
+ 							 config_enum_lookup_by_value(conf, conf->boot_val));
+ 				*conf->variable = conf->reset_val = conf->boot_val;
+ 				break;
+ 			}
+ 	}
+ }
  
  /*
   * Select the configuration files and data directory to be used, and
*************** define_custom_variable(struct config_gen
*** 5639,5644 ****
--- 5644,5650 ----
  	if (res == NULL)
  	{
  		/* No placeholder to replace, so just add it */
+ 		initialize_option(variable);
  		add_guc_variable(variable, ERROR);
  		return;
  	}
*************** define_custom_variable(struct config_gen
*** 5673,5678 ****
--- 5679,5686 ----
  		set_config_option(name, value,
  						  pHolder->gen.context, pHolder->gen.source,
  						  GUC_ACTION_SET, true);
+ 	else
+ 		initialize_option(variable);
  
  	/*
  	 * Free up as much as we conveniently can of the placeholder structure
*************** DefineCustomEnumVariable(const char *nam
*** 5804,5809 ****
--- 5812,5840 ----
  	var->assign_hook = assign_hook;
  	var->show_hook = show_hook;
  	define_custom_variable(&var->gen);
+ }
+ 
+ static const int config_varsize[] =
+ {
+ 	sizeof(struct config_bool),
+ 	sizeof(struct config_int),
+ 	sizeof(struct config_real),
+ 	sizeof(struct config_string),
+ 	sizeof(struct config_enum),
+ };
+ 
+ void
+ DefineCustomVariable(enum config_type type, const void *variable)
+ {
+ 	int		size = config_varsize[type];
+ 	const struct config_generic	   *var = variable;
+ 	struct config_generic		   *gen;
+ 
+ 	gen = (struct config_generic *) guc_malloc(ERROR, size);
+ 	memcpy(gen, var, size);
+ 	gen->name = guc_strdup(ERROR, var->name);
+ 	gen->vartype = type;
+ 	define_custom_variable(gen);
  }
  
  void
diff -cpr HEAD/src/include/commands/explain.h auto_explain/src/include/commands/explain.h
*** HEAD/src/include/commands/explain.h	Wed Jan  2 04:45:57 2008
--- auto_explain/src/include/commands/explain.h	Thu Oct  9 18:10:23 2008
*************** extern void ExplainOneUtility(Node *util
*** 41,44 ****
--- 41,47 ----
  extern void ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params,
  			   ExplainStmt *stmt, TupOutputState *tstate);
  
+ extern void ExplainOneResult(StringInfo str, QueryDesc *queryDesc,
+ 							 bool analyze, bool verbose);
+ 
  #endif   /* EXPLAIN_H */
diff -cpr HEAD/src/include/executor/execdesc.h auto_explain/src/include/executor/execdesc.h
*** HEAD/src/include/executor/execdesc.h	Wed Jan  2 04:45:57 2008
--- auto_explain/src/include/executor/execdesc.h	Thu Oct  9 18:10:23 2008
***************
*** 20,25 ****
--- 20,27 ----
  #include "tcop/dest.h"
  
  
+ extern PGDLLIMPORT bool force_instrument;
+ 
  /* ----------------
   *		query descriptor:
   *
diff -cpr HEAD/src/include/utils/guc_tables.h auto_explain/src/include/utils/guc_tables.h
*** HEAD/src/include/utils/guc_tables.h	Tue Sep 30 19:52:14 2008
--- auto_explain/src/include/utils/guc_tables.h	Thu Oct  9 18:10:23 2008
*************** extern const char *config_enum_lookup_by
*** 242,246 ****
--- 242,247 ----
  extern bool config_enum_lookup_by_name(struct config_enum *record,
  									  const char *value, int *retval);
  
+ extern void DefineCustomVariable(enum config_type type, const void *variable);
  
  #endif   /* GUC_TABLES_H */
