The statement ALTER DATABASE db_name SET TIFLASH REPLICA count; (supported in versions after 6.0) is equivalent to TiDB executing a series of DDL operations to add TiFlash replicas for the tables in that database. This statement will only add TiFlash replicas to all current tables in the database, and the behavior described in the post is as expected.
This is essentially equivalent to batch execution of the operation to add TiFlash by table, saving the manual effort of adding each table individually. Manually adding TiFlash to new tables in the future would also require re-executing the add TiFlash operation. Perhaps this operation of adding TiFlash to the database can be recorded somewhere, so that when new tables are added in the future, it can check whether this operation exists for the database, and if so, perform the add operation for the new table.
From the execution of the command to build TiFlash replicas by database until all tables in the database are synchronized, it is not recommended to execute TiFlash replica count settings or other DDL operations related to the database, otherwise, the final state may be unexpected. Unexpected scenarios include:
Setting the TiFlash replica count to 2 first, and then setting the TiFlash replica count to 1 before all tables in the database are synchronized, cannot guarantee that the final TiFlash replica count of all tables will be either 1 or 2.
During the execution of the command, if a table is created under the database, TiFlash replicas may be created for these newly added tables.
During the execution of the command, if an index is added to a table under the database, the command may be stuck waiting until the index is added.
The command to build TiFlash replicas by database will skip system tables, views, temporary tables, and tables containing character sets not supported by TiFlash.
In actual operations, the above tips should be considered. Specifically, during the execution of the command to build TiFlash replicas by database, if tables are created under the target database, TiFlash replicas may be created for these newly added tables, which is uncertain; after all synchronization is completed, creating tables will definitely create TiFlash replicas for these new tables. (It has been verified that only in the case of create table like, TiFlash replicas are automatically created.)
According to test results, if the DDL has not finished executing in the background when building replicas by database, the results are unpredictable. To check if it has finished executing, use:
SELECT * FROM INFORMATION_SCHEMA.TIFLASH_REPLICA WHERE TABLE_SCHEMA = 'TESTDB';
The reason is that the SQL statement alter database xxx set tiflash replica xx; does not guarantee atomicity in its internal execution process. It completes the work through multiple DDLs that set TiFlash table replicas, so it is uncertain whether the corresponding DDL has been executed for newly added tables during the SQL execution process. Additionally, after the SQL execution is completed, TiFlash replicas will not be automatically set for subsequently added tables.
I tested it again. After creating the database, if you execute the operation create table test1 like test;, you can see the newly created table using SELECT * FROM INFORMATION_SCHEMA.TIFLASH_REPLICA. However, if you directly create a table, you cannot see it. I just checked the documentation, and it clearly states that create table like will create a TiFlash replica. So, it is confirmed that after building by database, newly created tables will not automatically create TiFlash replicas.
Is this DDL operation a PD scheduling task? Besides checking the completion status of the TiFlash replica (SELECT * FROM INFORMATION_SCHEMA.TIFLASH_REPLICA WHERE TABLE_SCHEMA = ‘TESTDB’;), where else can we monitor whether it has been completed?
Yes, it can only be done for tables under the current schema. This behavior is quite strange; shouldn’t the create table like command automatically create a TiFlash replica?
The automatic creation of replicas with “create table like” is specifically mentioned in the official documentation, and this is how it currently works.