MySQL vs openGauss: Why Failed Transactions Block DDL in One but Not the Other
The article demonstrates how a failed transaction without explicit rollback in MySQL holds metadata locks, blocking subsequent DDL operations like partition additions, while openGauss automatically rolls back aborted transactions and releases locks, preventing such blocking; it provides reproduction steps, lock analysis, and five mitigation recommendations.
Reading font
MySQL Failure Simulation
1.1 Create Partitioned Table and Insert Data
Create a partitioned table tt1 with two daily partitions:
CREATE TABLE tt1 (
id int NOT NULL,
sdate date NOT NULL,
c1 varchar(4) NOT NULL,
PRIMARY KEY (id, sdate)
)
PARTITION BY RANGE columns(sdate) (
PARTITION p20240524 VALUES LESS THAN ('2024-05-25'),
PARTITION p20240525 VALUES LESS THAN ('2024-05-26')
);1.2 Explicitly Start Transaction and Insert Data
mysql> begin;
mysql> select * from tango.tt1;
+----+------------+-----+
| id | sdate | c1 |
+----+------------+-----+
| 1 | 2024-05-25 | aaa |
+----+------------+-----+
1 row in set (0.00 sec)
insert into tt1 values(1,'2024-05-25','aaa');
mysql> insert into tt1 values(3,'2024-05-27','ccc');
ERROR 1526 (HY000): Table has no partition for value from column_listThe database reports that the partition for the inserted value does not exist.
1.3 Check Table Locks and Transaction Status
mysql> select * from performance_schema.metadata_locks where object_name='tt1';
+-------------+---------------+-------------+-------------+-----------------------+--------------+---------------+-------------+-------------------+-----------------+----------------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | COLUMN_NAME | OBJECT_INSTANCE_BEGIN | LOCK_TYPE | LOCK_DURATION | LOCK_STATUS | SOURCE | OWNER_THREAD_ID | OWNER_EVENT_ID |
+-------------+---------------+-------------+-------------+-----------------------+--------------+---------------+-------------+-------------------+-----------------+----------------+
| TABLE | tango | tt1 | NULL | 140712994313232 | SHARED_READ | TRANSACTION | GRANTED | sql_parse.cc:5768 | 85 | 24 |
| TABLE | tango | tt1 | NULL | 140712994947616 | SHARED_WRITE | TRANSACTION | GRANTED | sql_parse.cc:5768 | 85 | 25 |
+-------------+---------------+-------------+-------------+-----------------------+--------------+---------------+-------------+-------------------+-----------------+----------------+
2 rows in set (0.00 sec)The table holds SHARED_READ and SHARED_WRITE locks that are not released after the statement error. This is MySQL's kernel mechanism: after a transaction error, the database does not automatically roll back; the application must decide to commit or roll back.
1.4 Another Session Executes ADD PARTITION DDL
mysql> ALTER table tt1 ADD PARTITION ( PARTITION p20240526 VALUES LESS THAN ('2024-05-27') );The DDL hangs. Checking metadata locks shows a pending EXCLUSIVE lock:
mysql> select * from performance_schema.metadata_locks where object_name='tt1';
+-------------+---------------+-------------+-------------+-----------------------+-------------------+---------------+-------------+-------------------+-----------------+----------------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | COLUMN_NAME | OBJECT_INSTANCE_BEGIN | LOCK_TYPE | LOCK_DURATION | LOCK_STATUS | SOURCE | OWNER_THREAD_ID | OWNER_EVENT_ID |
+-------------+---------------+-------------+-------------+-----------------------+-------------------+---------------+-------------+-------------------+-----------------+----------------+
| TABLE | tango | tt1 | NULL | 140712801139968 | SHARED_READ | TRANSACTION | GRANTED | sql_parse.cc:5768 | 123 | 21 |
| TABLE | tango | tt1 | NULL | 140712793308528 | SHARED_WRITE | TRANSACTION | GRANTED | sql_parse.cc:5768 | 123 | 22 |
| TABLE | tango | tt1 | NULL | 140712926580592 | SHARED_UPGRADABLE | TRANSACTION | GRANTED | sql_parse.cc:5768 | 121 | 20 |
| TABLE | tango | tt1 | NULL | 140712928177104 | EXCLUSIVE | TRANSACTION | PENDING | mdl.cc:3753 | 121 | 20 |
+-------------+---------------+-------------+-------------+-----------------------+-------------------+---------------+-------------+-------------------+-----------------+----------------+
5 rows in set (0.00 sec)The pending EXCLUSIVE lock belongs to the ALTER TABLE ... ADD PARTITION statement (thread 121, event 20).
1.5 Impact on New Business Operations
A simple SELECT * FROM tango.tt1 also hangs, showing a pending SHARED_READ lock. The blocked DDL prevents subsequent business access to the table until the DDL times out. MySQL controls DDL wait timeout via lock_wait_timeout (default 31536000s). Production systems typically set 30–60s, core systems 5s, but during the blocking window new business is affected.
1.6 Issues Identified in This Scenario
Application did not handle the transaction error and did not explicitly commit or roll back.
Partition pre-creation and monitoring are missing: partitioned tables need scheduled partition creation and alerts for insufficient partitions.
Metadata lock wait timeout ( lock_wait_timeout) set too high (e.g., 600s) prolongs the impact; a reasonable range (5–60s) is necessary.
MySQL-ecosystem databases (TiDB, GoldenDB) exhibit similar behavior. Oracle and PostgreSQL behave differently; the article verifies with openGauss.
openGauss Failure Simulation
2.1 Create Partitioned Table in openGauss
gsql -d postgres -p 5432
openGauss=# create database tango;
tango=# CREATE TABLE tt1 (
id int NOT NULL,
sdate date NOT NULL,
c1 varchar(4) NOT NULL
)
PARTITION BY RANGE(sdate) (
PARTITION p20240524 VALUES LESS THAN ('2024-05-25'),
PARTITION p20240525 VALUES LESS THAN ('2024-05-26')
);2.2 Start Transaction and Insert Data
tango=# begin;
BEGIN
tango=# select * from tt1;
id | sdate | c1
----+---------------------+-----
1 | 2024-05-25 00:00:00 | aaa
(1 row)
tango=# insert into tt1 values(3,'2024-05-28','ccc');
ERROR: inserted partition key does not map to any table partition2.3 Another Session Executes ADD PARTITION
tango=# ALTER table tt1 ADD PARTITION p20240526 VALUES LESS THAN ('2024-05-27');
ALTER TABLEThe partition addition succeeds immediately .
2.4 Check Locks and Transaction Status
tango=# SELECT l.locktype,l.database,l.relation::regclass,l.page,l.tuple,l.virtualxid,l.transactionid,l.classid,l.objid,l.objsubid,l.pid,l.mode,l.granted FROM pg_locks l JOIN pg_class c ON l.relation = c.oid WHERE c.relname = 'tt1';
locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | pid | mode | granted
----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+-----------------+-----------------+---------
relation | 16384 | tt1 | | | | | | | | 140405684233984 | AccessShareLock | t
(1 row)tango=# SELECT datname,pid,sessionid,usename,application_name,backend_start,xact_start,query_start,state,query FROM pg_stat_activity where datname='tango';
datname | pid | sessionid | usename | application_name | backend_start | xact_start | query_start | state | query
---------+-----------------+-----------+---------+------------------+-------------------------------+-------------------------------+-------------------------------+-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------
tango | 140405684233984 | 8 | opgauss | gsql | 2024-05-26 15:45:47.008274+08 | 2024-05-26 15:47:40.481015+08 | 2024-05-26 15:47:45.822262+08 | idle in transaction | select * from tt1;After the insert error, the transaction state becomes idle in transaction (aborted) and the table locks disappear:
tango=# SELECT l.locktype,l.database,l.relation::regclass,l.page,l.tuple,l.virtualxid,l.transactionid,l.classid,l.objid,l.objsubid,l.pid,l.mode,l.granted FROM pg_locks l JOIN pg_class c ON l.relation = c.oid WHERE c.relname = 'tt1';
locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | pid | mode | granted
----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+-----+------+---------
(0 rows)
tango=# SELECT datname,pid,sessionid,usename,application_name,backend_start,xact_start,query_start,state,query FROM pg_stat_activity where datname='tango';
datname | pid | sessionid | usename | application_name | backend_start | xact_start | query_start | state | query
---------+-----------------+-----------+---------+------------------+-------------------------------+-------------------------------+-------------------------------+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------
tango | 140405684233984 | 8 | opgauss | gsql | 2024-05-26 15:45:47.008274+08 | | 2024-05-26 15:49:09.048895+08 | idle in transaction (aborted) | insert into tt1 values(3,'2024-05-28','ccc');openGauss automatically rolls back the failed transaction and releases all locks, allowing the DDL to proceed. Oracle and PostgreSQL show the same behavior. The issue surfaced when migrating from Oracle to MySQL without accounting for this difference.
Summary of Optimizations
Application code must explicitly handle transaction errors and issue COMMIT or ROLLBACK.
Set a reasonable lock_wait_timeout (5–60s) at the database level.
Implement partition pre-creation and monitoring for partitioned tables.
Decouple DDL operations from the main business flow; run DDLs during maintenance windows.
Thoroughly validate business scenarios during heterogeneous database migrations.
Reference:
https://docs-opengauss.osinfra.cn/zh/docs/5.0.0-liteFor questions about attribution, corrections or content rights, please contactand we will review it promptly.