diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
new file mode 100644
index 05c5c19..bf247ea
*** a/src/backend/tcop/postgres.c
--- b/src/backend/tcop/postgres.c
*************** exec_simple_query(const char *query_stri
*** 947,953 ****
  		ereport(LOG,
  				(errmsg("statement: %s", query_string),
  				 errhidestmt(true),
! 				 errdetail_execute(parsetree_list)));
  		was_logged = true;
  	}
  
--- 947,954 ----
  		ereport(LOG,
  				(errmsg("statement: %s", query_string),
  				 errhidestmt(true),
! 				 errdetail_execute(parsetree_list),
! 				 errstream(LOG_STREAM_STATEMENT)));
  		was_logged = true;
  	}
  
*************** exec_execute_message(const char *portal_
*** 1982,1988 ****
  						*portal_name ? portal_name : "",
  						sourceText),
  				 errhidestmt(true),
! 				 errdetail_params(portalParams)));
  		was_logged = true;
  	}
  
--- 1983,1990 ----
  						*portal_name ? portal_name : "",
  						sourceText),
  				 errhidestmt(true),
! 				 errdetail_params(portalParams),
! 				 errstream(LOG_STREAM_STATEMENT)));
  		was_logged = true;
  	}
  
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
new file mode 100644
index 8f7fea8..569cdb7
*** a/src/backend/utils/misc/guc.c
--- b/src/backend/utils/misc/guc.c
*************** static struct config_bool ConfigureNames
*** 1461,1466 ****
--- 1461,1475 ----
  		false,
  		NULL, NULL, NULL
  	},
+ 	{
+ 		{"statement_log_truncate_on_rotation", PGC_SIGHUP, LOGGING_WHERE,
+ 			gettext_noop("Truncate existing statement log file of same name during log rotation."),
+ 			NULL
+ 		},
+ 		&log_streams[LOG_STREAM_STATEMENT].truncate_on_rotation,
+ 		false,
+ 		NULL, NULL, NULL
+ 	},
  
  #ifdef TRACE_SORT
  	{
*************** static struct config_int ConfigureNamesI
*** 1927,1932 ****
--- 1936,1955 ----
  	},
  
  	{
+ 		{"statement_log_file_mode", PGC_SIGHUP, LOGGING_WHERE,
+ 			gettext_noop("Sets the file permissions for statement log file."),
+ 			gettext_noop("The parameter value is expected "
+ 						 "to be a numeric mode specification in the form "
+ 						 "accepted by the chmod and umask system calls. "
+ 						 "(To use the customary octal format the number must "
+ 						 "start with a 0 (zero).)")
+ 		},
+ 		&log_streams[LOG_STREAM_STATEMENT].file_mode,
+ 		0600, 0000, 0777,
+ 		NULL, NULL, guc_show_log_file_mode
+ 	},
+ 
+ 	{
  		{"work_mem", PGC_USERSET, RESOURCES_MEM,
  			gettext_noop("Sets the maximum memory to be used for query workspaces."),
  			gettext_noop("This much memory can be used by each internal "
*************** static struct config_int ConfigureNamesI
*** 2563,2568 ****
--- 2586,2612 ----
  	},
  
  	{
+ 		{"statement_log_rotation_age", PGC_SIGHUP, LOGGING_WHERE,
+ 			gettext_noop("Automatic statement log file rotation will occur after N minutes."),
+ 			NULL,
+ 			GUC_UNIT_MIN
+ 		},
+ 		&log_streams[LOG_STREAM_STATEMENT].rotation_age,
+ 		HOURS_PER_DAY * MINS_PER_HOUR, 0, INT_MAX / SECS_PER_MINUTE,
+ 		NULL, NULL, NULL
+ 	},
+ 	{
+ 		{"statement_log_rotation_size", PGC_SIGHUP, LOGGING_WHERE,
+ 			gettext_noop("Automatic statement log file rotation will occur after N kilobytes."),
+ 			NULL,
+ 			GUC_UNIT_KB
+ 		},
+ 		&log_streams[LOG_STREAM_STATEMENT].rotation_size,
+ 		10 * 1024, 0, INT_MAX / 1024,
+ 		NULL, NULL, NULL
+ 	},
+ 
+ 	{
  		{"max_function_args", PGC_INTERNAL, PRESET_OPTIONS,
  			gettext_noop("Shows the maximum number of function arguments."),
  			NULL,
*************** static struct config_string ConfigureNam
*** 3093,3098 ****
--- 3137,3152 ----
  	},
  
  	{
+ 		{"statement_log_line_prefix", PGC_SIGHUP, LOGGING_WHAT,
+ 			gettext_noop("Controls information prefixed to each statement log line."),
+ 			gettext_noop("If blank, no prefix is used.")
+ 		},
+ 		&log_streams[LOG_STREAM_STATEMENT].line_prefix,
+ 		"%m [%p] ",
+ 		NULL, NULL, NULL
+ 	},
+ 
+ 	{
  		{"log_timezone", PGC_SIGHUP, LOGGING_WHAT,
  			gettext_noop("Sets the time zone to use in log messages."),
  			NULL
*************** static struct config_string ConfigureNam
*** 3360,3365 ****
--- 3414,3440 ----
  		"postgresql-%Y-%m-%d_%H%M%S.log",
  		NULL, NULL, NULL
  	},
+ 	{
+ 		{"statement_log_directory", PGC_SIGHUP, LOGGING_WHERE,
+ 			gettext_noop("Sets the destination directory for statement log file."),
+ 			gettext_noop("Can be specified as relative to the data directory "
+ 						 "or as absolute path."),
+ 			GUC_SUPERUSER_ONLY
+ 		},
+ 		&log_streams[LOG_STREAM_STATEMENT].directory,
+ 		"log",
+ 		guc_check_canonical_path, NULL, NULL
+ 	},
+ 	{
+ 		{"statement_log_filename", PGC_SIGHUP, LOGGING_WHERE,
+ 			gettext_noop("Sets the file name pattern for statement log."),
+ 			NULL,
+ 			GUC_SUPERUSER_ONLY
+ 		},
+ 		&log_streams[LOG_STREAM_STATEMENT].filename,
+ 		"statement-%Y-%m-%d_%H%M%S.log",
+ 		NULL, NULL, NULL
+ 	},
  
  	{
  		{"syslog_ident", PGC_SIGHUP, LOGGING_WHERE,
diff --git a/src/include/utils/elog_stream.h b/src/include/utils/elog_stream.h
new file mode 100644
index c975be6..5ada12e
*** a/src/include/utils/elog_stream.h
--- b/src/include/utils/elog_stream.h
***************
*** 25,30 ****
--- 25,31 ----
  typedef enum LogStreamId
  {
  	LOG_STREAM_CORE = 0,
+ 	LOG_STREAM_STATEMENT,
  	LOG_STREAM_FIRST_EXTENSION,
  } LogStreamId;
  
