{"id":694,"date":"2006-05-10T13:03:54","date_gmt":"2006-05-10T03:03:54","guid":{"rendered":"http:\/\/www.flamingspork.com\/blog\/2006\/05\/10\/how-auto_increment-is-implemented-in-ndb\/"},"modified":"2009-02-21T13:55:29","modified_gmt":"2009-02-21T03:55:29","slug":"how-auto_increment-is-implemented-in-ndb","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2006\/05\/10\/how-auto_increment-is-implemented-in-ndb\/","title":{"rendered":"How auto_increment is implemented in NDB"},"content":{"rendered":"<p>I was writing this in an email to a co-worker today, could possibly interest people in the outside world as well. It&#8217;s a good idea to look at the source at the same time as reading this :)<\/p>\n<p>In ha_ndbcluster::write_row(byte*),<\/p>\n<p><code>if (table_share->primary_key != MAX_KEY)<br \/>\n{<br \/>\n\/*<br \/>\n* Increase any auto_incremented primary key<br \/>\n*\/<br \/>\nif (has_auto_increment)<br \/>\n{<br \/>\nTHD *thd= table->in_use;<\/code><\/p>\n<p><code>m_skip_auto_increment= FALSE;<br \/>\nupdate_auto_increment();<br \/>\n\/* Ensure that handler is always called for auto_increment values *\/<br \/>\nthd->next_insert_id= 0;<br \/>\nm_skip_auto_increment= !auto_increment_column_changed;<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>We set next_insert_id to 0 so that in handler::update_auto_increment() we end calling the handler and never doing it just inside the server.<\/p>\n<p>The handler function that we end up in is: ha_ndbcluster::get_auto_increment().<\/p>\n<p>From here we end up inside NDB to do the actual work (not in the table handler).<\/p>\n<p>Looking inside storage\/ndb\/src\/ndbapi\/Ndb.cpp at the method:<\/p>\n<p>Ndb::getAutoIncrementValue(NdbDictionary::Table*,Uint32)<\/p>\n<p>which really just calls Ndb::getTupleIdFromNdb(Uint32,Uint32)<br \/>\nwhich either returns a cached value, or goes off and does a call to NDB to get either 1 auto increment value or the full cacheSize we&#8217;ve requested (which is worked out in ha_ndbcluster::get_auto_increment()). This increment is done in the interestingly named Ndb::opTupleIdOnNdb(Uint32 aTableId, Uint64 opValue, Uint32 op) (with op=0).<\/p>\n<p>This increments an entry in the SYSTAB_0 table inside the sys database in NDB. The row with SYSKEY_0 equal to the table id keeps the auto increment value. You can watch this by using a tool such as ndb_select_all on this table (grepping for the table id which you found with ndb_show_tables) while inserting rows into a table with an auto_increment value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was writing this in an email to a co-worker today, could possibly interest people in the outside world as well. It&#8217;s a good idea to look at the source at the same time as reading this :) In ha_ndbcluster::write_row(byte*), &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2006\/05\/10\/how-auto_increment-is-implemented-in-ndb\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[14],"tags":[140,628,54],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-auto_increment","tag-mysql","tag-ndb"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-bc","jetpack-related-posts":[{"id":606,"url":"https:\/\/www.flamingspork.com\/blog\/2006\/03\/02\/adding-an-information_schema-table\/","url_meta":{"origin":694,"position":0},"title":"Adding an INFORMATION_SCHEMA table","author":"Stewart Smith","date":"2006-03-02","format":false,"excerpt":"I (relatively) recently added the FILES table to INFORMATION_SCHEMA (check out the patch). This was so that we could provide an SQL interface to check how much free space their was in ndb disk data files. You can see how to use this in my post entitled INFORMATION_SCHEMA.FILES (querying disk\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":478,"url":"https:\/\/www.flamingspork.com\/blog\/2005\/10\/03\/a-funky-thing-done-last-week\/","url_meta":{"origin":694,"position":1},"title":"a funky thing done last week&#8230;","author":"Stewart Smith","date":"2005-10-03","format":false,"excerpt":"still have to talk to people about standards for this sort of thing and all that. But as a first checkin - funkyness++! mysql> select * from INFORMATION_SCHEMA.DATAFILES; select * from INFORMATION_SCHEMA.TABLESPACES; Empty set (0.03 sec) Empty set (0.00 sec) mysql> CREATE TABLESPACE ts1 ADD DATAFILE 'datafile.dat' USE LOGFILE GROUP\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":914,"url":"https:\/\/www.flamingspork.com\/blog\/2007\/11\/09\/mysql-5122-stew2\/","url_meta":{"origin":694,"position":2},"title":"mysql-5.1.22-stew2","author":"Stewart Smith","date":"2007-11-09","format":false,"excerpt":"New: Updated NDB Compressed LCP and BACKUP patches (now with O_DIRECT support) InnoDB patch for Windows that should give ~5x improvement on commits\/sec (Bug31876) Everything in current telco-6.3 tree (ndb ~6.3.5) Lots of NDB improvements and new features over regular 5.1. WL3686 Remove read before update WL2680 NDB Batched Update\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":847,"url":"https:\/\/www.flamingspork.com\/blog\/2007\/06\/19\/my-top-5-wishlist-for-mysql\/","url_meta":{"origin":694,"position":3},"title":"My Top 5 Wishlist for MySQL","author":"Stewart Smith","date":"2007-06-19","format":false,"excerpt":"I'm going and stealing Jay's idea (who stole it off Brian Duff... but his was for Oracle so obviously doesn't count :) So, my five wishes for MySQL Are: 5. Six-monthly release cycles Getting a release out there takes way too long. There's a variety of reasons, but seeing the\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":800,"url":"https:\/\/www.flamingspork.com\/blog\/2007\/03\/10\/code-size-of-an-engine-versus-test-suite\/","url_meta":{"origin":694,"position":4},"title":"Code size of an engine versus test suite","author":"Stewart Smith","date":"2007-03-10","format":false,"excerpt":"If you count the lines of code in the MySQL Cluster (NDB) test suite (mysql-5.1\/storage\/ndb\/test - and exclude the old ODBC stuff) you come up with about 104000 lines of code. This is in contrast to the approximate other 350,000 lines of code for the NDB engine (excluding the handler,\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1229,"url":"https:\/\/www.flamingspork.com\/blog\/2008\/10\/14\/mysql-cluster-ndb-on-win32-progress\/","url_meta":{"origin":694,"position":5},"title":"MySQL Cluster (NDB) on Win32 progress","author":"Stewart Smith","date":"2008-10-14","format":false,"excerpt":"Many things have been happenning in the land of NDB on Win32 as of late. I've fixed about 700 compiler warnings (some of which were real bugs) leaving about 161 to go on Win32 (VS2003). We're getting a few more warnings on Win64 (some of which look merely semantic, while\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.flamingspork.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":1,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":1548,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions\/1548"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}