@@ -112,6 +112,7 @@ int Log_destination = LOG_DESTINATION_STDERR;
112112char * Log_destination_string = NULL ;
113113bool syslog_sequence_numbers = true;
114114bool syslog_split_messages = true;
115+ int max_log_size = 0 ;
115116
116117/* Processed form of backtrace_functions GUC */
117118static char * backtrace_function_list ;
@@ -1696,11 +1697,18 @@ EmitErrorReport(void)
16961697{
16971698 ErrorData * edata = & errordata [errordata_stack_depth ];
16981699 MemoryContext oldcontext ;
1700+ char * truncated_query = NULL ;
16991701
17001702 recursion_depth ++ ;
17011703 CHECK_STACK_DEPTH ();
17021704 oldcontext = MemoryContextSwitchTo (edata -> assoc_context );
17031705
1706+ if (need_truncate_query_log (debug_query_string ))
1707+ {
1708+ truncated_query = truncate_query_log (debug_query_string );
1709+ debug_query_string = truncated_query ;
1710+ }
1711+
17041712 /*
17051713 * Reset the formatted timestamp fields before emitting any logs. This
17061714 * includes all the log destinations and emit_log_hook, as the latter
@@ -1741,6 +1749,9 @@ EmitErrorReport(void)
17411749
17421750 MemoryContextSwitchTo (oldcontext );
17431751 recursion_depth -- ;
1752+
1753+ if (truncated_query )
1754+ pfree (truncated_query );
17441755}
17451756
17461757/*
@@ -3817,3 +3828,39 @@ write_stderr(const char *fmt,...)
38173828#endif
38183829 va_end (ap );
38193830}
3831+
3832+ /*
3833+ * Apply truncation to build query that will be logged.
3834+ *
3835+ * If query needs to be truncated, copied will be set to true
3836+ * and returned string must be freed
3837+ */
3838+ char *
3839+ truncate_query_log (const char * query )
3840+ {
3841+ size_t truncated_query_len ;
3842+ char * truncatd_query ;
3843+ size_t query_len ;
3844+
3845+ if (!query )
3846+ return NULL ;
3847+
3848+ query_len = strlen (query );
3849+ truncated_query_len = pg_mbcliplen (query , query_len , max_log_size );
3850+ truncatd_query = (char * ) palloc (truncated_query_len + 1 );
3851+ memcpy (truncatd_query , query , truncated_query_len );
3852+ truncatd_query [truncated_query_len ] = '\0' ;
3853+ return truncatd_query ;
3854+ }
3855+
3856+ /*
3857+ * Checks if query should be truncated
3858+ * according to max_log_size
3859+ */
3860+ bool
3861+ need_truncate_query_log (const char * query )
3862+ {
3863+ if (!query )
3864+ return false;
3865+ return !(max_log_size == 0 || strlen (query ) < max_log_size );
3866+ }
0 commit comments