{"id":549,"date":"2017-11-04T16:33:47","date_gmt":"2017-11-04T21:33:47","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=549"},"modified":"2017-11-04T16:33:47","modified_gmt":"2017-11-04T21:33:47","slug":"gtid_intersect","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=549","title":{"rendered":"GTID_INTERSECT"},"content":{"rendered":"<p>There&#8217;s a GTID_SUBTRACT function, and the manual shows how to write your own cheap GTID_UNION, but not a GTID_INTERSECT. Fortunately it&#8217;s easy to write your own, as it&#8217;s just subtracting twice.<\/p>\n<p><code>CREATE FUNCTION GTID_INTERSECT(g1 TEXT, g2 TEXT)<br \/>\n    RETURNS TEXT DETERMINISTIC<br \/>\n    RETURN GTID_SUBTRACT(g1, GTID_SUBTRACT(g1, g2));<\/code><\/p>\n<p>What use is it?<\/p>\n<p><code>SET @slave_executed = '33738f8c-c1a5-11e7-8fc3-0a002700000f:1-681577,<br \/>\n421d139e-04b9-11e7-b702-0050569935dc:1-13764443,<br \/>\n52b9a949-d79d-11e3-80dd-0050568d193e:1-1207378:1207380-1261803:1261805-1267098:1267100-1267416:1267418-1589733';<\/p>\n<p>SET @master_executed = '33738f8c-c1a5-11e7-8fc3-0a002700000f:1-730294,<br \/>\n421d139e-04b9-11e7-b702-0050569935dc:1-13764443,<br \/>\n52b9a949-d79d-11e3-80dd-0050568d193e:1-1207378:1207380-1261803:1261805-1589733';<\/p>\n<p>SET @master_purged = '33738f8c-c1a5-11e7-8fc3-0a002700000f:1-681582,<br \/>\n421d139e-04b9-11e7-b702-0050569935dc:1-13077260,<br \/>\n52b9a949-d79d-11e3-80dd-0050568d193e:1-1207378:1207380-1261803:1261805-1589733';<\/p>\n<p>SELECT @slave_needs := GTID_SUBTRACT(@master_executed, @slave_executed);<br \/>\n\/*-----------------------------------------------------+<br \/>\n| 33738f8c-c1a5-11e7-8fc3-0a002700000f:681578-730294,  |<br \/>\n| 52b9a949-d79d-11e3-80dd-0050568d193e:1267099:1267417 |<br \/>\n+-----------------------------------------------------*\/<\/p>\n<p>SELECT @slave_will_not_get := GTID_INTERSECT(@master_purged, @slave_needs);<br \/>\n\/*-----------------------------------------------------+<br \/>\n| 33738f8c-c1a5-11e7-8fc3-0a002700000f:681578-681582,  |<br \/>\n| 52b9a949-d79d-11e3-80dd-0050568d193e:1267099:1267417 |<br \/>\n+-----------------------------------------------------*\/<\/code><\/p>\n<p>While we&#8217;re on the subject, the manual&#8217;s <a href=\"https:\/\/dev.mysql.com\/doc\/en\/gtid-functions.html\">GTID_UNION<\/a> just concatenates the two sets, which means the output will list values twice. A small improvement will clean that up:<\/p>\n<p><code>CREATE FUNCTION GTID_UNION(g1 TEXT, g2 TEXT)<br \/>\n    RETURNS TEXT DETERMINISTIC<br \/>\n    RETURN GTID_SUBTRACT(CONCAT(g1,',',g2), '');<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a GTID_SUBTRACT function, and the manual shows how to write your own cheap GTID_UNION, but not a GTID_INTERSECT. Fortunately it&#8217;s easy to write your own, as it&#8217;s just subtracting twice. CREATE FUNCTION GTID_INTERSECT(g1 TEXT, g2 TEXT) RETURNS TEXT DETERMINISTIC RETURN GTID_SUBTRACT(g1, GTID_SUBTRACT(g1, g2)); What use is it? SET @slave_executed = &#8216;33738f8c-c1a5-11e7-8fc3-0a002700000f:1-681577, 421d139e-04b9-11e7-b702-0050569935dc:1-13764443, 52b9a949-d79d-11e3-80dd-0050568d193e:1-1207378:1207380-1261803:1261805-1267098:1267100-1267416:1267418-1589733&#8217;; SET [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-mysql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-8R","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=549"}],"version-history":[{"count":2,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":551,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions\/551"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}