1 /* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef SYSTEM_VARIABLES_INCLUDED
24 #define SYSTEM_VARIABLES_INCLUDED
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 
29 #include "m_ctype.h"
30 #include "my_base.h"  // ha_rows
31 #include "my_inttypes.h"
32 #include "my_sqlcommand.h"
33 #include "my_thread_local.h"     // my_thread_id
34 #include "sql/rpl_gtid.h"        // Gitd_specification
35 #include "sql/sql_plugin_ref.h"  // plugin_ref
36 
37 class MY_LOCALE;
38 class Time_zone;
39 
40 typedef ulonglong sql_mode_t;
41 struct LIST;
42 
43 // Values for binlog_format sysvar
44 enum enum_binlog_format {
45   BINLOG_FORMAT_MIXED = 0,  ///< statement if safe, otherwise row - autodetected
46   BINLOG_FORMAT_STMT = 1,   ///< statement-based
47   BINLOG_FORMAT_ROW = 2,    ///< row-based
48   BINLOG_FORMAT_UNSPEC =
49       3  ///< thd_binlog_format() returns it when binlog is closed
50 };
51 
52 // Values for rbr_exec_mode_options sysvar
53 enum enum_rbr_exec_mode {
54   RBR_EXEC_MODE_STRICT,
55   RBR_EXEC_MODE_IDEMPOTENT,
56   RBR_EXEC_MODE_LAST_BIT
57 };
58 
59 // Values for binlog_row_image sysvar
60 enum enum_binlog_row_image {
61   /** PKE in the before image and changed columns in the after image */
62   BINLOG_ROW_IMAGE_MINIMAL = 0,
63   /** Whenever possible, before and after image contain all columns except
64      blobs. */
65   BINLOG_ROW_IMAGE_NOBLOB = 1,
66   /** All columns in both before and after image. */
67   BINLOG_ROW_IMAGE_FULL = 2
68 };
69 
70 // Bits for binlog_row_value_options sysvar
71 enum enum_binlog_row_value_options {
72   /// Store JSON updates in partial form
73   PARTIAL_JSON_UPDATES = 1
74 };
75 
76 // Values for binlog_row_metadata sysvar
77 enum enum_binlog_row_metadata {
78   BINLOG_ROW_METADATA_MINIMAL = 0,
79   BINLOG_ROW_METADATA_FULL = 1
80 };
81 
82 // Values for transaction_write_set_extraction sysvar
83 enum enum_transaction_write_set_hashing_algorithm {
84   HASH_ALGORITHM_OFF = 0,
85   HASH_ALGORITHM_MURMUR32 = 1,
86   HASH_ALGORITHM_XXHASH64 = 2
87 };
88 
89 // Values for session_track_gtids sysvar
90 enum enum_session_track_gtids { OFF = 0, OWN_GTID = 1, ALL_GTIDS = 2 };
91 
92 /** Values for use_secondary_engine sysvar. */
93 enum use_secondary_engine {
94   SECONDARY_ENGINE_OFF = 0,
95   SECONDARY_ENGINE_ON = 1,
96   SECONDARY_ENGINE_FORCED = 2
97 };
98 
99 /* Bits for different SQL modes modes (including ANSI mode) */
100 #define MODE_REAL_AS_FLOAT 1
101 #define MODE_PIPES_AS_CONCAT 2
102 #define MODE_ANSI_QUOTES 4
103 #define MODE_IGNORE_SPACE 8
104 #define MODE_NOT_USED 16
105 #define MODE_ONLY_FULL_GROUP_BY 32
106 #define MODE_NO_UNSIGNED_SUBTRACTION 64
107 #define MODE_NO_DIR_IN_CREATE 128
108 #define MODE_ANSI 262144L
109 #define MODE_NO_AUTO_VALUE_ON_ZERO (MODE_ANSI * 2)
110 #define MODE_NO_BACKSLASH_ESCAPES (MODE_NO_AUTO_VALUE_ON_ZERO * 2)
111 #define MODE_STRICT_TRANS_TABLES (MODE_NO_BACKSLASH_ESCAPES * 2)
112 #define MODE_STRICT_ALL_TABLES (MODE_STRICT_TRANS_TABLES * 2)
113 /*
114  * NO_ZERO_DATE, NO_ZERO_IN_DATE and ERROR_FOR_DIVISION_BY_ZERO modes are
115  * removed in 5.7 and their functionality is merged with STRICT MODE.
116  * However, For backward compatibility during upgrade, these modes are kept
117  * but they are not used. Setting these modes in 5.7 will give warning and
118  * have no effect.
119  */
120 #define MODE_NO_ZERO_IN_DATE (MODE_STRICT_ALL_TABLES * 2)
121 #define MODE_NO_ZERO_DATE (MODE_NO_ZERO_IN_DATE * 2)
122 #define MODE_INVALID_DATES (MODE_NO_ZERO_DATE * 2)
123 #define MODE_ERROR_FOR_DIVISION_BY_ZERO (MODE_INVALID_DATES * 2)
124 #define MODE_TRADITIONAL (MODE_ERROR_FOR_DIVISION_BY_ZERO * 2)
125 #define MODE_HIGH_NOT_PRECEDENCE (1ULL << 29)
126 #define MODE_NO_ENGINE_SUBSTITUTION (MODE_HIGH_NOT_PRECEDENCE * 2)
127 #define MODE_PAD_CHAR_TO_FULL_LENGTH (1ULL << 31)
128 /*
129   If this mode is set the fractional seconds which cannot fit in given fsp will
130   be truncated.
131 */
132 #define MODE_TIME_TRUNCATE_FRACTIONAL (1ULL << 32)
133 
134 #define MODE_LAST (1ULL << 33)
135 
136 #define MODE_ALLOWED_MASK                                                      \
137   (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |              \
138    MODE_IGNORE_SPACE | MODE_NOT_USED | MODE_ONLY_FULL_GROUP_BY |               \
139    MODE_NO_UNSIGNED_SUBTRACTION | MODE_NO_DIR_IN_CREATE | MODE_ANSI |          \
140    MODE_NO_AUTO_VALUE_ON_ZERO | MODE_NO_BACKSLASH_ESCAPES |                    \
141    MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES | MODE_NO_ZERO_IN_DATE |  \
142    MODE_NO_ZERO_DATE | MODE_INVALID_DATES | MODE_ERROR_FOR_DIVISION_BY_ZERO |  \
143    MODE_TRADITIONAL | MODE_HIGH_NOT_PRECEDENCE | MODE_NO_ENGINE_SUBSTITUTION | \
144    MODE_PAD_CHAR_TO_FULL_LENGTH | MODE_TIME_TRUNCATE_FRACTIONAL)
145 
146 /*
147   We can safely ignore and reset these obsolete mode bits while replicating:
148 */
149 #define MODE_IGNORED_MASK                         \
150   (0x00100 |  /* was: MODE_POSTGRESQL          */ \
151    0x00200 |  /* was: MODE_ORACLE              */ \
152    0x00400 |  /* was: MODE_MSSQL               */ \
153    0x00800 |  /* was: MODE_DB2                 */ \
154    0x01000 |  /* was: MODE_MAXDB               */ \
155    0x02000 |  /* was: MODE_NO_KEY_OPTIONS      */ \
156    0x04000 |  /* was: MODE_NO_TABLE_OPTIONS    */ \
157    0x08000 |  /* was: MODE_NO_FIELD_OPTIONS    */ \
158    0x10000 |  /* was: MODE_MYSQL323            */ \
159    0x20000 |  /* was: MODE_MYSQL40             */ \
160    0x10000000 /* was: MODE_NO_AUTO_CREATE_USER */ \
161   )
162 
163 /*
164   Replication uses 8 bytes to store SQL_MODE in the binary log. The day you
165   use strictly more than 64 bits by adding one more define above, you should
166   contact the replication team because the replication code should then be
167   updated (to store more bytes on disk).
168 
169   NOTE: When adding new SQL_MODE types, make sure to also add them to
170   the scripts used for creating the MySQL system tables
171   in scripts/mysql_system_tables.sql and scripts/mysql_system_tables_fix.sql
172 */
173 
174 struct System_variables {
175   /*
176     How dynamically allocated system variables are handled:
177 
178     The global_system_variables and max_system_variables are "authoritative"
179     They both should have the same 'version' and 'size'.
180     When attempting to access a dynamic variable, if the session version
181     is out of date, then the session version is updated and realloced if
182     neccessary and bytes copied from global to make up for missing data.
183   */
184   ulong dynamic_variables_version;
185   char *dynamic_variables_ptr;
186   uint dynamic_variables_head;    /* largest valid variable offset */
187   uint dynamic_variables_size;    /* how many bytes are in use */
188   LIST *dynamic_variables_allocs; /* memory hunks for PLUGIN_VAR_MEMALLOC */
189 
190   ulonglong max_heap_table_size;
191   ulonglong tmp_table_size;
192   ulonglong long_query_time;
193   bool end_markers_in_json;
194   bool windowing_use_high_precision;
195   /* A bitmap for switching optimizations on/off */
196   ulonglong optimizer_switch;
197   ulonglong optimizer_trace;           ///< bitmap to tune optimizer tracing
198   ulonglong optimizer_trace_features;  ///< bitmap to select features to trace
199   long optimizer_trace_offset;
200   long optimizer_trace_limit;
201   ulong optimizer_trace_max_mem_size;
202   sql_mode_t sql_mode;  ///< which non-standard SQL behaviour should be enabled
203   ulonglong option_bits;  ///< OPTION_xxx constants, e.g. OPTION_PROFILING
204   ha_rows select_limit;
205   ha_rows max_join_size;
206   ulong auto_increment_increment, auto_increment_offset;
207   ulong bulk_insert_buff_size;
208   uint eq_range_index_dive_limit;
209   uint cte_max_recursion_depth;
210   ulonglong histogram_generation_max_mem_size;
211   ulong join_buff_size;
212   ulong lock_wait_timeout;
213   ulong max_allowed_packet;
214   ulong max_error_count;
215   ulong max_length_for_sort_data;  ///< Unused.
216   ulong max_points_in_geometry;
217   ulong max_sort_length;
218   ulong max_insert_delayed_threads;
219   ulong min_examined_row_limit;
220   ulong net_buffer_length;
221   ulong net_interactive_timeout;
222   ulong net_read_timeout;
223   ulong net_retry_count;
224   ulong net_wait_timeout;
225   ulong net_write_timeout;
226   ulong optimizer_prune_level;
227   ulong optimizer_search_depth;
228   ulonglong parser_max_mem_size;
229   ulong range_optimizer_max_mem_size;
230   ulong preload_buff_size;
231   ulong profiling_history_size;
232   ulong read_buff_size;
233   ulong read_rnd_buff_size;
234   ulong div_precincrement;
235   ulong sortbuff_size;
236   ulong max_sp_recursion_depth;
237   ulong default_week_format;
238   ulong max_seeks_for_key;
239   ulong range_alloc_block_size;
240   ulong query_alloc_block_size;
241   ulong query_prealloc_size;
242   ulong trans_alloc_block_size;
243   ulong trans_prealloc_size;
244   ulong group_concat_max_len;
245   ulong binlog_format;  ///< binlog format for this thd (see enum_binlog_format)
246   ulong rbr_exec_mode_options;  // see enum_rbr_exec_mode
247   bool binlog_direct_non_trans_update;
248   ulong binlog_row_image;  // see enum_binlog_row_image
249   bool binlog_trx_compression;
250   ulong binlog_trx_compression_type;  // see enum_binlog_trx_compression
251   uint binlog_trx_compression_level_zstd;
252   ulonglong binlog_row_value_options;
253   bool sql_log_bin;
254   // see enum_transaction_write_set_hashing_algorithm
255   ulong transaction_write_set_extraction;
256   ulong completion_type;
257   ulong transaction_isolation;
258   ulong updatable_views_with_limit;
259   uint max_user_connections;
260   ulong my_aes_mode;
261   ulong ssl_fips_mode;
262   /**
263     Controls what resultset metadata will be sent to the client.
264     @sa enum_resultset_metadata
265   */
266   ulong resultset_metadata;
267 
268   /**
269     In slave thread we need to know in behalf of which
270     thread the query is being run to replicate temp tables properly
271   */
272   my_thread_id pseudo_thread_id;
273   /**
274     Default transaction access mode. READ ONLY (true) or READ WRITE (false).
275   */
276   bool transaction_read_only;
277   bool low_priority_updates;
278   bool new_mode;
279   bool keep_files_on_create;
280 
281   bool old_alter_table;
282   bool big_tables;
283 
284   plugin_ref table_plugin;
285   plugin_ref temp_table_plugin;
286 
287   /* Only charset part of these variables is sensible */
288   const CHARSET_INFO *character_set_filesystem;
289   const CHARSET_INFO *character_set_client;
290   const CHARSET_INFO *character_set_results;
291 
292   /* Both charset and collation parts of these variables are important */
293   const CHARSET_INFO *collation_server;
294   const CHARSET_INFO *collation_database;
295   const CHARSET_INFO *collation_connection;
296 
297   /* Error messages */
298   MY_LOCALE *lc_messages;
299   /* Locale Support */
300   MY_LOCALE *lc_time_names;
301 
302   Time_zone *time_zone;
303   /*
304     TIMESTAMP fields are by default created with DEFAULT clauses
305     implicitly without users request. This flag when set, disables
306     implicit default values and expect users to provide explicit
307     default clause. i.e., when set columns are defined as NULL,
308     instead of NOT NULL by default.
309   */
310   bool explicit_defaults_for_timestamp;
311 
312   bool sysdate_is_now;
313   bool binlog_rows_query_log_events;
314 
315   double long_query_time_double;
316 
317   bool pseudo_slave_mode;
318 
319   Gtid_specification gtid_next;
320   Gtid_set_or_null gtid_next_list;
321   ulong session_track_gtids;  // see enum_session_track_gtids
322 
323   ulong max_execution_time;
324 
325   char *track_sysvars_ptr;
326   bool session_track_schema;
327   bool session_track_state_change;
328   ulong session_track_transaction_info;
329 
330   /*
331     Time in seconds, after which the statistics in mysql.table/index_stats
332     get invalid
333   */
334   ulong information_schema_stats_expiry;
335 
336   /**
337     Used for the verbosity of SHOW CREATE TABLE. Currently used for displaying
338     the row format in the output even if the table uses default row format.
339   */
340   bool show_create_table_verbosity;
341 
342   /**
343     Compatibility option to mark the pre MySQL-5.6.4 temporals columns using
344     the old format using comments for SHOW CREATE TABLE and in I_S.COLUMNS
345     'COLUMN_TYPE' field.
346   */
347   bool show_old_temporals;
348   // Used for replication delay and lag monitoring
349   ulonglong original_commit_timestamp;
350 
351   ulong
352       internal_tmp_mem_storage_engine;  // enum_internal_tmp_mem_storage_engine
353 
354   const CHARSET_INFO *default_collation_for_utf8mb4;
355 
356   /** Used for controlling preparation of queries against secondary engine. */
357   ulong use_secondary_engine;
358 
359   /**
360     Used for controlling which statements to execute in a secondary
361     storage engine. Only queries with an estimated cost higher than
362     this value will be attempted executed in a secondary storage
363     engine.
364   */
365   double secondary_engine_cost_threshold;
366 
367   /** Used for controlling Group Replication consistency guarantees */
368   ulong group_replication_consistency;
369 
370   bool sql_require_primary_key;
371 
372   /**
373     Used in replication to determine the server version of the original server
374     where the transaction was executed.
375   */
376   uint32_t original_server_version;
377 
378   /**
379     Used in replication to determine the server version of the immediate server
380     in the replication topology.
381   */
382   uint32_t immediate_server_version;
383 
384   /**
385     Used to determine if the database or tablespace should be encrypted by
386     default.
387   */
388   bool default_table_encryption;
389 
390   /**
391     @sa Sys_var_print_identified_with_as_hex
392   */
393   bool print_identified_with_as_hex;
394 
395   /**
396     @sa Sys_var_show_create_table_skip_secondary_engine
397   */
398   bool show_create_table_skip_secondary_engine;
399 
400   /**
401     @sa Sys_var_generated_random_password_length
402   */
403   uint32_t generated_random_password_length;
404 
405   /**
406     @sa Sys_var_require_row_format
407   */
408   bool require_row_format;
409 };
410 
411 /**
412   Per thread status variables.
413   Must be long/ulong up to last_system_status_var so that
414   add_to_status/add_diff_to_status can work.
415 */
416 
417 struct System_status_var {
418   /* IMPORTANT! See first_system_status_var definition below. */
419   ulonglong created_tmp_disk_tables;
420   ulonglong created_tmp_tables;
421   ulonglong ha_commit_count;
422   ulonglong ha_delete_count;
423   ulonglong ha_read_first_count;
424   ulonglong ha_read_last_count;
425   ulonglong ha_read_key_count;
426   ulonglong ha_read_next_count;
427   ulonglong ha_read_prev_count;
428   ulonglong ha_read_rnd_count;
429   ulonglong ha_read_rnd_next_count;
430   /*
431     This number doesn't include calls to the default implementation and
432     calls made by range access. The intent is to count only calls made by
433     BatchedKeyAccess.
434   */
435   ulonglong ha_multi_range_read_init_count;
436   ulonglong ha_rollback_count;
437   ulonglong ha_update_count;
438   ulonglong ha_write_count;
439   ulonglong ha_prepare_count;
440   ulonglong ha_discover_count;
441   ulonglong ha_savepoint_count;
442   ulonglong ha_savepoint_rollback_count;
443   ulonglong ha_external_lock_count;
444   ulonglong opened_tables;
445   ulonglong opened_shares;
446   ulonglong table_open_cache_hits;
447   ulonglong table_open_cache_misses;
448   ulonglong table_open_cache_overflows;
449   ulonglong select_full_join_count;
450   ulonglong select_full_range_join_count;
451   ulonglong select_range_count;
452   ulonglong select_range_check_count;
453   ulonglong select_scan_count;
454   ulonglong long_query_count;
455   ulonglong filesort_merge_passes;
456   ulonglong filesort_range_count;
457   ulonglong filesort_rows;
458   ulonglong filesort_scan_count;
459   /* Prepared statements and binary protocol. */
460   ulonglong com_stmt_prepare;
461   ulonglong com_stmt_reprepare;
462   ulonglong com_stmt_execute;
463   ulonglong com_stmt_send_long_data;
464   ulonglong com_stmt_fetch;
465   ulonglong com_stmt_reset;
466   ulonglong com_stmt_close;
467 
468   ulonglong bytes_received;
469   ulonglong bytes_sent;
470 
471   ulonglong max_execution_time_exceeded;
472   ulonglong max_execution_time_set;
473   ulonglong max_execution_time_set_failed;
474 
475   /* Number of statements sent from the client. */
476   ulonglong questions;
477 
478   /// How many queries have been executed on a secondary storage engine.
479   ulonglong secondary_engine_execution_count;
480 
481   ulong com_other;
482   ulong com_stat[(uint)SQLCOM_END];
483 
484   /*
485     IMPORTANT! See last_system_status_var definition below. Variables after
486     'last_system_status_var' cannot be handled automatically by add_to_status()
487     and add_diff_to_status().
488   */
489   double last_query_cost;
490   ulonglong last_query_partial_plans;
491 };
492 
493 /*
494   This must reference the LAST ulonglong variable in system_status_var that is
495   used as a global counter. It marks the end of a contiguous block of counters
496   that can be iteratively totaled. See add_to_status().
497 */
498 #define LAST_STATUS_VAR secondary_engine_execution_count
499 
500 /*
501   This must reference the FIRST ulonglong variable in system_status_var that is
502   used as a global counter. It marks the start of a contiguous block of counters
503   that can be iteratively totaled.
504 */
505 #define FIRST_STATUS_VAR created_tmp_disk_tables
506 
507 /* Number of contiguous global status variables. */
508 const int COUNT_GLOBAL_STATUS_VARS =
509     ((offsetof(System_status_var, LAST_STATUS_VAR) -
510       offsetof(System_status_var, FIRST_STATUS_VAR)) /
511      sizeof(ulonglong)) +
512     1;
513 
514 void add_diff_to_status(System_status_var *to_var, System_status_var *from_var,
515                         System_status_var *dec_var);
516 
517 void add_to_status(System_status_var *to_var, System_status_var *from_var);
518 
519 void reset_system_status_vars(System_status_var *status_vars);
520 
521 #endif  // SYSTEM_VARIABLES_INCLUDED
522