[MDEV-33261] aut_increment field cannot be use unles it's first field in primary key Created: 2024-01-16  Updated: 2024-01-17

Status: Needs Feedback
Project: MariaDB Server
Component/s: Storage Engine - InnoDB
Fix Version/s: None

Type: Task Priority: Major
Reporter: Philip orleans Assignee: Marko Mäkelä
Resolution: Unresolved Votes: 0
Labels: None


 Description   

I thought, this was allowed

CREATE TABLE test (
  day date NOT NULL DEFAULT CURDATE(),
  fileid int(11) UNSIGNED NOT NULL DEFAULT 0,
  lineid int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (day, fileid, lineid)
)

the above create table statement fails, and the only one that works is

CREATE TABLE test (
  day date NOT NULL DEFAULT CURDATE(),
  fileid int(11) UNSIGNED NOT NULL DEFAULT 0,
  lineid int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (lineid,day, fileid)
)

Is there a workaround?
my version is 10.6.16-MariaDB-1:10.6.16+maria~ubu2004 mariadb.org binary distribution



 Comments   
Comment by Marko Mäkelä [ 2024-01-17 ]

I don’t think that anything has changed recently about this. After MDEV-6076, the only reason why an AUTO_INCREMENT column needs to be specified first in some index (not necessarily in the PRIMARY KEY) is that in the native ALTER TABLE, commit_set_autoinc() invokes row_search_max_autoinc() due to compatibility:

			/* Mimic ALGORITHM=COPY in the following scenario:
 
			CREATE TABLE t (a SERIAL);
			INSERT INTO t SET a=100;
			ALTER TABLE t AUTO_INCREMENT = 1;
			INSERT INTO t SET a=NULL;
			SELECT * FROM t;
 
			By default, ALGORITHM=INPLACE would reset the
			sequence to 1, while after ALGORITHM=COPY, the
			last INSERT would use a value larger than 100.
 
			We could only search the tree to know current
			max counter in the table and compare. */

The implementation could be rewritten so that in case there is no usable index for quickly reading MAX(auto_increment_column), we will scan the entire table. Then we could remove the requirement to have a suitable index on the AUTO_INCREMENT column.

Can you please be more specific with the "until now, this was allowed"? With which version of MariaDB Server would your first example work?

Comment by Philip orleans [ 2024-01-17 ]

I cannot find out what version was allowing this.

Comment by Sergei Golubchik [ 2024-01-17 ]

It used to work, and it still does, in MyISAM and Aria. InnoDB doesn't seem to have the HA_AUTO_PART_KEY flag and that causes your ER_WRONG_AUTO_KEY error. As far as I can see, InnoDB never had it.

But what behavior do you actually need? Do you need it to have different values for all rows? Or you want it to have different values for all rows with the same day and fileid but start from 1 for every new day, for example?

Comment by Philip orleans [ 2024-01-17 ]

I checked with Oracle's version of Innodb and it does not work either.
My database does not use it, I just checked. I need it for a new version of the model.
The reason is: that the auto_increment field lacks any business meaning, while all my selects begin with "where day='XXX-XX-XX' and ..." If I could use "day" first in the primary key, I could avoid creating yet another index for "day", and save resources. I partitioned the table based on day (week)
PARTITION BY RANGE (to_days(`day`))
(PARTITION `p078` VALUES LESS THAN (739180) ENGINE = InnoDB,
so I need to have day in the primary key.
Can it be modified?

Comment by Sergei Golubchik [ 2024-01-17 ]

if you don't need specifically the prefix-autoincrement behavior, then perhaps you can create a primary key specifically for auto-increment, and a separate UNIQUE key for (day, fileid, lineid)

Comment by Philip orleans [ 2024-01-17 ]

I am doing exactly that, but I need to add a new index, unnecessarily, to the table. If Innodb would support the auto_increment field in any position within the primary key, it would more rational. I would save a unique index.

Generated at Thu Feb 08 10:37:36 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.