@@ -112,6 +112,7 @@ int Log_destination = LOG_DESTINATION_STDERR;
112
112
char * Log_destination_string = NULL ;
113
113
bool syslog_sequence_numbers = true;
114
114
bool syslog_split_messages = true;
115
+ int max_log_size = 0 ;
115
116
116
117
/* Processed form of backtrace_functions GUC */
117
118
static char * backtrace_function_list ;
@@ -1693,11 +1694,18 @@ EmitErrorReport(void)
1693
1694
{
1694
1695
ErrorData * edata = & errordata [errordata_stack_depth ];
1695
1696
MemoryContext oldcontext ;
1697
+ char * truncated_query = NULL ;
1696
1698
1697
1699
recursion_depth ++ ;
1698
1700
CHECK_STACK_DEPTH ();
1699
1701
oldcontext = MemoryContextSwitchTo (edata -> assoc_context );
1700
1702
1703
+ if (need_truncate_query_log (debug_query_string ))
1704
+ {
1705
+ truncated_query = truncate_query_log (debug_query_string );
1706
+ debug_query_string = truncated_query ;
1707
+ }
1708
+
1701
1709
/*
1702
1710
* Reset the formatted timestamp fields before emitting any logs. This
1703
1711
* includes all the log destinations and emit_log_hook, as the latter
@@ -1738,6 +1746,9 @@ EmitErrorReport(void)
1738
1746
1739
1747
MemoryContextSwitchTo (oldcontext );
1740
1748
recursion_depth -- ;
1749
+
1750
+ if (truncated_query )
1751
+ pfree (truncated_query );
1741
1752
}
1742
1753
1743
1754
/*
@@ -3814,3 +3825,39 @@ write_stderr(const char *fmt,...)
3814
3825
#endif
3815
3826
va_end (ap );
3816
3827
}
3828
+
3829
+ /*
3830
+ * Apply truncation to build query that will be logged.
3831
+ *
3832
+ * If query needs to be truncated, copied will be set to true
3833
+ * and returned string must be freed
3834
+ */
3835
+ char *
3836
+ truncate_query_log (const char * query )
3837
+ {
3838
+ size_t truncated_query_len ;
3839
+ char * truncatd_query ;
3840
+ size_t query_len ;
3841
+
3842
+ if (!query )
3843
+ return NULL ;
3844
+
3845
+ query_len = strlen (query );
3846
+ truncated_query_len = pg_mbcliplen (query , query_len , max_log_size );
3847
+ truncatd_query = (char * ) palloc (truncated_query_len + 1 );
3848
+ memcpy (truncatd_query , query , truncated_query_len );
3849
+ truncatd_query [truncated_query_len ] = '\0' ;
3850
+ return truncatd_query ;
3851
+ }
3852
+
3853
+ /*
3854
+ * Checks if query should be truncated
3855
+ * according to max_log_size
3856
+ */
3857
+ bool
3858
+ need_truncate_query_log (const char * query )
3859
+ {
3860
+ if (!query )
3861
+ return false;
3862
+ return !(max_log_size == 0 || strlen (query ) < max_log_size );
3863
+ }
0 commit comments