Skip to content

MDEV-8235: Expose check_slave_start_position as SQL function GTID_CHECK_POS()#4956

Open
ayush-jha123 wants to merge 1 commit intoMariaDB:mainfrom
ayush-jha123:mdev-8235-gtid-pos
Open

MDEV-8235: Expose check_slave_start_position as SQL function GTID_CHECK_POS()#4956
ayush-jha123 wants to merge 1 commit intoMariaDB:mainfrom
ayush-jha123:mdev-8235-gtid-pos

Conversation

@ayush-jha123
Copy link
Copy Markdown

@ayush-jha123 ayush-jha123 commented Apr 18, 2026

I’ve been diving deep into the replication source code for the last two weeks,
investigating MDEV-8235. I noticed this task has been open for a while and realized
how useful it would be for DBAs to have a simple way to check GTID availability
directly from the SQL layer.

After studying how check_slave_start_position() works internally, I’ve implemented
a user-facing version called GTID_CHECK_POS().

What this PR does:

  • Extracted the internal GTID binlog-lookup logic into a shared helper function
    in sql_repl.cc to avoid code duplication.
  • Created the Item_func_gtid_check_pos class to wrap the logic for the SQL parser.
  • Added input validation: it returns 1 (exists), 0 (purged), or NULL (invalid input)
    which makes it robust for automation scripts.
  • Included an MTR test main.gtid_check_pos covering standard and edge cases.

I’m really excited to contribute this to MariaDB and would appreciate feedback
on the implementation.

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Apr 20, 2026
Copy link
Copy Markdown
Member

@gkodinov gkodinov left a comment

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.

First, the formalities:

  • please keep a single commit, with a commit message in it.
  • please make sure all of the buildbot hosts compile and run successfully

Below are some of my thoughts on the functionality and its implementation and test coverage. You can ignore all of the below and take it with the final reviewer. Or address it and have a faster final review with these fixed.

Comment thread sql/item_func.h Outdated
};


class Item_func_gtid_check_pos :public Item_int_func
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.

if you are aiming at returning a boolean why not derive from Item_bool_func?

Comment thread sql/sql_repl.cc Outdated

if (state.load(gtid_str->ptr(), gtid_str->length()))
{
if (thd && thd->is_error())
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.

Why clear the error? Why not just return it?

Comment thread sql/sql_repl.cc Outdated
errormsg= gtid_find_binlog_pos(&state, buf, &until_gtid_state, &until_binlog_state, &found_in_index, &out_start_seek);
until_binlog_state.free();
if (errormsg)
return 0;
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 can mean many things. E.g. the log file is absent, cannot be read etc. You will need a proper description of what the returns mean in the JIRA.

Comment thread sql/sql_repl.cc Outdated
gtid_find_binlog_pos returns an error message if the required
GTIDs have been purged from the binary logs.
*/
bool found_in_index= false;
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 is where the count (from the index) gets returned. I guess this is good enough to return. But, the actual use also scans the data. Why aren't you doing anything with it? The function can return empty error message but 0 found GTIDs and your function is returning 1 (a match).

Comment thread sql/sql_repl.cc Outdated
return -1;
}

if (state.load(gtid_str->ptr(), gtid_str->length()))
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 (afaiu) can take many gtids. Not just one. How do you account for that case?

Comment thread sql/item_func.cc Outdated
if ((null_value= args[0]->null_value))
return 0;

int result= gtid_check_pos(current_thd, gtid_str);
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.

you get a compile error for embedded here. ifdef the whole thing with HAVE_REPLICATION

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.

you've forgotten to git add the .result file for this.

--echo # Get the current GTID position manually and verify it exists
let $pos= `SELECT @@GLOBAL.gtid_binlog_pos`;
--replace_result $pos GTID_POS
eval SELECT GTID_CHECK_POS('$pos');
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.

you're missing a test for an otherwise valid GTID with the right server and domain id that's absent.

@gkodinov gkodinov self-assigned this Apr 20, 2026
This commit implements the GTID_CHECK_POS() SQL function, which allows
validating if a given GTID position is reachable within the current
set of binary logs. This is useful for external tools or orchestration
layers to verify if a node is viable for replication start without
actually initiating a slave connection.

Features:
- Returns 1 if all GTIDs in the requested state are reachable.
- Returns 0 if any GTID in the requested state has been purged.
- Propagates parsing errors for malformed GTID strings.
- Derived from Item_bool_func to ensure native boolean handling.
- Protected by HAVE_REPLICATION for safe embedded builds.

Implemented via a thin wrapper over the engine-internal
gtid_find_binlog_pos() logic.
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.

Development

Successfully merging this pull request may close these issues.

2 participants