Bug #121950 GR applier misses parent/child dependency when FK string columns differ in length (PAD SPACE), secondary fails with 1452

Submitted: 26 Nov 21:17
Reporter: Vinicius Malvestio Grippa (OCA) Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Group Replication Severity:S2 (Serious)
Version:8.4.13, 8.0.46, 26.10.0.1 OS:Any
Assigned to: CPU Architecture:Any
Tags: 1452, foreign key, parallel applier, writeset

[26 Nov 21:17] Vinicius Malvestio Grippa
Description:
A Group Replication secondary stops with ER_NO_REFERENCED_ROW_2 (1452) on a child
row insert, although the parent row exists on both members. STOP/START
GROUP_REPLICATION brings the member back with no data change.

Seen on a production InnoDB Cluster 8.4.13: single primary,
replica_parallel_workers=4, replica_preserve_commit_order=ON, parent PK
VARCHAR(100), child FK VARCHAR(256), utf8mb3_general_ci.

The certifier never makes the child insert depend on the parent insert. For PAD
SPACE collations, add_pke() hashes a sort key padded to the column's own length,
so the child's FK hash (padded to 256) differs from the parent's PK hash (padded
to 100) for the same value. The child transaction gets a last_committed lower than
the parent's sequence_number, another worker runs it before the parent row is
written, and the FK check fails. 1452 is not retried, so the member leaves the
group.

Code (mysql-26.10.0.1, same logic in 8.0.46 and 8.4.13):
- sql/rpl_write_set_handler.cc:1158  FK hash sized with the child field's pack_length
- sql/rpl_write_set_handler.cc:1001  key hash sized with the parent field
- sql/field.cc:6674  Field_varstring::make_sort_key pads with MY_STRXFRM_PAD_TO_MAXLEN

Async replication is safe because the binlog writeset tracker falls back to
COMMIT_ORDER for transactions on FK parent tables (has_related_foreign_keys,
set at rpl_write_set_handler.cc:1190). The GR certifier doesn't check that flag.

Affected when the lengths differ: utf8mb3_general_ci, utf8mb4_general_ci,
utf8mb4_unicode_ci, utf8mb4_bin, latin1_swedish_ci. Not affected: equal
lengths, NO PAD collations (utf8mb4_0900_ai_ci), integer keys. A parent delete
can likewise run before the child delete that precedes it (1451).
restrict_fk_on_non_standard_key accepts this schema.

Reproduced on 8.0.46, 8.4.13 and trunk 26.10.0.1.

Related: Bug #97836 (same symptom; a 2023 comment there has this shape with
latin1), Bug #112120 (asks DDL to reject mismatched FK lengths).

How to repeat:
Two members, single primary, replica_parallel_workers=4.

On the primary:
CREATE TABLE p (k VARCHAR(100) PRIMARY KEY) CHARSET utf8mb3 COLLATE utf8mb3_general_ci;
CREATE TABLE c (id INT PRIMARY KEY, k VARCHAR(256),
  FOREIGN KEY (k) REFERENCES p (k)) CHARSET utf8mb3 COLLATE utf8mb3_general_ci;
CREATE TABLE blocker (id INT PRIMARY KEY, v INT);
INSERT INTO blocker VALUES (1, 0);

1) Missing dependency. On the primary:
INSERT INTO p VALUES ('a');
INSERT INTO c VALUES (1, 'a');
On the secondary, read the group_replication_applier relay log with mysqlbinlog.
The child's last_committed is lower than the parent's sequence_number. With
c.k VARCHAR(100), or with utf8mb4_0900_ai_ci, it equals it.

2) The 1452. On the secondary:
LOCK TABLES blocker READ;
On the primary:
BEGIN; UPDATE blocker SET v = v + 1 WHERE id = 1; INSERT INTO p VALUES ('b'); COMMIT;
INSERT INTO c VALUES (2, 'b');
The secondary applies the child while the parent waits on the table lock, fails
with 1452 and leaves the group. Expected: the child waits for the parent.
LOCK TABLES is used because the GR applier kills local transactions that hold
row locks.

The attached MTR test (group_replication.gr_parallel_applier_indexes_fk_pad_space)
covers both. On trunk it fails the same way in 3 of 3 runs.

Suggested fix:
For PAD SPACE string columns, emit an extra writeset hash built from the value
with trailing spaces trimmed and no padding. Do it for FK hashes and for the
unique-key hashes of tables referenced by foreign keys. Adding a hash instead of
changing the existing one keeps mixed-version groups safe, as in the fix for
Bug#35404584.

Broader option: have the GR certifier treat transactions on FK parent tables as
sequential, as the binlog tracker does. That also covers FKs on non-unique keys
(Bug #109923), at the cost of parallelism.

Workarounds: equal column lengths, a NO PAD collation, or
replica_parallel_workers=1.
[26 Nov 21:19] Vinicius Malvestio Grippa
MTR test reproducing the missing dependency and the 1452 (unzip at source root). Fails on trunk 26.10.0.1 in 3 of 3 runs.

Attachment: gr_parallel_applier_indexes_fk_pad_space.zip (application/zip, text), 5.66 KiB.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.055 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.