<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Thanks Giving Challenge:  How to detect replication context</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/</link>
	<description>Everything about MySQL Performance</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:23:57 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Robert Hodges</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-398605</link>
		<dc:creator>Robert Hodges</dc:creator>
		<pubDate>Mon, 01 Dec 2008 17:52:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-398605</guid>
		<description>Hi Peter, 

This is a very interesting question.  I would say cheat and use a different replication product (Tungsten Replicator) that automatically sets a session variable as follows whenever it logs in to apply changes to the slave:  

  set @rep_status=&#039;slave&#039;;

On a non-slave connection this value will be null, whereas the slave connection will have the preceding value.  I just added a JIRA to get this added to Tungsten Replicator and will try to get it into our beta-3 build, which is due for later in the month of December.  (JIRA reference:  https://forge.continuent.org/jira/browse/TREP-130)

Cheers, Robert</description>
		<content:encoded><![CDATA[<p>Hi Peter, </p>
<p>This is a very interesting question.  I would say cheat and use a different replication product (Tungsten Replicator) that automatically sets a session variable as follows whenever it logs in to apply changes to the slave:  </p>
<p>  set @rep_status=&#8217;slave&#8217;;</p>
<p>On a non-slave connection this value will be null, whereas the slave connection will have the preceding value.  I just added a JIRA to get this added to Tungsten Replicator and will try to get it into our beta-3 build, which is due for later in the month of December.  (JIRA reference:  <a href="https://forge.continuent.org/jira/browse/TREP-130)" rel="nofollow">https://forge.continuent.org/jira/browse/TREP-130)</a></p>
<p>Cheers, Robert</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-396775</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sun, 30 Nov 2008 06:06:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-396775</guid>
		<description>Shlomi,

Thanks. I tried that too. The results were very strange.  It was returning &quot;&quot; for replication events until I did an update to this table besides replication. In this case it changed to the user which did update even for thread which did updates. But yes this is exactly what I&#039;d expect to work:)</description>
		<content:encoded><![CDATA[<p>Shlomi,</p>
<p>Thanks. I tried that too. The results were very strange.  It was returning &#8220;&#8221; for replication events until I did an update to this table besides replication. In this case it changed to the user which did update even for thread which did updates. But yes this is exactly what I&#8217;d expect to work:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shlomi Noach</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-396771</link>
		<dc:creator>Shlomi Noach</dc:creator>
		<pubDate>Sun, 30 Nov 2008 05:35:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-396771</guid>
		<description>Peter (comment #6) - Try using USER(). In triggers, CURRENT_USER() will relate to the &#039;definer&#039; of the trigger.
USER() will relate to the invoker of the trigger

Regards
Shlomi</description>
		<content:encoded><![CDATA[<p>Peter (comment #6) &#8211; Try using USER(). In triggers, CURRENT_USER() will relate to the &#8216;definer&#8217; of the trigger.<br />
USER() will relate to the invoker of the trigger</p>
<p>Regards<br />
Shlomi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shlomi Noach</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-396065</link>
		<dc:creator>Shlomi Noach</dc:creator>
		<pubDate>Sat, 29 Nov 2008 14:16:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-396065</guid>
		<description>Solution:
Use the UUID function twice. First time, use it as a function which does not replicate well. Second time, allow it to replicate as it were on the master.
Compare the last part of both results: that&#039;s the host part. If both are equal, we are on the master. If different - we are on the slave.
The following stored procedure writes &#039;master&#039; or &#039;slave&#039; in the `replication_status` table. It can be called from a trigger, of course.

Peter - great challange!

Shlomi
-------------

DROP TABLE IF EXISTS uuid_test;
DROP TABLE IF EXISTS replication_status;
CREATE TABLE uuid_test (uuid VARCHAR(12) NOT NULL);
CREATE TABLE replication_status (whoami VARCHAR(12) NOT NULL);

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`deduce_whoami` $$
CREATE PROCEDURE `test`.`deduce_whoami` ()
BEGIN
  TRUNCATE TABLE uuid_test;
  TRUNCATE TABLE replication_status;
  SELECT UUID() INTO @current_uuid;
  INSERT INTO uuid_test SELECT SUBSTRING_INDEX(UUID(),&#039;-&#039;,-1) FROM DUAL;
  INSERT INTO uuid_test VALUES(SUBSTRING_INDEX(@current_uuid,&#039;-&#039;,-1)) ;

  INSERT INTO replication_status SELECT
    CASE COUNT(DISTINCT(uuid))
      WHEN &#039;1&#039; THEN &#039;master&#039;
      WHEN &#039;2&#039; THEN &#039;slave&#039;
    END
    FROM uuid_test;
END $$

DELIMITER ;

call deduce_whoami();</description>
		<content:encoded><![CDATA[<p>Solution:<br />
Use the UUID function twice. First time, use it as a function which does not replicate well. Second time, allow it to replicate as it were on the master.<br />
Compare the last part of both results: that&#8217;s the host part. If both are equal, we are on the master. If different &#8211; we are on the slave.<br />
The following stored procedure writes &#8216;master&#8217; or &#8217;slave&#8217; in the `replication_status` table. It can be called from a trigger, of course.</p>
<p>Peter &#8211; great challange!</p>
<p>Shlomi<br />
&#8212;&#8212;&#8212;&#8212;-</p>
<p>DROP TABLE IF EXISTS uuid_test;<br />
DROP TABLE IF EXISTS replication_status;<br />
CREATE TABLE uuid_test (uuid VARCHAR(12) NOT NULL);<br />
CREATE TABLE replication_status (whoami VARCHAR(12) NOT NULL);</p>
<p>DELIMITER $$</p>
<p>DROP PROCEDURE IF EXISTS `test`.`deduce_whoami` $$<br />
CREATE PROCEDURE `test`.`deduce_whoami` ()<br />
BEGIN<br />
  TRUNCATE TABLE uuid_test;<br />
  TRUNCATE TABLE replication_status;<br />
  SELECT UUID() INTO @current_uuid;<br />
  INSERT INTO uuid_test SELECT SUBSTRING_INDEX(UUID(),&#8217;-',-1) FROM DUAL;<br />
  INSERT INTO uuid_test VALUES(SUBSTRING_INDEX(@current_uuid,&#8217;-',-1)) ;</p>
<p>  INSERT INTO replication_status SELECT<br />
    CASE COUNT(DISTINCT(uuid))<br />
      WHEN &#8216;1&#8242; THEN &#8216;master&#8217;<br />
      WHEN &#8216;2&#8242; THEN &#8217;slave&#8217;<br />
    END<br />
    FROM uuid_test;<br />
END $$</p>
<p>DELIMITER ;</p>
<p>call deduce_whoami();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Edwards</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-395118</link>
		<dc:creator>Dave Edwards</dc:creator>
		<pubDate>Fri, 28 Nov 2008 21:41:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-395118</guid>
		<description>&lt;a href=&quot;http://www.pythian.com/blogs/1408/log-buffer-125-a-carnival-of-the-vanities-for-dbas&quot; rel=&quot;nofollow&quot;&gt;Peter Zaitsev of the MySQL Performance Blog has a little contest too—a Thanksgiving challenge: How to detect replication context. (But Thanksgiving was more than a month ago!)...&lt;/a&gt; -- &lt;a href=&quot;http://www.pythian.com/blogs/1408/log-buffer-125-a-carnival-of-the-vanities-for-dbas&quot; rel=&quot;nofollow&quot;&gt;Log Buffer #125&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p><a href="http://www.pythian.com/blogs/1408/log-buffer-125-a-carnival-of-the-vanities-for-dbas" rel="nofollow">Peter Zaitsev of the MySQL Performance Blog has a little contest too—a Thanksgiving challenge: How to detect replication context. (But Thanksgiving was more than a month ago!)&#8230;</a> &#8212; <a href="http://www.pythian.com/blogs/1408/log-buffer-125-a-carnival-of-the-vanities-for-dbas" rel="nofollow">Log Buffer #125</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-393798</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Thu, 27 Nov 2008 19:23:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-393798</guid>
		<description>James,

True you can do the join to the processlist table in 5.1 or 5.0-percona  though this is a bit overkill to do for single row update statements I think :)

I showed example with broken CURRENT_USER()  USER() works   just &quot;strange&quot; on 5.0.67 - it will show &quot;empty&quot; for a while for replication thread but as soon as I insert any row directly it switches for root@localhost  (the guy who did insert.</description>
		<content:encoded><![CDATA[<p>James,</p>
<p>True you can do the join to the processlist table in 5.1 or 5.0-percona  though this is a bit overkill to do for single row update statements I think <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I showed example with broken CURRENT_USER()  USER() works   just &#8220;strange&#8221; on 5.0.67 &#8211; it will show &#8220;empty&#8221; for a while for replication thread but as soon as I insert any row directly it switches for root@localhost  (the guy who did insert.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-393773</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Thu, 27 Nov 2008 18:20:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-393773</guid>
		<description>Isotopp,

This is what I tried (in 5.0)  however it does not seems to work well with triggers:

  CREATE TRIGGER topic_update After UPDATE ON topic
    FOR Each Row INSERT INTO topic_log (ts, connection_id, usr, operation,pk)
    VALUES (NOW(), CONNECTION_ID(), CURRENT_USER(), &#039;UPDATE&#039;, OLD.id);

mysql&gt; select * from topic_log;
+---------------------+---------------+----------------+-----------+---------+
&#124; ts                  &#124; connection_id &#124; usr            &#124; operation &#124; pk      &#124;
+---------------------+---------------+----------------+-----------+---------+
&#124; 2008-11-27 13:17:41 &#124;     253944746 &#124; root@localhost &#124; UPDATE    &#124; 4160633 &#124;
&#124; 2008-11-27 13:17:41 &#124;     253944746 &#124; root@localhost &#124; UPDATE    &#124; 5394938 &#124;
&#124; 2008-11-27 13:17:41 &#124;     253944746 &#124; root@localhost &#124; UPDATE    &#124; 4628914 &#124;
&#124; 2008-11-27 13:17:41 &#124;     253944746 &#124; root@localhost &#124; UPDATE    &#124; 5399979 &#124;
&#124; 2008-11-27 13:17:41 &#124;     253944746 &#124; root@localhost &#124; UPDATE    &#124; 4136666 &#124;
&#124; 2008-11-27 13:17:41 &#124;     253944746 &#124; root@localhost &#124; UPDATE    &#124; 4749235 &#124;

So it shows root@localhost rather than system user which was used in this case.</description>
		<content:encoded><![CDATA[<p>Isotopp,</p>
<p>This is what I tried (in 5.0)  however it does not seems to work well with triggers:</p>
<p>  CREATE TRIGGER topic_update After UPDATE ON topic<br />
    FOR Each Row INSERT INTO topic_log (ts, connection_id, usr, operation,pk)<br />
    VALUES (NOW(), CONNECTION_ID(), CURRENT_USER(), &#8216;UPDATE&#8217;, OLD.id);</p>
<p>mysql> select * from topic_log;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<br />
| ts                  | connection_id | usr            | operation | pk      |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<br />
| 2008-11-27 13:17:41 |     253944746 | root@localhost | UPDATE    | 4160633 |<br />
| 2008-11-27 13:17:41 |     253944746 | root@localhost | UPDATE    | 5394938 |<br />
| 2008-11-27 13:17:41 |     253944746 | root@localhost | UPDATE    | 4628914 |<br />
| 2008-11-27 13:17:41 |     253944746 | root@localhost | UPDATE    | 5399979 |<br />
| 2008-11-27 13:17:41 |     253944746 | root@localhost | UPDATE    | 4136666 |<br />
| 2008-11-27 13:17:41 |     253944746 | root@localhost | UPDATE    | 4749235 |</p>
<p>So it shows root@localhost rather than system user which was used in this case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Cohen</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-393553</link>
		<dc:creator>James Cohen</dc:creator>
		<pubDate>Thu, 27 Nov 2008 13:22:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-393553</guid>
		<description>As others have said you could use the contents of the USER() function&#039;s return value to see if it&#039;s the system user.

Another way would be to rowcount `SHOW PROCESSLIST` or `information_schema`.`PROCESSLIST` 
WHERE Id=CONNECTION_ID() AND User=&quot;system user&quot;</description>
		<content:encoded><![CDATA[<p>As others have said you could use the contents of the USER() function&#8217;s return value to see if it&#8217;s the system user.</p>
<p>Another way would be to rowcount `SHOW PROCESSLIST` or `information_schema`.`PROCESSLIST`<br />
WHERE Id=CONNECTION_ID() AND User=&#8221;system user&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Isotopp</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-393356</link>
		<dc:creator>Isotopp</dc:creator>
		<pubDate>Thu, 27 Nov 2008 09:16:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-393356</guid>
		<description>The SQL Thread executes as system user, so looking at current_user/session_user will tell you if you are executed by the SQL Thread (on the slave) or a regular thread (on the master)</description>
		<content:encoded><![CDATA[<p>The SQL Thread executes as system user, so looking at current_user/session_user will tell you if you are executed by the SQL Thread (on the slave) or a regular thread (on the master)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Noyes</title>
		<link>http://www.mysqlperformanceblog.com/2008/11/26/thanks-giving-challenge-how-to-detect-replication-context/comment-page-1/#comment-393155</link>
		<dc:creator>Scott Noyes</dc:creator>
		<pubDate>Thu, 27 Nov 2008 03:46:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/?p=546#comment-393155</guid>
		<description>If it&#039;s acceptable that the master and the replication thread on the slave do the same task, then in 5.1 you can put one trigger on the master, a different trigger on the slave, and use ROW binlog_format. The master and the replication thread will do whatever the master&#039;s trigger says; direct updates will do whatever the slave&#039;s trigger says.</description>
		<content:encoded><![CDATA[<p>If it&#8217;s acceptable that the master and the replication thread on the slave do the same task, then in 5.1 you can put one trigger on the master, a different trigger on the slave, and use ROW binlog_format. The master and the replication thread will do whatever the master&#8217;s trigger says; direct updates will do whatever the slave&#8217;s trigger says.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
