Commit graph

76600 commits

Author SHA1 Message Date
Hashim Syed
b76760360a MDEV-12182 Add Client TCP Port Number to MySQL Audit Plugin Logs
In environments with load balancers or proxies, the audit plugin logs
only the IP address, making it difficult to differentiate individual client
connections from the same IP.

Add a new 'port' field to the appropriate event objects to capture the
client's TCP port number. Populate the port field with thd->port  in the
appropriate functions. The audit plugin receives and logs this port field
along  with  other connection information, enabling better identification
of individual client connections.

All new code of the whole pull request, including one or several files that
are either new files or modified ones, are contributed under the BSD-new license.
I am contributing on behalf of my employer Amazon Web Services, Inc.
2025-03-19 22:22:51 +01:00
Meng-Hsiu Chiang
cdd080705a MDEV-33834 Extend audit plugin to include tls_version and tls_version_length variables
Add tls_version and tls_version_length variables to the audit plugin so
they can be logged. This is useful to help identify suspicious or
malformed connections attempting to use unsupported TLS versions. A log
with this information will allow to detect and block more malicious
connection attempts.

Users with 'server_audit_events' empty will have these two new variables
automatically visible in their logs, but if users don't want them, they
can always configure what fields to include by listing the fields in
'server_audit_events'.

In connection event, The TLS version will be populated in `object` field
in key=value format, and the key-value pair will be omitted when the
value is empty.

To ensure the MTR test result matches in all environments, the TLS
version string is replaced with a general `TLS_VERSION` to avoid the MTR
test failing unexpectedly. It stores the version with query `SHOW STATUS
LIKE 'Ssl_version'` and replace the output with `replace_result` command.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.

[1]: https://docs.openssl.org/3.2/man3/SSL_get_version/
2025-03-19 22:22:51 +01:00
Sergei Golubchik
a6b0394cff more fixes for windows 2025-03-19 12:11:58 +01:00
Sergei Golubchik
5ed14c2fde second batch of C-O-R fixes
--embed versioning.partition main.create_or_replace atomic.create_replace_no_binlog atomic.create_table_no_binlog main.create_not_windows period.create
2025-03-19 10:43:19 +01:00
Sergei Golubchik
6ff7313172 atomic COR compilation on windows 2025-03-18 22:38:20 +01:00
Dave Gosselin
58c1ef4563 MDEV-36094 Row ID filtering for reverse-ordered scans
The fix for MDEV-34413 added support for Index Condition Pushdown with reverse
ordered scans.  This makes Rowid filtering work with reverse-ordered scans, too,
so enable it.  For example, InnoDB can now check the pushed index condition and
then check the rowid filter on success, in the ORDER BY ... DESC case.
2025-03-18 18:35:58 +01:00
Dave Gosselin
e059b131ba MDEV-34413 Index Condition Pushdown for reverse ordered scans
Allows index condition pushdown for reverse ordered scans, a previously
disabled feature due to poor performance.  This patch adds a new
API to the handler class called set_end_range which allows callers to
tell the handler what the end of the index range will be when scanning.
Combined with a pushed index condition, the handler can scan the index
efficiently and not read beyond the end of the given range.  When
checking if the pushed index condition matches, the handler will also
check if scanning has reached the end of the provided range and stop if
so.

If we instead only enabled ICP for reverse ordered scans without
also calling this new API, then the handler would perform unnecessary
index condition checks.  In fact this would continue until the end of
the index is reached.

These changes are agnostic of storage engine.  That is, any storage
engine that supports index condition pushdown will inhereit this new
behavior as it is implemented in the SQL and storage engine
API layers.

The partitioned tables storage meta-engine (ha_partition) adds an
override of set_end_range which recursively calls set_end_range on its
child storage engine (handler) implementations.

This commit updates the test made in an earlier commit to show that
ICP matches happen for the reverse ordered case.

This patch is based on changes written by Olav Sandstaa in
MySQL commit da1d92fd46071cd86de61058b6ea39fd9affcd87
2025-03-18 18:35:06 +01:00
Alexander Barkov
87cb0722f5 MDEV-36322 Comparison ROW(stored_func(),1)=ROW(1,1) calls the function twice per row
Item_func_sp::execute() called two times per row in this scenario:

SELECT ROW(f1(),1) = ROW(1,1), @counter FROM seq_1_to_5;

Fix:

Adding a new bool member Item_func_sp::m_use_cached_value,
which in case of "true" means that the Item_func_sp::execute()
has already been executed for the current result set row and
its return value has been cached in Item_func_sp::sp_result_field.

All Item_func_sp value methods (like val_int() in the reported query)
check m_use_cached_value. If it is "true" then execute() is not
called again and the function value is returned using directly
sp_result_field value methods.

Item_func_sp::bring_value() calls execute() no matter what m_use_cached_value is.
2025-03-18 18:31:28 +01:00
Sergei Golubchik
06446daef3 MDEV-20034 post-cherry-pick 2025-03-18 18:31:28 +01:00
Alexander Barkov
e6961ea311 MDEV-20034 Add support for the pre-defined weak SYS_REFCURSOR
This patch adds support for SYS_REFCURSOR (a weakly typed cursor)
for both sql_mode=ORACLE and sql_mode=DEFAULT.

Works as a regular stored routine variable, parameter and return value:

- can be passed as an IN parameter to stored functions and procedures
- can be passed as an INOUT and OUT parameter to stored procedures
- can be returned from a stored function

Note, strongly typed REF CURSOR will be added separately.

Note, to maintain dependencies easier, some parts of sql_class.h
and item.h were moved to new header files:

- select_results.h:
  class select_result_sink
  class select_result
  class select_result_interceptor

- sp_cursor.h:
  class sp_cursor_statistics
  class sp_cursor

- sp_rcontext_handler.h
  class Sp_rcontext_handler and its descendants

The implementation consists of the following parts:
- A new class sp_cursor_array deriving from Dynamic_array

- A new class Statement_rcontext which contains data shared
  between sub-statements of a compound statement.
  It has a member m_statement_cursors of the sp_cursor_array data type,
  as well as open cursor counter. THD inherits from Statement_rcontext.

- A new data type handler Type_handler_sys_refcursor in plugins/type_cursor/
  It is designed to store uint16 references -
  positions of the cursor in THD::m_statement_cursors.

- Type_handler_sys_refcursor suppresses some derived numeric features.
  When a SYS_REFCURSOR variable is used as an integer an error is raised.

- A new abstract class sp_instr_fetch_cursor. It's needed to share
  the common code between "OPEN cur" (for static cursors) and
  "OPER cur FOR stmt" (for SYS_REFCURSORs).

- New sp_instr classes:
  * sp_instr_copen_by_ref      - OPEN sys_ref_curor FOR stmt;
  * sp_instr_cfetch_by_ref     - FETCH sys_ref_cursor INTO targets;
  * sp_instr_cclose_by_ref     - CLOSE sys_ref_cursor;
  * sp_instr_destruct_variable - to destruct SYS_REFCURSOR variables when
                                 the execution goes out of the BEGIN..END block
                                 where SYS_REFCURSOR variables are declared.
- New methods in LEX:
  * sp_open_cursor_for_stmt   - handles "OPEN sys_ref_cursor FOR stmt".
  * sp_add_instr_fetch_cursor - "FETCH cur INTO targets" for both
                                static cursors and SYS_REFCURSORs.
  * sp_close - handles "CLOSE cur" both for static cursors and SYS_REFCURSORs.

- Changes in cursor functions to handle both static cursors and SYS_REFCURSORs:
  * Item_func_cursor_isopen
  * Item_func_cursor_found
  * Item_func_cursor_notfound
  * Item_func_cursor_rowcount

- A new system variable @@max_open_cursors - to limit the number
  of cursors (static and SYS_REFCURSORs) opened at the same time.
  Its allowed range is [0-65536], with 50 by default.

- A new virtual method Type_handler::can_return_bool() telling
  if calling item->val_bool() is allowed for Items of this data type,
  or if otherwise the "Illegal parameter for operation" error should be raised
  at fix_fields() time.

- New methods in Sp_rcontext_handler:
  * get_cursor()
  * get_cursor_by_ref()

- A new class Sp_rcontext_handler_statement to handle top level statement
  wide cursors which are shared by all substatements.

- A new virtual method expr_event_handler() in classes Item and Field.
  It's needed to close (and make available for a new OPEN)
  unused THD::m_statement_cursors elements which do not have any references
  any more. It can happen in various moments in time, e.g.
  * after evaluation parameters of an SQL routine
  * after assigning a cursor expression into a SYS_REFCURSOR variable
  * when leaving a BEGIN..END block with SYS_REFCURSOR variables
  * after setting OUT/INOUT routine actual parameters from formal
    parameters.
2025-03-18 18:31:28 +01:00
ParadoxV5
8d6ff0db25 MDEV-27669: Add skip-slave-start info message
When a slave does not start up the slave threads on restart,
but not reporting anything to the error log about startup failures
either, this can be due to `skip-slave-start` being set in the
config file(s) or on the command line (and most likely is).

Reviewed-by: Sergei Golubchik <serg@mariadb.org>
2025-03-18 18:31:28 +01:00
ParadoxV5
ec01103f92 MDEV-30189 Add remaining replication options as system variables
Promote the last few SQL-inaccessible replication options (command line
or `mariadb.cnf`) as these GLOBAL read-only system variables:
```
@@master_info_file
@@replicate_same_server_id
@@show_slave_auth_info
```

Side effect: The latter two options changed from no argument
to optional argument. Quote `include/my_getopt.h`:
> It should be noted that for historical reasons variables with the
> combination arg_type=NO_ARG, my_option::var_type=GET_BOOL still
> accepts arguments. This is someone counter intuitive and care should
> be taken if the code is refactored.

Reviewed-by: Brandon Nesterenko <brandon.nesterenko@mariadb.com>
2025-03-18 18:28:20 +01:00
Yuchen Pei
051e5c3739 MDEV-34712 Add support to sha2 and pbkdf2 key derivation in file_key_management
Add two new variables:

file_key_management_digest={sha1|sha224|sha256|sha384|sha512}

This specify the digest function to use in key derivation of the key
used for decryption of the keyfile.

file_key_management_use_pbkdf2=N

This specify whether pbkdf2 is used in the key derivation, and if
so (N != 0), how many iterations.
2025-03-18 18:28:20 +01:00
Denis Protivensky
c5688a17a0 MDEV-34822: Skip FK checks in Galera during applying in IST
Appliers need to verify foreign key constraints during normal
operation, in multi-active topologies, and for this reason appliers
are configured to enable FK checking.

However, during node joining, in IST and latter catch up period,
the node is still idle (from local connections), and only source
for incoming transactions is the cluster sending certified write
sets for applying. IST happens with parallel applying, and there
is a possibility that foreign key check cause lock conflicts between
appliers accessing FK child and parent tables. Also, the excessive
FK checking will slow down IST process somewhat.

For this reasons, we could relax FK checks for appliers during IST
and catch up periods. The relaxed FK check mode should, however, be
configurable e.g. by wsrep_mode flag: SKIP_APPLIER_FK_CHECKS_IN_IST.
When this operation mode is set, and the node is processing IST or
catch up, appliers should skip FK checking.

Signed-off-by: Julius Goryavsky <julius.goryavsky@mariadb.com>
2025-03-18 18:28:20 +01:00
Vladislav Vaintroub
ff63885b60 MDEV-14091 - Address review comments
- Return error when reading file-based password from locations readable
  by LOAD DATA or LOAD_FILE().

- If ssl_passphrase was set, SHOW VARIABLE will show "file:", "env:" or
  "pass:" (but won't reveal sensitive data)
2025-03-18 18:28:20 +01:00
Vladislav Vaintroub
42fe974075 MDEV-14091 Support password protected SSL key in server.
Add ssl_passphrase server parameter, which works similarly
to --passout/--passin openssl command line parameters.

Pass phrase value can be formatted as follows.

- pass:password
Provide actual password after the pass: prefix.

- env:var
Obtain the password from the environment variable 'var'a

- file:pathname
Reads the password from the specified file pathname.
Only the first line, up to the newline character, is read from the stream.
2025-03-18 18:28:20 +01:00
Sergei Golubchik
77fadec4dd fix compilation failures
-Wformat-security
2025-03-18 18:28:20 +01:00
Oleg Smirnov
5c9b97953f MDEV-34870 Fix code review comments, part 3 2025-03-18 18:28:20 +01:00
Oleg Smirnov
d2593693f9 MDEV-34870 Correctly process nested joins having multiple levels of embedding 2025-03-18 18:28:20 +01:00
Oleg Smirnov
f8a39ffc18 MDEV-34870 Correctly update JOIN_TAB::key_dependent after applying hints 2025-03-18 18:28:20 +01:00
Oleg Smirnov
3971ba6d98 MDEV-34870 Fix code review comments, part 2 2025-03-18 18:28:20 +01:00
Oleg Smirnov
ee2dc6ac47 MDEV-34870 Fix code review comments 2025-03-18 18:28:20 +01:00
Oleg Smirnov
9955997429 MDEV-34870: implement join order hints
This commit implements optimizer hints allowing to affect the order
of joining tables:

 - JOIN_FIXED_ORDER similar to existing STRAIGHT_JOIN hint;
 - JOIN_ORDER to apply the specified table order;
 - JOIN_PREFIX to hint what tables should be first in the join;
 - JOIN_SUFFIX to hint what tables should be last in the join.
2025-03-18 18:28:20 +01:00
Sergei Petrunia
c5b324b41b Code cleanup:
- Make print_warn() not be a template.
- Introduce Printable_parser_rule interface, make grammar rules that
  emit warnings implement it ( and print_warn invokes its function).

- Remove Parser::Hint::append_args() as it is not used anywhere.
  (it used to be necessary call print_warn(... (Parser::Hint*)NULL);
2025-03-18 18:28:20 +01:00
Sergei Petrunia
71ad912ee4 Add dbug_print_hints() 2025-03-18 18:28:20 +01:00
Sergei Petrunia
67bdf89990 Optimizer trace support for hints. No testcase yet. 2025-03-18 18:28:19 +01:00
Sergei Petrunia
fea9d2bf57 Rename st_opt_hint_info::hint_name to hint_type.
It's not obvious but "name" is the name of the object hint attaches to.
And "type" is the word like "MRR" or "SEMIJOIN"
2025-03-18 18:28:19 +01:00
Sergei Petrunia
37c5adeb15 - Code cleanup in hint printing: remove get_args_printer(),
add append_hint_arguments(THD *thd, opt_hints_enum hint, String *str).
- Added more comments
2025-03-18 18:28:19 +01:00
Oleg Smirnov
19e832dc85 MDEV-35504 Fix opt_hint_timeout.test for embedded; fix mariadb client
1. Disable opt_hint_timeout.test for embedded server. Make sure
the test does not crash even when started for embedded server.
Disable view-protocol since hints are not supported inside views.

2. Hints are designed to behave like regular /* ... */ comments:
   `SELECT /*+ " */ 1;` -- a valid SQL query
However, the mysql client program waits for the closing doublequote
character.
Another problem is observed when there is no space character between
closing `*/` of the hint and the following `*`:
   `SELECT /*+ some_hints(...) */* FROM t1;`
In this case the client treats `/*` as a comment section opening and
waits for the closing `*/` sequence.

This commit fixes all of these issues
2025-03-18 18:28:19 +01:00
Sergei Petrunia
1169eb4358 Code cleanup:
- Rename Hint "adjustment" and "resolution" (yes, both terms were used)
to "fixing". "Resolution" is already used for parse-tree objects.
2025-03-18 18:28:19 +01:00
Sergei Petrunia
5a0e192828 Cleanup:
- add a comment that opt_hints_global->check_unresolved() is never called
- improve comments
- Rename everything with "resolved_children" to "fully_resolved_children"
- Opt_hints_table::adjust_key_hints() now returns value.
  Less "reach-back-to-parent" logic.
2025-03-18 18:28:19 +01:00
Oleg Smirnov
9a86120049 MDEV-34888 Implement SEMIJOIN() and SUBQUERY() hints 2025-03-18 18:28:19 +01:00
Oleg Smirnov
7d7c2b4971 MDEV-35504 Fix post-rebase conflicts 2025-03-18 18:28:19 +01:00
Oleg Smirnov
4468a19a3e MDEV-34860 Make the hint override global/session/statement setting of max_statement_time 2025-03-18 18:28:19 +01:00
Oleg Smirnov
cb2a79db8c MDEV-33281 MDEV-33281 Make BNL() hint enable hashed join buffers 2025-03-18 18:28:19 +01:00
Oleg Smirnov
e22980ad52 MDEV-34760 Fix potential problem with append_args() 2025-03-18 18:28:19 +01:00
Oleg Smirnov
991cec8cee MDEV-34860 Fix review comments 2025-03-18 18:28:19 +01:00
Oleg Smirnov
fcba1ff661 MDEV-34860 Implement MAX_EXECUTION_TIME hint
It places a limit N (a timeout value in milliseconds) on how long
a statement is permitted to execute before the server terminates it.

Syntax:
SELECT /*+ MAX_EXECUTION_TIME(milliseconds) */ ...

Only top-level SELECT statements support the hint.
2025-03-18 18:28:19 +01:00
Oleg Smirnov
f313bffb09 MDEV-33281 Make BNL() hint work for join_cache_levels from 0 to 3
BNL() hint effectively increases join_cache_level up to 4 if it is
set to value less than 4.
This commit also makes the BKA() hint override not only
`join_cache_bka` optimizer switch but `join_cache_level` as well.
I.e., BKA() hint enables BKA and BKAH join buffers both flat and
incremental despite `join_cache_level` and `join_cache_bka` setting.
2025-03-18 18:28:19 +01:00
Sergei Petrunia
4044dbb761 Code cleanup in parser:
- Comments in simple_parser.h
- Rename Optimizer_hint_parser::PARSER to Parser (it's not a template
  argument, don't use caps)
2025-03-18 18:28:19 +01:00
Sergei Petrunia
94c6932d08 Add an overview comment about hints 2025-03-18 18:28:19 +01:00
Sergei Petrunia
e3970cd7c1 Add a comment describing the parser 2025-03-18 18:28:19 +01:00
Sergei Petrunia
fd91219b0b Make parsed output: SELECT_LEX::parsed_optimizer_hints const.
This required changing some other functions to const too.
2025-03-18 18:28:18 +01:00
Oleg Smirnov
3aa1bf0e0d MDEV-33281 Make BNL() hint work for join_cache_level=0
join_cache_level=0 disables join cache buffers, but the hint
BNL() now allows to employ BNL(H) buffers for particular tables
or query blocks.

This commit also adds a number of test cases including
OUTER JOINs to make sure hints do not break the rules of
join buffers application
2025-03-18 18:28:18 +01:00
Oleg Smirnov
3923b10894 MDEV-33281 Implement optimizer hints
Forbid adding optimizer hints to view definitions.
In the case when optimizer hints are added to the view definition
at a `CREATE (OR REPLACE) VIEW`/`ALTER VIEW` statement, a warning is
generated and the hints are ignored.

This commit also disables ps-protocol for test cases where
`Unresolved table/index name` warnings are generated. The reason
for this is such warnings are generated during both PREPARE
and EXECUTE stages. Since opt_hints.test has `--enable_prepare_warnings`,
running it with `--ps-protocol` causes duplication of warning messages
2025-03-18 18:28:18 +01:00
Oleg Smirnov
28a59a4e29 MDEV-33281 Fix code review comments 2025-03-18 18:28:18 +01:00
Alexander Barkov
8879474654 MDEV-33281 Implement optimizer hints
- Using Lex_ident_sys to scan identifiers, like the SQL parser does.

  This fixes handling of double-quote-delimited and backtick-delimited identifiers,
  as well as handling of non-ASCII identifiers.

  Unescaping and converting from the client character set to the system
  character set is now done using Lex_ident_cli_st and Lex_ident_sys,
  like it's done in the SQL tokenizer/parser.
  Adding helper methods to_ident_cli() and to_ident_sys()
  in Optimizer_hint_parser::Token.

- Fixing the hint parser to report a syntax error when an empty identifiers:
    SELECT /*+ BKA(``) */ * FROM t1;

- Moving a part of the code from opt_hints_parser.h to opt_hints_parser.cc

  Moving these method definitions:
  - Optimizer_hint_tokenizer::find_keyword()
  - Optimizer_hint_tokenizer::get_token()

  to avoid huge pieces of the code in the header file.

- A Lex_ident_cli_st cleanup
  Fixing a few Lex_ident_cli_st methods to return Lex_ident_cli_st &
  instead of void, to use them easier in the caller code.

- Fixing the hint parser to display the correct line number

  Adding a new data type Lex_comment_st
  (a combination of LEX_CSTRING and a line number)
  Using it in sql_yacc.yy

- Getting rid of redundant dependencies on sql_hints_parser.h

  Moving void LEX::resolve_optimizer_hints() from sql_lex.h to sql_lex.cc

  Adding a class Optimizer_hint_parser_output, deriving from
  Optimizer_hint_parser::Hint_list. Fixing the hint parser to
  return a pointer to an allocated instance of Optimizer_hint_parser_output
  rather than an instance of Optimizer_hint_parser::Hint_list.
  This allows to use a forward declaration of Optimizer_hint_parser_output
  in sql_lex.h and thus avoid dependencies on sql_hints_parser.h.
2025-03-18 18:28:18 +01:00
Oleg Smirnov
52c96bef29 MDEV-33281 Implement optimizer hints
This commit introduces:
    - the infrastructure for optimizer hints;
    - hints for join buffering: BNL(), NO_BNL(), BKA(), NO_BKA();
    - NO_ICP() hint for disabling index condition pushdown;
    - MRR(), MO_MRR() hint for multi-range reads control;
    - NO_RANGE_OPTIMIZATION() for disabling range optimization;
    - QB_NAME() for assigning names for query blocks.
2025-03-18 18:28:18 +01:00
Alexander Barkov
19b7318a63 MDEV-33281 Implement optimizer hints
Implementing a recursive descent parser for optimizer hints.
2025-03-18 18:28:18 +01:00
Dmitry Shulga
c170aaf06b MDEV-10164: Add support for TRIGGERS that fire on multiple events
Added capability to create a trigger associated with several trigger events.
For this goal, the syntax of the CREATE TRIGGER statement was extended
to support the syntax structure { event [ OR ... ] } for the `trigger_event`
clause. Since one trigger will be able to handle several events it should be
provided a way to determine what kind of event is handled on execution of a
trigger. For this goal support of the clauses INSERTING, UPDATING , DELETING
was added by this patch. These clauses can be used inside a trigger body
to detect what kind of trigger action is currently processed using the following
boilerplate:
  IF INSERTING THEN ...
  ELSIF UPDATING THEN ...
  ELSIF DELETING THEN ...
In case one of the clauses INSERTING, UPDATING, DELETING specified in
a trigger's body not matched with a trigger event type, the error
ER_INCOMPATIBLE_EVENT_FLAG is emitted.

After this patch be pushed, one Trigger object will be associated with several
trigger events. It means that the array Table_triggers_list::triggers can
contain several pointers to the same Trigger object in array members
corresponding to different events. Moreover, support of several trigger events
for the same trigger requires that the data members `next` and `action_order`
of the Trigger class be converted to arrays to store relating information per
trigger event base.

Ability to specify the same trigger for different event types results in
necessity to handle invalid cases on execution of the multi-event trigger,
when the OLD or NEW qualifiers doesn't match a current event type against that
the trigger is run. The clause OLD should produce the NULL value for INSERT event,
whereas the clause NEW should produce the NULL value for DELETE event.
2025-03-18 18:28:18 +01:00