diff -c -r orig_pgsql/doc/src/sgml/config.sgml pgsql/doc/src/sgml/config.sgml
*** orig_pgsql/doc/src/sgml/config.sgml	Tue Jul 29 23:11:32 2008
--- pgsql/doc/src/sgml/config.sgml	Thu Jul 31 18:27:51 2008
***************
*** 3942,3947 ****
--- 3942,3970 ----
        </listitem>
       </varlistentry>
  
+      <varlistentry id="guc-statement-cost-limit" xreflabel="statement_cost_limit">
+       <term><varname>statement_cost_limit</varname> (<type>integer</type>)</term>
+       <indexterm>
+        <primary><varname>statement_cost_limit</> configuration parameter</primary>
+       </indexterm>
+       <listitem>
+        <para>
+         Abort any statement that has higher costs than the specified number of
+         penalty points predicted by the planner.
+         If <varname>log_min_error_statement</> is set to
+         <literal>ERROR</> or lower, the statement that is too expensive will also be
+         logged.  A value of zero (the default) turns off the 
+         limitation.
+        </para>
+ 
+        <para>
+         Setting <varname>statement_cost_limit</> in
+         <filename>postgresql.conf</> is not recommended because it
+         affects all sessions.
+        </para>
+       </listitem>
+      </varlistentry>
+ 
       <varlistentry id="guc-vacuum-freeze-min-age" xreflabel="vacuum_freeze_min_age">
        <term><varname>vacuum_freeze_min_age</varname> (<type>integer</type>)</term>
        <indexterm>
diff -c -r orig_pgsql/src/backend/tcop/postgres.c pgsql/src/backend/tcop/postgres.c
*** orig_pgsql/src/backend/tcop/postgres.c	Tue Jul 29 23:11:37 2008
--- pgsql/src/backend/tcop/postgres.c	Thu Jul 31 18:22:57 2008
***************
*** 64,69 ****
--- 64,70 ----
  #include "tcop/tcopprot.h"
  #include "tcop/utility.h"
  #include "utils/flatfiles.h"
+ #include "utils/guc.h"
  #include "utils/lsyscache.h"
  #include "utils/memutils.h"
  #include "utils/ps_status.h"
***************
*** 95,100 ****
--- 96,103 ----
  /* wait N seconds to allow attach from a debugger */
  int			PostAuthDelay = 0;
  
+ /* check for plan costs */
+ int			StatementCostLimit = 0;
  
  
  /* ----------------
***************
*** 682,688 ****
  
  	/* call the optimizer */
  	plan = planner(querytree, cursorOptions, boundParams);
! 
  	if (log_planner_stats)
  		ShowUsage("PLANNER STATISTICS");
  
--- 685,693 ----
  
  	/* call the optimizer */
  	plan = planner(querytree, cursorOptions, boundParams);
! 	if (StatementCostLimit > 0 && plan->planTree->total_cost > StatementCostLimit)
! 		elog(ERROR, "execution plan is too expensive: %f", plan->planTree->total_cost );
! 	
  	if (log_planner_stats)
  		ShowUsage("PLANNER STATISTICS");
  
diff -c -r orig_pgsql/src/backend/utils/misc/guc.c pgsql/src/backend/utils/misc/guc.c
*** orig_pgsql/src/backend/utils/misc/guc.c	Tue Jul 29 23:11:39 2008
--- pgsql/src/backend/utils/misc/guc.c	Thu Jul 31 18:24:20 2008
***************
*** 1515,1520 ****
--- 1515,1530 ----
  	},
  
  	{
+ 		{"statement_cost_limit", PGC_USERSET, CLIENT_CONN_STATEMENT,
+ 			gettext_noop("Sets the maximum costs of a statement."),
+ 			gettext_noop("A value of 0 turns off the threshold."),
+ 			NULL
+ 		},
+ 		&StatementCostLimit,
+ 		0, 100, INT_MAX, NULL, NULL
+ 	},
+ 
+ 	{
  		{"vacuum_freeze_min_age", PGC_USERSET, CLIENT_CONN_STATEMENT,
  			gettext_noop("Minimum age at which VACUUM should freeze a table row."),
  			NULL
diff -c -r orig_pgsql/src/backend/utils/misc/postgresql.conf.sample pgsql/src/backend/utils/misc/postgresql.conf.sample
*** orig_pgsql/src/backend/utils/misc/postgresql.conf.sample	Tue Jul 29 23:11:39 2008
--- pgsql/src/backend/utils/misc/postgresql.conf.sample	Thu Jul 31 18:07:42 2008
***************
*** 418,423 ****
--- 418,424 ----
  #session_replication_role = 'origin'
  #statement_timeout = 0			# 0 is disabled
+ #statement_cost_limit = 0		# 0 is disabled
  #vacuum_freeze_min_age = 100000000
  #xmlbinary = 'base64'
  #xmloption = 'content'
diff -c -r orig_pgsql/src/include/utils/guc.h pgsql/src/include/utils/guc.h
*** orig_pgsql/src/include/utils/guc.h	Tue Jul 29 23:11:36 2008
--- pgsql/src/include/utils/guc.h	Thu Jul 31 18:13:00 2008
***************
*** 145,150 ****
--- 145,152 ----
  extern int	log_min_duration_statement;
  extern int	log_temp_files;
  
+ extern int	StatementCostLimit;
+ 
  extern int	num_temp_buffers;
  
  extern char *ConfigFileName;
