I was writing this in an email to a co-worker today, could possibly interest people in the outside world as well. It’s a good idea to look at the source at the same time as reading this :)
if (table_share->primary_key != MAX_KEY)
* Increase any auto_incremented primary key
THD *thd= table->in_use;
/* Ensure that handler is always called for auto_increment values */
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.
The handler function that we end up in is: ha_ndbcluster::get_auto_increment().
From here we end up inside NDB to do the actual work (not in the table handler).
Looking inside storage/ndb/src/ndbapi/Ndb.cpp at the method:
which really just calls Ndb::getTupleIdFromNdb(Uint32,Uint32)
which 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’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).
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.