Skip to content

sql/opt/exec: perform implicit SELECT FOR UPDATE on UPDATE with index join #45511

@nvb

Description

@nvb

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/2

Currently, we only match on the following patterns:

// Try to match the pattern:
//
// (Update
// $input:(Scan $scanPrivate:*)
// $checks:*
// $mutationPrivate:*
// )
//
// Or
//
// (Update
// $input:(Project
// (Scan $scanPrivate:*)
// $projections:*
// $passthrough:*
// )
// $checks:*
// $mutationPrivate:*
// )
//

Metadata

Metadata

Assignees

Labels

A-sql-optimizerSQL logical planning and optimizations.C-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions