Skip to content

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

Open
ayush-jha123 wants to merge 5 commits into
MariaDB: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 5 commits into
MariaDB:mainfrom
ayush-jha123:mdev-8235-gtid-pos

Conversation

@ayush-jha123
Copy link
Copy Markdown
Contributor

@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
Comment thread sql/sql_repl.cc Outdated
Comment thread sql/sql_repl.cc Outdated
Comment thread sql/sql_repl.cc Outdated
Comment thread sql/sql_repl.cc Outdated
Comment thread sql/item_func.cc Outdated
Comment thread mysql-test/main/gtid_check_pos.test
Comment thread mysql-test/main/gtid_check_pos.test
@gkodinov gkodinov self-assigned this Apr 20, 2026
@ayush-jha123 ayush-jha123 force-pushed the mdev-8235-gtid-pos branch 2 times, most recently from 81aa4a1 to c4377de Compare April 24, 2026 17:57
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.
@vuvova vuvova requested a review from knielsen May 13, 2026 10:22
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.

It's looking much better. Please make sure to address the comments below. This is my last batch of preliminary review comments.
Thanks for your efforts.

Comment thread sql/sql_repl.cc
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.

revert these please

Comment thread libmariadb
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.

revert this please.

Comment thread sql/item_func.cc
@ayush-jha123
Copy link
Copy Markdown
Contributor Author

Hi @gkodinov,

I've just pushed the updated commit addressing your latest feedback:

Compilaton: Added #include "sql_repl.h" to sql/item_func.cc to fix the scope error you observed on Buildbot.
Submodules: Reverted all changes to the libmariadb and storage/columnstore/columnstore submodules.
Whitespace: I've ensured that no space-only changes are included in the diff; the comment lines and empty lines now match the original source exactly.

Thanks for the review, let me know if there's anything else!

@ayush-jha123 ayush-jha123 force-pushed the mdev-8235-gtid-pos branch 4 times, most recently from ca84483 to 75568df Compare May 25, 2026 05:13
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 working on this. Some of the test results do not make a lot of sense it seems.

It'd be good to have even more test coverage: more edge cases, more positive and negative outcomes, scenarios producing all the possible return values and/or errors etc.

And also a better design definition of what the SQL function does is going to help a lot: e.g. possible return values, when are they returned, formal description or parameters, what determines the return value (just the parameter values or some other state, etc.

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

#ifndef 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.

I'd reverse the condition for readability. But that's optional.

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.

It is looking much better now! Thanks for the updated tests. This makes it look good. Some final remarks below.

SELECT GTID_CHECK_POS('NEW_GTID_POS');
GTID_CHECK_POS('NEW_GTID_POS')
1
# The old position (now in a purged log) returns 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 one fails on buildbot:

main.gtid_check_pos                      w7 [ fail ]
        Test ended at 2026-06-03 19:55:16

CURRENT_TEST: main.gtid_check_pos
--- C:/a/server/server/mysql-test/main/gtid_check_pos.result	2026-06-03 19:12:08.051490800 +0000
+++ C:\a\server\server\mysql-test\main\gtid_check_pos.reject	2026-06-03 19:55:13.752902600 +0000
@@ -30,7 +30,7 @@
 # The old position (now in a purged log) returns 0
 SELECT GTID_CHECK_POS('OLD_GTID_POS');
 GTID_CHECK_POS('OLD_GTID_POS')
-0
+1

Comment thread sql/item_func.cc Outdated
@@ -37,6 +37,7 @@
#include "sql_acl.h" // EXECUTE_ACL
#include "mysqld.h" // LOCK_short_uuid_generator
#include "rpl_mi.h"

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.

no white-space only changes please.

Comment thread sql/item_func.cc Outdated
and verifies if they exist and are reachable (viable) within the current
set of binary logs.

@param gtid_str A string representation of GTIDs (e.g. '0-1-1,0-1-2').
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 will make doxygen return an error if one day it was run on these files. This is a method documentation block. And yet, you're talking about the SQL function itself. I'd move this particular block towards the class definition (?), reformat it a bit so that there's no @return or @param directives and then add a real one here explaining the return value and what the function does in specific.

Comment thread sql/item_func.cc Outdated

@param gtid_str A string representation of GTIDs (e.g. '0-1-1,0-1-2').

@return
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.

fwiw, use @RetVal if you want to do good doxygen. Or don't do doxygen.

Comment thread sql/sql_repl.cc
@@ -5838,4 +5839,113 @@ int compare_log_name(const char *log_1, const char *log_2) {
return res;
}

/**
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.

that's what good doxygen looks like! Nice! I'd consider doing some @sa too towards the bigger picture/subsystem. But this is optional.

@ayush-jha123
Copy link
Copy Markdown
Contributor Author

Thnks again for the review, I pushed an update addressing your final remarks:

  1. Buildbot w7 failure (gtid_check_pos.test):
    The test was failing on Windows because the active binary log was occasionally locked by a background thread, preventing PURGE BINARY LOGS TO from successfully removing it before the subsequent GTID_CHECK_POS() check was made. I have refactored this block to use --source include/wait_for_purge.inc (wrapped in --disable_result_log), which safely polls until the log is genuinely purged.
  2. Whitespace only changes: Removed the empty newline after #include "rpl_mi.h" in item_func.cc.
  3. Doxygen Cleanup:
    • I moved the general overview documentation for the SQL function into item_cmpfunc.h right above the class definition (and removed @param and @return).
    • I replaced the documentation block for val_bool() in item_func.cc with a specific method-level explanation using the proper @retval tags.
    • I added the @sa cross-references in sql_repl.cc to tie it into the bigger picture.

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