-
Notifications
You must be signed in to change notification settings - Fork 4.1k
sql/opt/exec: perform implicit SELECT FOR UPDATE on UPDATE with index join #45511
Copy link
Copy link
Closed
Closed
Copy link
Labels
A-sql-optimizerSQL logical planning and optimizations.SQL logical planning and optimizations.C-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
Description
We should be able to use implicit row-level locking on UPDATEs with an index-join in its initial row scan like the following, which perform no gateway-side filtering:
--- Schema
CREATE TABLE t (
id INT8 NOT NULL DEFAULT unique_rowid(),
instance_id INT8 NULL,
is_active INT8 NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
INDEX idx (instance_id ASC, is_active ASC)
);
--- Update
EXPLAIN UPDATE t SET is_active = 0, updated_at = now() WHERE (is_active = 1) AND (instance_id = 15);
tree | field | description
----------------------------+-------------+------------------------
| distributed | false
| vectorized | false
count | |
└── update | |
│ | table | t
│ | set | is_active, updated_at
│ | strategy | updater
│ | auto commit |
└── render | |
└── index-join | |
│ | table | t@primary
│ | key columns | id
└── scan | |
| table | t@idx
| spans | /15/1-/15/2Currently, we only match on the following patterns:
cockroach/pkg/sql/opt/exec/execbuilder/mutation.go
Lines 793 to 812 in 9c72e72
| // Try to match the pattern: | |
| // | |
| // (Update | |
| // $input:(Scan $scanPrivate:*) | |
| // $checks:* | |
| // $mutationPrivate:* | |
| // ) | |
| // | |
| // Or | |
| // | |
| // (Update | |
| // $input:(Project | |
| // (Scan $scanPrivate:*) | |
| // $projections:* | |
| // $passthrough:* | |
| // ) | |
| // $checks:* | |
| // $mutationPrivate:* | |
| // ) | |
| // |
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-sql-optimizerSQL logical planning and optimizations.SQL logical planning and optimizations.C-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)