-
Notifications
You must be signed in to change notification settings - Fork 4.1k
opt: inline constant values for foreign key checks #63882
Description
As described in #63735, we cannot compute constant values for computed columns when planning foreign key checks, since the foreign key checks scan the mutation buffer rather than using the values directly from an INSERT, UPDATE, or UPSERT statement. For example, consider the following (simplified) schema:
CREATE TABLE history (
rowid UUID NOT NULL DEFAULT gen_random_uuid(),
h_c_id INT8 NOT NULL,
h_c_d_id INT8 NOT NULL,
h_c_w_id INT8 NOT NULL,
h_d_id INT8 NOT NULL,
h_w_id INT8 NOT NULL,
h_date TIMESTAMP NULL,
h_amount DECIMAL(6,2) NULL,
h_data VARCHAR(24) NULL,
crdb_region crdb_internal_region NOT VISIBLE NOT NULL AS (CASE WHEN h_w_id BETWEEN 0 AND 3 THEN 'us-east1' WHEN h_w_id BETWEEN 4 AND 6 THEN 'us-west1' WHEN h_w_id BETWEEN 7 AND 9 THEN 'us-central1' END) STORED,
CONSTRAINT "primary" PRIMARY KEY (h_w_id ASC, rowid ASC),
CONSTRAINT fk_h_c_w_id_ref_customer FOREIGN KEY (h_c_w_id, h_c_d_id, h_c_id) REFERENCES customer(c_w_id, c_d_id, c_id),
CONSTRAINT fk_h_w_id_ref_district FOREIGN KEY (h_w_id, h_d_id) REFERENCES district(d_w_id, d_id)
) LOCALITY REGIONAL BY ROW;
The referenced tables customer and district have a computed column crdb_region with the same definition. Running a query like:
INSERT INTO history (h_c_id, h_c_d_id, h_c_w_id, h_d_id, h_w_id, h_amount, h_date, h_data) VALUES (2057, 4, 3, 4, 3, 2100.9, '2021-04-15 15:22:14', 'test data');
should allow the foreign key checks to determine that crdb_region is 'us-east1' and avoid communication with remote regions, but this is currently not possible. This is currently somewhat mitigated by locality optimized anti joins, but ideally we wouldn't need to rely on any sort of locality optimized search when crdb_region is computed.
Jira issue: CRDB-6809
Metadata
Metadata
Assignees
Labels
Type
Projects
Status