<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Toru Maesaka &#187; innodb</title>
	<atom:link href="http://torum.net/tag/innodb/feed/" rel="self" type="application/rss+xml" />
	<link>http://torum.net</link>
	<description>Hackaholic and a Web Addict based in Tokyo</description>
	<lastBuildDate>Tue, 28 Feb 2012 10:52:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Thoughts on tackling InnoDB&#8217;s auto increment</title>
		<link>http://torum.net/2009/04/tackling-innodb-auto-increment/</link>
		<comments>http://torum.net/2009/04/tackling-innodb-auto-increment/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 10:28:11 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://torum.net/?p=1237</guid>
		<description><![CDATA[As I mentioned in my previous entry, InnoDB has it&#8217;s own auto increment counter which it uses to generate the next value for the database kernel (as we call it in Drizzle). At Drizzle project, we came across a suspicion that InnoDB doesn&#8217;t increment it&#8217;s internal counter on row updates. So what can this mean [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in my <a href="http://torum.net/2009/04/understanding-autoinc-with-innodb/">previous entry</a>, <a href="http://www.innodb.com/innodb/">InnoDB</a> has it&#8217;s own auto increment counter which it uses to generate the next value for the database kernel (as we call it in <a href="https://launchpad.net/drizzle">Drizzle</a>). At Drizzle project, we came across a <a href="https://bugs.launchpad.net/drizzle/+bug/314570">suspicion</a> that InnoDB doesn&#8217;t increment it&#8217;s internal counter on row updates. So what can this mean to you as a database admin?</p>
<p>Well, consider this simple table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1 <span style="color: #66cc66;">&#40;</span>
    a INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
    val INT
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB;</pre></div></div>

<p>and the following statements:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> t1 <span style="color: #993333; font-weight: bold;">SET</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">WHERE</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We should now have a table with one row where the <a href="http://en.wikipedia.org/wiki/Primary_key">primary key</a> value is 4:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>So far so good! Now remember our hypothesis that InnoDB doesn&#8217;t temper with the internal counter on an update. This would mean that our next row should have a primary key value of 2 since the counter variable should still be at 1 from our initial INSERT statement:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">2</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> LAST_INSERT_ID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span> LAST_INSERT_ID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span>                <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As expected, we have a new row with a primary key value of 2. But hey, what happens if we keep inserting into this table? specifically twice (assuming that you haven&#8217;t changed the auto-increment-increment system variable).</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
ERROR <span style="color: #cc66cc;">1062</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23000</span><span style="color: #66cc66;">&#41;</span>: Duplicate entry <span style="color: #ff0000;">'4'</span> <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">'PRIMARY'</span></pre></div></div>

<p>Yep, a duplicate key error! This isn&#8217;t really surprising since the increment was resumed from a value less than what we had updated the first value to. I&#8217;m not even sure if this sort of operation would be done in production but it shows how this behavior can be dangerous.</p>
<p>Something to note here is that, even on the INSERT error that we just experienced, InnoDB will update the internal count because get_auto_increment() was called when processing the query. So, this means that the next INSERT query will work fine since we&#8217;ve now passed the maximum value:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.02</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">4</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Great! now we can hopefully keep incrementing happily.</p>
<p>I&#8217;ve confirmed the internal behavior of this example with <a href="http://www.gnu.org/software/gdb/">GDB</a> and also verified the same behavior in MySQL 5.1.30. Whether this behavior is inappropriate or not deserves a discussion of it&#8217;s own and you could of course, absorb this problem in the application layer but the important question here is, what is the right behavior for Drizzle?</p>
<p>Well, take a look at how <a href="http://en.wikipedia.org/wiki/MyISAM">MyISAM</a> handles the same set of queries:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t2 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> t2 <span style="color: #993333; font-weight: bold;">SET</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">WHERE</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
Rows matched: <span style="color: #cc66cc;">1</span>  Changed: <span style="color: #cc66cc;">1</span>  Warnings: <span style="color: #cc66cc;">0</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t2 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t2;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">2</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> last_insert_id<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span> last_insert_id<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span>                <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As you can see above, MyISAM will resume incrementing from the updated primary key value. To me this is the correct behavior that should be consistently provided to DBAs. So the next question is, how can we make InnoDB behave that way?</p>
<h3>Ideas on tackling this issue</h3>
<p>A simple solution would be to make InnoDB update the internal count on updates (e.g. ha_innobase::update_row) when it deals with with auto increment. This approach, however can end up being complicated (code wise) and worst of all, it can create a minor overhead to the update operation on InnoDB which may not be favorable for our target users.</p>
<p>So after briefly discussing this issue with <a href="http://mysql-ha.com/">Monty Taylor</a>, seems the appropriate fix for this is to leave ha_innobase::update_row() as it is (don&#8217;t increment the internal count) and handle this on INSERT. Specifically speaking, on a row that doesn&#8217;t have a user specified auto increment value.</p>
<p>This means that we can recalculate the auto increment value on duplication/collision since the user is expecting an auto-generated discrete value. Needless to say, an error <strong>must</strong> be thrown if a collision occurs on a user-specified value.</p>
<p>So yeah, I&#8217;m flying to the US in couple of days so this should be something fun to hack on amongst other work related tasks on my boring flight :)</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/04/tackling-innodb-auto-increment/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Understanding how auto increment works with InnoDB</title>
		<link>http://torum.net/2009/04/understanding-autoinc-with-innodb/</link>
		<comments>http://torum.net/2009/04/understanding-autoinc-with-innodb/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 11:22:39 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://torum.net/?p=1090</guid>
		<description><![CDATA[Lately I&#8217;ve been having lots of fun going through Drizzle and InnoDB&#8216;s sourcecode to get a grasp of how auto increment is processed internally. I think I now have a fairly good grasp of what&#8217;s going on so I&#8217;m writing this entry as a note for myself. I&#8217;m also hoping that this will be helpful [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been having lots of fun going through <a href="https://launchpad.net/drizzle">Drizzle</a> and <a href="http://www.innodb.com/innodb/">InnoDB</a>&#8216;s sourcecode to get a grasp of how auto increment is processed internally. I think I now have a fairly good grasp of what&#8217;s going on so I&#8217;m writing this entry as a note for myself. I&#8217;m also hoping that this will be helpful to those that are interested in this topic too.</p>
<p>So in MySQL and Drizzle, the storage engine (in this case InnoDB) is responsible for computing the auto increment value. Here&#8217;s an <strong>abbreviated</strong> execution path for a simple INSERT statement to a table with an auto increment column:</p>

<div class="wp_syntax"><div class="code"><pre class="null" style="font-family:monospace;">mysql_parse() -&gt; mysql_execute_command() -&gt; mysql_insert() -&gt;
write_record() -&gt; handler::ha_write_row() -&gt; ha_innobase::write_row() -&gt;
handler::update_auto_increment() -&gt; ha_innobase::get_auto_increment()</pre></div></div>

<p>As you may have noticed, &#8220;handler&#8221; is an <a href="http://en.wikipedia.org/wiki/Abstract_class">abstract class</a> so you can apply this execution path to other engines too. For example, if you look inside <a href="http://en.wikipedia.org/wiki/MyISAM">MyISAM</a> there is a <em>get_auto_increment()</em> in there too. </p>
<h3>InnoDB&#8217;s internal counter</h3>
<p>InnoDB keeps track of it&#8217;s own auto increment count (an unsigned 64bit integer) called &#8220;autoinc&#8221; inside a structure that represents a database table (dict_table_struct). As you would expect, this is what InnoDB uses to compute the auto increment value that the server will use for further processing.</p>
<h3>Diving in a little deeper</h3>
<p>As you can see in the execution path, <em>handler::update_auto_increment()</em> function asks InnoDB&#8217;s <em>get_auto_increment()</em> function for an incremented value. Looking slightly deeper, you will see that this function:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">innobase_get_autoinc<span style="color: #009900;">&#40;</span>uint64_t <span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>is called inside <em>ha_innobase::get_auto_increment()</em> which will attempt to obtain the autoinc lock for that table (look at <em>ha_innobase::innobase_lock_autoinc()</em>), then given that the lock was successfully obtained, <em>innobase_get_autoinc()</em> will set the provided uint64_t variable with the next auto increment value:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"> <span style="color: #339933;">*</span>value <span style="color: #339933;">=</span> dict_table_autoinc_read<span style="color: #009900;">&#40;</span>prebuilt<span style="color: #339933;">-&gt;</span>table<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you look inside <em>dict_table_autoinc_read()</em> you will notice that it returns <em>table->autoinc</em> which is InnoDB&#8217;s internal auto increment counter described in the previous section. We don&#8217;t increment table->autoinc before returning it since this value is going to be updated after it is returned. In other words, the next auto increment value is precomputed. Needless to say, this critical region is still protected by the autoinc mutex so you don&#8217;t have to worry about updated values to clash.</p>
<h3>After obtaining the value from InnoDB</h3>
<p>Once we return from <em>ha_innobase::get_auto_increment()</em>, the core server can now store the returned 64bit integer as binary to the next_number_field object for further processing.</p>
<p>If you wish to see the value of this data, you can print it as a primitive type (uint64_t in this case) by obtaining it with the val_int() function. So if you were <a href="http://drizzle.org/wiki/Debugging_Drizzle_with_GDB">going through the code with gdb</a>, performing this at an appropriate time (e.g. after the autoinc value is stored) will print the value that will be used to update the auto increment column of your table:</p>

<div class="wp_syntax"><div class="code"><pre class="gdb" style="font-family:monospace;">(gdb) print table-&gt;next_number_field-&gt;val_int()</pre></div></div>

<p>Finally at the end of <em>handler::update_auto_increment()</em>, the next auto increment value is computed so that the storage engine won&#8217;t have to do any work in case the request is a multi row statement (note that this is different to InnoDB&#8217;s autoinc precomputation). We then return back to <em>ha_innobase::write_row()</em>, where the row is actually inserted into the table with <em>row_insert_for_mysql()</em>.</p>
<p>The rest of the flow is what you would expect: log what just happened, cleanup the mess caused in generating the autoinc value, and respond to the client.</p>
<h3>Final words</h3>
<p>So, this is all I have to cover in this entry. Admittedly my explanation is really abbreviated and perhaps a little over simplified but I figured this is enough to get back on the sourcecode after I forget my findings from the past couple of days in the future (my memory sucks).</p>
<p>I haven&#8217;t covered the auto increment internals for UPDATE statements but covering this would make this entry longer than I want it to be so I will save it for another day. Nevertheless, this entry will hopefully help curious individuals like myself to stare at Drizzle and InnoDB&#8217;s source code and enjoy an hour or two there.</p>
<p>Oh yea, and as a warning, do note that this entry could be outdated by the time you read it!</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/04/understanding-autoinc-with-innodb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

