Skip to content

MDEV-27837: Disallow SET @@session.server_id within transaction#4660

Open
shabbann wants to merge 2 commits into
MariaDB:mainfrom
shabbann:MDEV-27837-disallow-set-@@session.server_id-within-transaction
Open

MDEV-27837: Disallow SET @@session.server_id within transaction#4660
shabbann wants to merge 2 commits into
MariaDB:mainfrom
shabbann:MDEV-27837-disallow-set-@@session.server_id-within-transaction

Conversation

@shabbann

Copy link
Copy Markdown
Contributor

Setting server_id within a transaction is dangerous for replication as it
can cause events to be filtered incorrectly on the replica, especially in
parallel replication mode.

This patch adds a check to prevent SET @@session.server_id from being
executed while a transaction is active

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Feb 17, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution. This is a preliminary review. Once this is done I will solicit a final review on this.

Comment thread mysql-test/suite/sys_vars/t/set_server_id_inside_transaction.test
Comment thread sql/sys_vars.cc Outdated
@shabbann
shabbann requested a review from gkodinov February 17, 2026 16:59

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for your quick fixes! Stand by for the final review please.

Comment thread sql/share/errmsg-utf8.txt Outdated
ER_STORAGE_ENGINE_DISABLED
eng "Storage engine %s is disabled"
spa "El motor de almacenaje %s está desactivado"
ER_CANT_SET_SERVER_ID_IN_TRANSACTION

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly sure we can't add new error messages to old versions. The numbering will conflict/renumber the newer versions.

If there's an unused error message that is still unused in the main branch that can be used. Otherwise the option is to reuse another or generic error message as best as possible.

@shabbann shabbann Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I searched for unused error messages in both 10.6 and main and found none.

My idea is either to return to ER_CANT_DO_THIS_DURING_AN_TRANSACTION or to edit only the description (not the code itself) of ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_GTID_DOMAIN_ID_SEQ_NO and add session.server to it

@bnestere bnestere Mar 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't add this patch to 10.6 anyway. I'd suggest just adding it to the main branch and then we don't have to worry about making error messages consistent across versions.

BTW, @grooverdan , we can add error messages to old versions, but before doing so, we have to backport the latest errmsg-utf8.txt from main to whatever version the error is being added to so things are consistent. At least, that is what @vuvova has told me in the past.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test binlog_in_engine.rpl_mysqlbinlog_slave_consistency_oob fails when I try to patch it in main

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the test appropriately. I'd imagine it tries to set session.server_id in a transaction somewhere. I quickly looked through the file but didn't see where immediately. You should be able to use the failure output to find where this happens. Then, likely, it could probably be set outside of the transaction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

working on it

@grooverdan
grooverdan self-requested a review March 4, 2026 03:08
@shabbann
shabbann changed the base branch from 10.6 to main March 6, 2026 00:10
@shabbann
shabbann changed the base branch from main to 10.6 March 6, 2026 00:26
@shabbann
shabbann force-pushed the MDEV-27837-disallow-set-@@session.server_id-within-transaction branch from d1a9df0 to e26cea1 Compare March 6, 2026 01:02
@shabbann
shabbann changed the base branch from 10.6 to main March 6, 2026 01:02
@shabbann

shabbann commented Mar 6, 2026

Copy link
Copy Markdown
Contributor Author

@bnestere

@vuvova

vuvova commented Apr 9, 2026

Copy link
Copy Markdown
Member

I'd prefer to avoid a per-variable error messages, better to have one generic one that takes a variable name in %s.

We already have six (!) such error messages per variable (one could've understood where it's going after the second one was added 😄)

@bnestere

bnestere commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

I agree with @vuvova it would indeed be better to make the message generic. Though I'm not sure whether or not we can switch the other error messages to use the new generic one, as it would break consistency when upgrading. @vuvova thoughts?

@grooverdan
grooverdan requested review from grooverdan and removed request for grooverdan April 14, 2026 03:45
@vuvova

vuvova commented Jun 8, 2026

Copy link
Copy Markdown
Member

I agree with @vuvova it would indeed be better to make the message generic. Though I'm not sure whether or not we can switch the other error messages to use the new generic one, as it would break consistency when upgrading. @vuvova thoughts?

Right, valid concern. There are two approaches here. Only use a generic message from now on, keep old variables on their individual messages. And an approach of ER_DUP_ENTRY vs. ER_DUP_ENTRY_WITH_KEYNAME. Originally it was

ER_DUP_ENTRY
        eng "Duplicate entry '%-.192s' for key %d"

and the usage was (simplified)

  my_error(ER_DUP_ENTRY, MYF(0), val->c_str(), key_num);

And it became

ER_DUP_ENTRY
        eng "Duplicate entry '%-.192T' for key %d"
ER_DUP_ENTRY_WITH_KEY_NAME
        eng "Duplicate entry '%-.64s' for key '%-.192s'"

used as (simplified)

  my_printf_error(ER_DUP_ENTRY, ER(ER_DUP_ENTRY_WITH_KEY_NAME), MYF(0),
    val->c_str(), table->key_info[key_num].name);

That is, it's the same error code, but a different error message.

@gkodinov

Copy link
Copy Markdown
Member

There has been no reply on Serg's comment for 3 weeks now. Converting to Draft. Please re-open when/if you intend to resume working on this.

@gkodinov
gkodinov marked this pull request as draft June 29, 2026 11:04
@gkodinov gkodinov added the need feedback Can the contributor please address the questions asked. label Jun 29, 2026
@shabbann

shabbann commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

hey @gkodinov I was waiting for the reviewers to pick an approach so I could go with it. I didn't think that the decision is up to me :)

@vuvova

vuvova commented Jul 6, 2026

Copy link
Copy Markdown
Member

ok,let's use a new common error message from now on:

ER_CANT_SET_IN_TRANSACTION
        eng "Cannot set %s within a transaction"
ER_CANT_SET_IN_SUBSTATEMENT
        eng "Cannot set %s within a trigger or stored program"

And use it for all variables, old too. But preserve old error numbers for old variables. Like

static bool
error_if_in_trans_or_substatement(THD *thd, const char *varname, int in_substatement_error,
                                  int in_transaction_error)
{
  if (thd->in_sub_stmt)
  {
    my_printf_error(in_substatement_error, ER_THD(thd, ER_CANT_SET_IN_SUBSTATEMENT),
                              MYF(0), varname);
    return true;
  }

shabbann added 2 commits July 14, 2026 02:48
Setting server_id within a transaction is dangerous for replication.
- Added error_if_in_trans_or_substatement validation to server_id variable.
- Added specific error codes ER_CANT_SET_SERVER_ID_IN_TRANSACTION and
  ER_CANT_SET_SERVER_ID_IN_SUBSTATEMENT
- Added proper COMMITs to sql_out_of_order_gtid.inc to ensure server_id
  isn't set inside a transaction
@shabbann
shabbann force-pushed the MDEV-27837-disallow-set-@@session.server_id-within-transaction branch from e26cea1 to 4ed020e Compare July 14, 2026 00:31
@shabbann
shabbann marked this pull request as ready for review July 14, 2026 00:33

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buildbot fails, please fix that.

Also, if you are set on keeping the two separate commits, please prefix them both with the MDEV- number.

--echo #
BEGIN;
INSERT INTO t1 VALUES (1);
--error ER_CANT_SET_SERVER_ID_IN_TRANSACTION

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fails as follows in buildbot:

sys_vars.set_server_id_inside_transaction w6 [ fail ]
        Test ended at 2026-07-14 00:52:24
CURRENT_TEST: sys_vars.set_server_id_inside_transaction
mysqltest: At line 14: Unknown SQL error name 'ER_CANT_SET_SERVER_ID_IN_TRANSACTION'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. need feedback Can the contributor please address the questions asked.

Development

Successfully merging this pull request may close these issues.

5 participants