# LogicTest: multiregion-9node-3region-3azs !metamorphic-batch-sizes
# TODO(#75864): enable multiregion-9node-3region-3azs-tenant

statement ok
SET CLUSTER SETTING kv.closed_timestamp.side_transport_interval = '10ms';

statement ok
SET CLUSTER SETTING kv.closed_timestamp.target_duration = '10ms';

statement ok
SET CLUSTER SETTING kv.rangefeed.closed_timestamp_refresh_interval = '10ms';

statement ok
CREATE DATABASE multi_region_test_db PRIMARY REGION "ca-central-1" REGIONS "ap-southeast-2", "us-east-1" SURVIVE REGION FAILURE

statement ok
USE multi_region_test_db

# Make sure foreign key references checked is planned
subtest test_fk_constraint

statement ok
CREATE TABLE t_parent (
  id INT PRIMARY KEY USING HASH
) LOCALITY REGIONAL BY ROW;

statement ok
CREATE TABLE t_child (
  id INT PRIMARY KEY,
  pid INT REFERENCES t_parent (id),
  FAMILY fam_0 (id, pid)
);

statement ok
CREATE TABLE t_child_regional (
  id INT PRIMARY KEY,
  pid INT REFERENCES t_parent (id),
  FAMILY fam_0 (id, pid)
) LOCALITY REGIONAL BY ROW;

query T retry
EXPLAIN (VERBOSE) INSERT INTO t_child VALUES (123, 321)
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • insert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_child(id, pid)
│   │
│   └── • buffer
│       │ columns: (column1, column2)
│       │ label: buffer 1
│       │
│       └── • values
│             columns: (column1, column2)
│             size: 2 columns, 1 row
│             row 0, expr 0: 123
│             row 0, expr 1: 321
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (pid)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_id_shard_16_eq, pid)
                │ estimated row count: 0 (missing stats)
                │ table: t_parent@t_parent_pkey
                │ equality cols are key
                │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (pid = id)
                │
                └── • lookup join (anti)
                    │ columns: (crdb_internal_id_shard_16_eq, pid)
                    │ estimated row count: 1 (missing stats)
                    │ table: t_parent@t_parent_pkey
                    │ equality cols are key
                    │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (pid = id)
                    │
                    └── • render
                        │ columns: (crdb_internal_id_shard_16_eq, pid)
                        │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
                        │ render pid: column2
                        │
                        └── • project
                            │ columns: (column2)
                            │
                            └── • scan buffer
                                  columns: (column1, column2)
                                  estimated row count: 1
                                  label: buffer 1

query T
EXPLAIN (VERBOSE) INSERT INTO t_child_regional VALUES (123, 321)
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • insert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_child_regional(id, pid, crdb_region)
│   │
│   └── • buffer
│       │ columns: (column1, column2, crdb_region_default, check1)
│       │ label: buffer 1
│       │
│       └── • values
│             columns: (column1, column2, crdb_region_default, check1)
│             size: 4 columns, 1 row
│             row 0, expr 0: 123
│             row 0, expr 1: 321
│             row 0, expr 2: 'ap-southeast-2'
│             row 0, expr 3: true
│
├── • constraint-check
│   │
│   └── • error if rows
│       │ columns: ()
│       │
│       └── • project
│           │ columns: (id)
│           │
│           └── • cross join (inner)
│               │ columns: (id, id, crdb_region)
│               │ estimated row count: 1 (missing stats)
│               │
│               ├── • values
│               │     columns: (id)
│               │     size: 1 column, 1 row
│               │     row 0, expr 0: 123
│               │
│               └── • scan
│                     columns: (id, crdb_region)
│                     estimated row count: 1 (missing stats)
│                     table: t_child_regional@t_child_regional_pkey
│                     spans: /"\x80"/123/0 /"\xc0"/123/0
│                     limit: 1
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (pid)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_id_shard_16_eq, pid)
                │ estimated row count: 0 (missing stats)
                │ table: t_parent@t_parent_pkey
                │ equality cols are key
                │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (pid = id)
                │
                └── • lookup join (anti)
                    │ columns: (crdb_internal_id_shard_16_eq, pid)
                    │ estimated row count: 1 (missing stats)
                    │ table: t_parent@t_parent_pkey
                    │ equality cols are key
                    │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (pid = id)
                    │
                    └── • render
                        │ columns: (crdb_internal_id_shard_16_eq, pid)
                        │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
                        │ render pid: column2
                        │
                        └── • project
                            │ columns: (column2)
                            │
                            └── • scan buffer
                                  columns: (column1, column2, crdb_region_default, check1)
                                  estimated row count: 1
                                  label: buffer 1

subtest test_uniqueness_check_uuid

# Make sure uniqueness check is omitted with gen_random_uuid().
statement ok
CREATE TABLE t_gen_random_uuid (
  user_id UUID DEFAULT gen_random_uuid() PRIMARY KEY USING HASH,
  val STRING NOT NULL,
  FAMILY fam_0 (user_id, val)
) LOCALITY REGIONAL BY ROW;

query T
EXPLAIN (VERBOSE) INSERT INTO t_gen_random_uuid (val) VALUES (4321);
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_gen_random_uuid(crdb_internal_user_id_shard_16, user_id, val, crdb_region)
│ auto commit
│
└── • render
    │ columns: (crdb_internal_user_id_shard_16_comp, user_id_default, val_cast, crdb_region_default, check1, check2)
    │ render check1: crdb_internal_user_id_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render val_cast: val_cast
    │ render user_id_default: user_id_default
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_user_id_shard_16_comp: crdb_internal_user_id_shard_16_comp
    │
    └── • render
        │ columns: (crdb_internal_user_id_shard_16_comp, val_cast, user_id_default, crdb_region_default)
        │ render crdb_internal_user_id_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(user_id_default))), 16)
        │ render val_cast: val_cast
        │ render user_id_default: user_id_default
        │ render crdb_region_default: crdb_region_default
        │
        └── • values
              columns: (val_cast, user_id_default, crdb_region_default)
              size: 3 columns, 1 row
              row 0, expr 0: '4321'
              row 0, expr 1: gen_random_uuid()
              row 0, expr 2: 'ap-southeast-2'

query T
EXPLAIN (VERBOSE) INSERT INTO t_gen_random_uuid (val) VALUES (4321) ON CONFLICT DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_gen_random_uuid(crdb_internal_user_id_shard_16, user_id, val, crdb_region)
│ auto commit
│ arbiter constraints: t_gen_random_uuid_pkey
│
└── • render
    │ columns: (crdb_internal_user_id_shard_16_comp, user_id_default, val_cast, crdb_region_default, check1, check2)
    │ render check1: crdb_internal_user_id_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render val_cast: val_cast
    │ render user_id_default: user_id_default
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_user_id_shard_16_comp: crdb_internal_user_id_shard_16_comp
    │
    └── • project
        │ columns: (val_cast, user_id_default, crdb_region_default, crdb_internal_user_id_shard_16_comp)
        │
        └── • lookup join (anti)
            │ columns: (crdb_internal_user_id_shard_16_eq, crdb_internal_user_id_shard_16_comp, val_cast, user_id_default, crdb_region_default)
            │ estimated row count: 0 (missing stats)
            │ table: t_gen_random_uuid@t_gen_random_uuid_pkey
            │ equality cols are key
            │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_user_id_shard_16_eq = crdb_internal_user_id_shard_16)) AND (user_id_default = user_id)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_user_id_shard_16_eq, crdb_internal_user_id_shard_16_comp, val_cast, user_id_default, crdb_region_default)
                │ estimated row count: 1 (missing stats)
                │ table: t_gen_random_uuid@t_gen_random_uuid_pkey
                │ equality cols are key
                │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_user_id_shard_16_eq = crdb_internal_user_id_shard_16)) AND (user_id_default = user_id)
                │
                └── • render
                    │ columns: (crdb_internal_user_id_shard_16_eq, crdb_internal_user_id_shard_16_comp, val_cast, user_id_default, crdb_region_default)
                    │ render crdb_internal_user_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(user_id_default))), 16)
                    │ render crdb_internal_user_id_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(user_id_default))), 16)
                    │ render val_cast: val_cast
                    │ render user_id_default: user_id_default
                    │ render crdb_region_default: crdb_region_default
                    │
                    └── • values
                          columns: (val_cast, user_id_default, crdb_region_default)
                          size: 3 columns, 1 row
                          row 0, expr 0: '4321'
                          row 0, expr 1: gen_random_uuid()
                          row 0, expr 2: 'ap-southeast-2'

query T
EXPLAIN (VERBOSE) INSERT INTO t_gen_random_uuid (val) VALUES (4321), (8765) ON CONFLICT DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_gen_random_uuid(crdb_internal_user_id_shard_16, user_id, val, crdb_region)
│ auto commit
│ arbiter constraints: t_gen_random_uuid_pkey
│
└── • render
    │ columns: (crdb_internal_user_id_shard_16_comp, user_id_default, val_cast, crdb_region_default, check1, check2)
    │ render check1: crdb_internal_user_id_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render val_cast: val_cast
    │ render user_id_default: user_id_default
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_user_id_shard_16_comp: crdb_internal_user_id_shard_16_comp
    │
    └── • distinct
        │ columns: (val_cast, user_id_default, crdb_region_default, crdb_internal_user_id_shard_16_comp)
        │ estimated row count: 0 (missing stats)
        │ distinct on: user_id_default
        │ nulls are distinct
        │
        └── • project
            │ columns: (val_cast, user_id_default, crdb_region_default, crdb_internal_user_id_shard_16_comp)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_user_id_shard_16_eq, crdb_internal_user_id_shard_16_comp, val_cast, user_id_default, crdb_region_default)
                │ estimated row count: 0 (missing stats)
                │ table: t_gen_random_uuid@t_gen_random_uuid_pkey
                │ equality cols are key
                │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_user_id_shard_16_eq = crdb_internal_user_id_shard_16)) AND (user_id_default = user_id)
                │
                └── • lookup join (anti)
                    │ columns: (crdb_internal_user_id_shard_16_eq, crdb_internal_user_id_shard_16_comp, val_cast, user_id_default, crdb_region_default)
                    │ estimated row count: 1 (missing stats)
                    │ table: t_gen_random_uuid@t_gen_random_uuid_pkey
                    │ equality cols are key
                    │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_user_id_shard_16_eq = crdb_internal_user_id_shard_16)) AND (user_id_default = user_id)
                    │
                    └── • render
                        │ columns: (crdb_internal_user_id_shard_16_eq, crdb_internal_user_id_shard_16_comp, val_cast, user_id_default, crdb_region_default)
                        │ render crdb_internal_user_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(user_id_default))), 16)
                        │ render crdb_internal_user_id_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(user_id_default))), 16)
                        │ render val_cast: val_cast
                        │ render user_id_default: user_id_default
                        │ render crdb_region_default: crdb_region_default
                        │
                        └── • render
                            │ columns: (user_id_default, crdb_region_default, val_cast)
                            │ render user_id_default: gen_random_uuid()
                            │ render crdb_region_default: 'ap-southeast-2'
                            │ render val_cast: val_cast
                            │
                            └── • values
                                  columns: (val_cast)
                                  size: 1 column, 2 rows
                                  row 0, expr 0: '4321'
                                  row 1, expr 0: '8765'

subtest test_uniqueness_check_pk

statement ok
CREATE TABLE t_unique_hash_pk (
  id INT PRIMARY KEY USING HASH
) LOCALITY REGIONAL BY ROW;

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_pk (id) VALUES (4321);
----
distribution: local
vectorized: true
·
• insert fast path
  columns: ()
  estimated row count: 0 (missing stats)
  into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
  auto commit
  uniqueness check: t_unique_hash_pk@t_unique_hash_pk_pkey
  size: 5 columns, 1 row
  row 0, expr 0: 11
  row 0, expr 1: 4321
  row 0, expr 2: 'ap-southeast-2'
  row 0, expr 3: true
  row 0, expr 4: true

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_pk (id) VALUES (4321) ON CONFLICT DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│ auto commit
│ arbiter constraints: t_unique_hash_pk_pkey
│
└── • render
    │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, check1, check2)
    │ render check1: crdb_internal_id_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
    │
    └── • cross join (anti)
        │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
        │ estimated row count: 0 (missing stats)
        │
        ├── • values
        │     columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
        │     size: 3 columns, 1 row
        │     row 0, expr 0: 4321
        │     row 0, expr 1: 'ap-southeast-2'
        │     row 0, expr 2: 11
        │
        └── • union all
            │ columns: (id)
            │ estimated row count: 1 (missing stats)
            │ limit: 1
            │
            ├── • scan
            │     columns: (id)
            │     estimated row count: 1 (missing stats)
            │     table: t_unique_hash_pk@t_unique_hash_pk_pkey
            │     spans: /"@"/11/4321/0
            │
            └── • scan
                  columns: (id)
                  estimated row count: 1 (missing stats)
                  table: t_unique_hash_pk@t_unique_hash_pk_pkey
                  spans: /"\x80"/11/4321/0 /"\xc0"/11/4321/0
                  parallel

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_pk (id) VALUES (4321), (8765) ON CONFLICT DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│ auto commit
│ arbiter constraints: t_unique_hash_pk_pkey
│
└── • render
    │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, check1, check2)
    │ render check1: crdb_internal_id_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
    │
    └── • project
        │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
        │
        └── • lookup join (anti)
            │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
            │ estimated row count: 0 (missing stats)
            │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
            │ equality cols are key
            │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
                │ estimated row count: 1 (missing stats)
                │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
                │ equality cols are key
                │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
                │
                └── • render
                    │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
                    │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                    │ render crdb_internal_id_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                    │ render crdb_region_default: 'ap-southeast-2'
                    │ render column1: column1
                    │
                    └── • values
                          columns: (column1)
                          size: 1 column, 2 rows
                          row 0, expr 0: 4321
                          row 1, expr 0: 8765

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_pk (id) VALUES (4321) ON CONFLICT (id) DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│ auto commit
│ arbiter constraints: t_unique_hash_pk_pkey
│
└── • render
    │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, check1, check2)
    │ render check1: crdb_internal_id_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
    │
    └── • cross join (anti)
        │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
        │ estimated row count: 0 (missing stats)
        │
        ├── • values
        │     columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
        │     size: 3 columns, 1 row
        │     row 0, expr 0: 4321
        │     row 0, expr 1: 'ap-southeast-2'
        │     row 0, expr 2: 11
        │
        └── • union all
            │ columns: (id)
            │ estimated row count: 1 (missing stats)
            │ limit: 1
            │
            ├── • scan
            │     columns: (id)
            │     estimated row count: 1 (missing stats)
            │     table: t_unique_hash_pk@t_unique_hash_pk_pkey
            │     spans: /"@"/11/4321/0
            │
            └── • scan
                  columns: (id)
                  estimated row count: 1 (missing stats)
                  table: t_unique_hash_pk@t_unique_hash_pk_pkey
                  spans: /"\x80"/11/4321/0 /"\xc0"/11/4321/0
                  parallel

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_pk (id) VALUES (4321), (8765) ON CONFLICT (id) DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│ auto commit
│ arbiter constraints: t_unique_hash_pk_pkey
│
└── • render
    │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, check1, check2)
    │ render check1: crdb_internal_id_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
    │
    └── • project
        │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
        │
        └── • lookup join (anti)
            │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
            │ estimated row count: 0 (missing stats)
            │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
            │ equality cols are key
            │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
                │ estimated row count: 1 (missing stats)
                │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
                │ equality cols are key
                │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
                │
                └── • render
                    │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
                    │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                    │ render crdb_internal_id_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                    │ render crdb_region_default: 'ap-southeast-2'
                    │ render column1: column1
                    │
                    └── • values
                          columns: (column1)
                          size: 1 column, 2 rows
                          row 0, expr 0: 4321
                          row 1, expr 0: 8765

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_pk (id) VALUES (4321) ON CONFLICT (id) DO UPDATE SET id = excluded.id
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • upsert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│   │ arbiter constraints: t_unique_hash_pk_pkey
│   │
│   └── • buffer
│       │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, column1, crdb_region, check1, check2, upsert_crdb_region)
│       │ label: buffer 1
│       │
│       └── • project
│           │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, column1, crdb_region, check1, check2, upsert_crdb_region)
│           │
│           └── • render
│               │ columns: (check1, check2, column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region, upsert_crdb_region)
│               │ render check1: crdb_internal_id_shard_16_comp IN (0, 1, __more10_100__, 15)
│               │ render check2: upsert_crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│               │ render column1: column1
│               │ render crdb_region_default: crdb_region_default
│               │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
│               │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16
│               │ render id: id
│               │ render crdb_region: crdb_region
│               │ render upsert_crdb_region: upsert_crdb_region
│               │
│               └── • render
│                   │ columns: (upsert_crdb_region, column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region)
│                   │ render upsert_crdb_region: CASE WHEN crdb_region IS NULL THEN crdb_region_default ELSE crdb_region END
│                   │ render column1: column1
│                   │ render crdb_region_default: crdb_region_default
│                   │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
│                   │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16
│                   │ render id: id
│                   │ render crdb_region: crdb_region
│                   │
│                   └── • cross join (left outer)
│                       │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region)
│                       │ estimated row count: 1 (missing stats)
│                       │
│                       ├── • values
│                       │     columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
│                       │     size: 3 columns, 1 row
│                       │     row 0, expr 0: 4321
│                       │     row 0, expr 1: 'ap-southeast-2'
│                       │     row 0, expr 2: 11
│                       │
│                       └── • union all
│                           │ columns: (crdb_internal_id_shard_16, id, crdb_region)
│                           │ estimated row count: 1 (missing stats)
│                           │ limit: 1
│                           │
│                           ├── • scan
│                           │     columns: (crdb_internal_id_shard_16, id, crdb_region)
│                           │     estimated row count: 1 (missing stats)
│                           │     table: t_unique_hash_pk@t_unique_hash_pk_pkey
│                           │     spans: /"@"/11/4321/0
│                           │
│                           └── • scan
│                                 columns: (crdb_internal_id_shard_16, id, crdb_region)
│                                 estimated row count: 1 (missing stats)
│                                 table: t_unique_hash_pk@t_unique_hash_pk_pkey
│                                 spans: /"\x80"/11/4321/0 /"\xc0"/11/4321/0
│                                 parallel
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (id)
            │
            └── • project
                │ columns: (crdb_internal_id_shard_16, id, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16, id, crdb_region)
                    │ estimated row count: 0 (missing stats)
                    │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (id = id)
                    │ pred: (crdb_internal_id_shard_16 != crdb_internal_id_shard_16) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16, id, crdb_region)
                        │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                        │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16_comp
                        │ render id: column1
                        │ render crdb_region: upsert_crdb_region
                        │
                        └── • project
                            │ columns: (crdb_internal_id_shard_16_comp, column1, upsert_crdb_region)
                            │
                            └── • scan buffer
                                  columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, column1, crdb_region, check1, check2, upsert_crdb_region)
                                  estimated row count: 1 (missing stats)
                                  label: buffer 1

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_pk (id) VALUES (4321), (8765) ON CONFLICT (id) DO UPDATE SET id = excluded.id
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • upsert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│   │ arbiter constraints: t_unique_hash_pk_pkey
│   │
│   └── • buffer
│       │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, column1, crdb_region, check1, check2, upsert_crdb_region)
│       │ label: buffer 1
│       │
│       └── • project
│           │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, column1, crdb_region, check1, check2, upsert_crdb_region)
│           │
│           └── • render
│               │ columns: (check1, check2, column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region, upsert_crdb_region)
│               │ render check1: crdb_internal_id_shard_16_comp IN (0, 1, __more10_100__, 15)
│               │ render check2: upsert_crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│               │ render column1: column1
│               │ render crdb_region_default: crdb_region_default
│               │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
│               │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16
│               │ render id: id
│               │ render crdb_region: crdb_region
│               │ render upsert_crdb_region: upsert_crdb_region
│               │
│               └── • render
│                   │ columns: (upsert_crdb_region, column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region)
│                   │ render upsert_crdb_region: CASE WHEN crdb_region IS NULL THEN crdb_region_default ELSE crdb_region END
│                   │ render column1: column1
│                   │ render crdb_region_default: crdb_region_default
│                   │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
│                   │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16
│                   │ render id: id
│                   │ render crdb_region: crdb_region
│                   │
│                   └── • project
│                       │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region)
│                       │
│                       └── • lookup join (left outer)
│                           │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1, crdb_internal_id_shard_16, id, crdb_region)
│                           │ estimated row count: 2 (missing stats)
│                           │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
│                           │ equality cols are key
│                           │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
│                           │ remote lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
│                           │ locking strength: for update
│                           │
│                           └── • render
│                               │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
│                               │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
│                               │ render crdb_internal_id_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
│                               │ render crdb_region_default: 'ap-southeast-2'
│                               │ render column1: column1
│                               │
│                               └── • values
│                                     columns: (column1)
│                                     size: 1 column, 2 rows
│                                     row 0, expr 0: 4321
│                                     row 1, expr 0: 8765
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (id)
            │
            └── • project
                │ columns: (crdb_internal_id_shard_16, id, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16, id, crdb_region)
                    │ estimated row count: 1 (missing stats)
                    │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (id = id)
                    │ pred: (crdb_internal_id_shard_16 != crdb_internal_id_shard_16) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16, id, crdb_region)
                        │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                        │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16_comp
                        │ render id: column1
                        │ render crdb_region: upsert_crdb_region
                        │
                        └── • project
                            │ columns: (crdb_internal_id_shard_16_comp, column1, upsert_crdb_region)
                            │
                            └── • scan buffer
                                  columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, column1, crdb_region, check1, check2, upsert_crdb_region)
                                  estimated row count: 2 (missing stats)
                                  label: buffer 1

query T
EXPLAIN (VERBOSE) UPSERT INTO t_unique_hash_pk (id) VALUES (4321);
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│ auto commit
│ arbiter constraints: t_unique_hash_pk_pkey
│
└── • render
    │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_region, check1, check2)
    │ render check1: CASE WHEN crdb_region IS NULL THEN crdb_internal_id_shard_16_comp ELSE crdb_internal_id_shard_16 END IN (0, 1, __more10_100__, 15)
    │ render check2: CASE WHEN crdb_region IS NULL THEN crdb_region_default ELSE crdb_region END IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
    │ render crdb_region: crdb_region
    │
    └── • cross join (left outer)
        │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region)
        │ estimated row count: 1 (missing stats)
        │
        ├── • values
        │     columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp)
        │     size: 3 columns, 1 row
        │     row 0, expr 0: 4321
        │     row 0, expr 1: 'ap-southeast-2'
        │     row 0, expr 2: 11
        │
        └── • union all
            │ columns: (crdb_internal_id_shard_16, id, crdb_region)
            │ estimated row count: 1 (missing stats)
            │ limit: 1
            │
            ├── • scan
            │     columns: (crdb_internal_id_shard_16, id, crdb_region)
            │     estimated row count: 1 (missing stats)
            │     table: t_unique_hash_pk@t_unique_hash_pk_pkey
            │     spans: /"@"/11/4321/0
            │
            └── • scan
                  columns: (crdb_internal_id_shard_16, id, crdb_region)
                  estimated row count: 1 (missing stats)
                  table: t_unique_hash_pk@t_unique_hash_pk_pkey
                  spans: /"\x80"/11/4321/0 /"\xc0"/11/4321/0
                  parallel

query T
EXPLAIN (VERBOSE) UPSERT INTO t_unique_hash_pk (id) VALUES (4321), (8765);
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_pk(crdb_internal_id_shard_16, id, crdb_region)
│ auto commit
│ arbiter constraints: t_unique_hash_pk_pkey
│
└── • render
    │ columns: (crdb_internal_id_shard_16_comp, column1, crdb_region_default, crdb_region, check1, check2)
    │ render check1: CASE WHEN crdb_region IS NULL THEN crdb_internal_id_shard_16_comp ELSE crdb_internal_id_shard_16 END IN (0, 1, __more10_100__, 15)
    │ render check2: CASE WHEN crdb_region IS NULL THEN crdb_region_default ELSE crdb_region END IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_id_shard_16_comp: crdb_internal_id_shard_16_comp
    │ render crdb_region: crdb_region
    │
    └── • project
        │ columns: (column1, crdb_region_default, crdb_internal_id_shard_16_comp, crdb_internal_id_shard_16, id, crdb_region)
        │
        └── • lookup join (left outer)
            │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1, crdb_internal_id_shard_16, id, crdb_region)
            │ estimated row count: 2 (missing stats)
            │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
            │ equality cols are key
            │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
            │ remote lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (column1 = id)
            │ locking strength: for update
            │
            └── • render
                │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16_comp, crdb_region_default, column1)
                │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                │ render crdb_internal_id_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column1))), 16)
                │ render crdb_region_default: 'ap-southeast-2'
                │ render column1: column1
                │
                └── • values
                      columns: (column1)
                      size: 1 column, 2 rows
                      row 0, expr 0: 4321
                      row 1, expr 0: 8765

query T
EXPLAIN (VERBOSE) UPDATE t_unique_hash_pk SET id = 1234 WHERE id = 4321;
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • update
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ table: t_unique_hash_pk
│   │ set: crdb_internal_id_shard_16, id
│   │
│   └── • buffer
│       │ columns: (crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, id_new, check1)
│       │ label: buffer 1
│       │
│       └── • render
│           │ columns: (crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, id_new, check1)
│           │ render check1: true
│           │ render crdb_internal_id_shard_16_comp: 4
│           │ render id_new: 1234
│           │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16
│           │ render id: id
│           │ render crdb_region: crdb_region
│           │
│           └── • union all
│               │ columns: (crdb_internal_id_shard_16, id, crdb_region)
│               │ estimated row count: 1 (missing stats)
│               │ limit: 1
│               │
│               ├── • scan
│               │     columns: (crdb_internal_id_shard_16, id, crdb_region)
│               │     estimated row count: 1 (missing stats)
│               │     table: t_unique_hash_pk@t_unique_hash_pk_pkey
│               │     spans: /"@"/11/4321/0
│               │
│               └── • scan
│                     columns: (crdb_internal_id_shard_16, id, crdb_region)
│                     estimated row count: 1 (missing stats)
│                     table: t_unique_hash_pk@t_unique_hash_pk_pkey
│                     spans: /"\x80"/11/4321/0 /"\xc0"/11/4321/0
│                     parallel
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (id)
            │
            └── • project
                │ columns: (crdb_internal_id_shard_16, id, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16, id, crdb_region)
                    │ estimated row count: 0 (missing stats)
                    │ table: t_unique_hash_pk@t_unique_hash_pk_pkey
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_id_shard_16_eq = crdb_internal_id_shard_16)) AND (id = id)
                    │ pred: (crdb_internal_id_shard_16 != crdb_internal_id_shard_16) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_id_shard_16_eq, crdb_internal_id_shard_16, id, crdb_region)
                        │ render crdb_internal_id_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(id_new))), 16)
                        │ render crdb_internal_id_shard_16: crdb_internal_id_shard_16_comp
                        │ render id: id_new
                        │ render crdb_region: crdb_region
                        │
                        └── • project
                            │ columns: (crdb_internal_id_shard_16_comp, id_new, crdb_region)
                            │
                            └── • scan buffer
                                  columns: (crdb_internal_id_shard_16, id, crdb_region, crdb_internal_id_shard_16_comp, id_new, check1)
                                  estimated row count: 1 (missing stats)
                                  label: buffer 1

subtest test_uniqueness_check_sec_key

statement ok
CREATE TABLE t_unique_hash_sec_key (
  id INT PRIMARY KEY,
  email STRING,
  FAMILY fam_0 (id, email)
) LOCALITY REGIONAL BY ROW;

statement ok
CREATE UNIQUE INDEX idx_uniq_hash_email ON t_unique_hash_sec_key (email) USING HASH;

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_sec_key (id, email) VALUES (4321, 'some_email');
----
distribution: local
vectorized: true
·
• insert fast path
  columns: ()
  estimated row count: 0 (missing stats)
  into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
  auto commit
  uniqueness check: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
  uniqueness check: t_unique_hash_sec_key@idx_uniq_hash_email
  size: 6 columns, 1 row
  row 0, expr 0: 4321
  row 0, expr 1: 'some_email'
  row 0, expr 2: 'ap-southeast-2'
  row 0, expr 3: 1
  row 0, expr 4: true
  row 0, expr 5: true

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_sec_key (id, email) VALUES (4321, 'some_email') ON CONFLICT DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│ auto commit
│ arbiter constraints: t_unique_hash_sec_key_pkey, idx_uniq_hash_email
│
└── • render
    │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, check1, check2)
    │ render check1: crdb_internal_email_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render column2: column2
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
    │
    └── • project
        │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
        │
        └── • lookup join (anti)
            │ columns: (crdb_internal_email_shard_16_eq, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
            │ estimated row count: 0 (missing stats)
            │ table: t_unique_hash_sec_key@idx_uniq_hash_email
            │ equality cols are key
            │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_email_shard_16_eq, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
                │ estimated row count: 0 (missing stats)
                │ table: t_unique_hash_sec_key@idx_uniq_hash_email
                │ equality cols are key
                │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
                │
                └── • render
                    │ columns: (crdb_internal_email_shard_16_eq, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
                    │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
                    │ render column1: column1
                    │ render column2: column2
                    │ render crdb_region_default: crdb_region_default
                    │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
                    │
                    └── • cross join (anti)
                        │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
                        │ estimated row count: 0 (missing stats)
                        │
                        ├── • values
                        │     columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
                        │     size: 4 columns, 1 row
                        │     row 0, expr 0: 4321
                        │     row 0, expr 1: 'some_email'
                        │     row 0, expr 2: 'ap-southeast-2'
                        │     row 0, expr 3: 1
                        │
                        └── • project
                            │ columns: ()
                            │
                            └── • union all
                                │ columns: (id)
                                │ estimated row count: 1 (missing stats)
                                │ limit: 1
                                │
                                ├── • scan
                                │     columns: (id)
                                │     estimated row count: 1 (missing stats)
                                │     table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
                                │     spans: /"@"/4321/0
                                │
                                └── • scan
                                      columns: (id)
                                      estimated row count: 1 (missing stats)
                                      table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
                                      spans: /"\x80"/4321/0 /"\xc0"/4321/0
                                      parallel

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_sec_key (id, email) VALUES (4321, 'some_email'), (8765, 'another_email') ON CONFLICT DO NOTHING;
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│ auto commit
│ arbiter constraints: t_unique_hash_sec_key_pkey, idx_uniq_hash_email
│
└── • render
    │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, check1, check2)
    │ render check1: crdb_internal_email_shard_16_comp IN (0, 1, __more10_100__, 15)
    │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
    │ render column1: column1
    │ render column2: column2
    │ render crdb_region_default: crdb_region_default
    │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
    │
    └── • project
        │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
        │
        └── • lookup join (anti)
            │ columns: (crdb_internal_email_shard_16_eq, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
            │ estimated row count: 0 (missing stats)
            │ table: t_unique_hash_sec_key@idx_uniq_hash_email
            │ equality cols are key
            │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
            │
            └── • lookup join (anti)
                │ columns: (crdb_internal_email_shard_16_eq, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
                │ estimated row count: 0 (missing stats)
                │ table: t_unique_hash_sec_key@idx_uniq_hash_email
                │ equality cols are key
                │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
                │
                └── • render
                    │ columns: (crdb_internal_email_shard_16_eq, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
                    │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
                    │ render column1: column1
                    │ render column2: column2
                    │ render crdb_region_default: crdb_region_default
                    │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
                    │
                    └── • lookup join (anti)
                        │ columns: (crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
                        │ estimated row count: 0 (missing stats)
                        │ table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
                        │ equality cols are key
                        │ lookup condition: (crdb_region IN ('ca-central-1', 'us-east-1')) AND (column1 = id)
                        │
                        └── • lookup join (anti)
                            │ columns: (crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
                            │ estimated row count: 1 (missing stats)
                            │ table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
                            │ equality cols are key
                            │ lookup condition: (crdb_region = 'ap-southeast-2') AND (column1 = id)
                            │
                            └── • render
                                │ columns: (crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
                                │ render crdb_internal_email_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
                                │ render crdb_region_default: 'ap-southeast-2'
                                │ render column1: column1
                                │ render column2: column2
                                │
                                └── • values
                                      columns: (column1, column2)
                                      size: 2 columns, 2 rows
                                      row 0, expr 0: 4321
                                      row 0, expr 1: 'some_email'
                                      row 1, expr 0: 8765
                                      row 1, expr 1: 'another_email'

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_sec_key (id, email) VALUES (4321, 'some_email') ON CONFLICT (email) DO NOTHING;
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • insert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│   │ arbiter constraints: idx_uniq_hash_email
│   │
│   └── • render
│       │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, check1, check2)
│       │ render check1: crdb_internal_email_shard_16_comp IN (0, 1, __more10_100__, 15)
│       │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│       │ render column1: column1
│       │ render column2: column2
│       │ render crdb_region_default: crdb_region_default
│       │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│       │
│       └── • cross join (anti)
│           │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
│           │ estimated row count: 0 (missing stats)
│           │
│           ├── • values
│           │     columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
│           │     size: 4 columns, 1 row
│           │     row 0, expr 0: 4321
│           │     row 0, expr 1: 'some_email'
│           │     row 0, expr 2: 'ap-southeast-2'
│           │     row 0, expr 3: 1
│           │
│           └── • project
│               │ columns: ()
│               │
│               └── • union all
│                   │ columns: (email)
│                   │ estimated row count: 1 (missing stats)
│                   │ limit: 1
│                   │
│                   ├── • scan
│                   │     columns: (email)
│                   │     estimated row count: 1 (missing stats)
│                   │     table: t_unique_hash_sec_key@idx_uniq_hash_email
│                   │     spans: /"@"/1/"some_email"/0
│                   │
│                   └── • scan
│                         columns: (email)
│                         estimated row count: 1 (missing stats)
│                         table: t_unique_hash_sec_key@idx_uniq_hash_email
│                         spans: /"\x80"/1/"some_email"/0 /"\xc0"/1/"some_email"/0
│                         parallel
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (id)
            │
            └── • cross join (inner)
                │ columns: (id, id, crdb_region)
                │ estimated row count: 1 (missing stats)
                │
                ├── • values
                │     columns: (id)
                │     size: 1 column, 1 row
                │     row 0, expr 0: 4321
                │
                └── • scan
                      columns: (id, crdb_region)
                      estimated row count: 1 (missing stats)
                      table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
                      spans: /"\x80"/4321/0 /"\xc0"/4321/0
                      limit: 1

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_sec_key (id, email) VALUES (4321, 'some_email'), (8765, 'another_email') ON CONFLICT (email) DO NOTHING;
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • insert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│   │ arbiter constraints: idx_uniq_hash_email
│   │
│   └── • buffer
│       │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, check1, check2)
│       │ label: buffer 1
│       │
│       └── • render
│           │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, check1, check2)
│           │ render check1: crdb_internal_email_shard_16_comp IN (0, 1, __more10_100__, 15)
│           │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│           │ render column1: column1
│           │ render column2: column2
│           │ render crdb_region_default: crdb_region_default
│           │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│           │
│           └── • project
│               │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
│               │
│               └── • lookup join (anti)
│                   │ columns: (crdb_internal_email_shard_16_eq, crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
│                   │ estimated row count: 0 (missing stats)
│                   │ table: t_unique_hash_sec_key@idx_uniq_hash_email
│                   │ equality cols are key
│                   │ lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
│                   │
│                   └── • lookup join (anti)
│                       │ columns: (crdb_internal_email_shard_16_eq, crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
│                       │ estimated row count: 1 (missing stats)
│                       │ table: t_unique_hash_sec_key@idx_uniq_hash_email
│                       │ equality cols are key
│                       │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
│                       │
│                       └── • render
│                           │ columns: (crdb_internal_email_shard_16_eq, crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
│                           │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
│                           │ render crdb_internal_email_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
│                           │ render crdb_region_default: 'ap-southeast-2'
│                           │ render column1: column1
│                           │ render column2: column2
│                           │
│                           └── • values
│                                 columns: (column1, column2)
│                                 size: 2 columns, 2 rows
│                                 row 0, expr 0: 4321
│                                 row 0, expr 1: 'some_email'
│                                 row 1, expr 0: 8765
│                                 row 1, expr 1: 'another_email'
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (column1)
            │
            └── • lookup join (semi)
                │ columns: (column1, crdb_region_default)
                │ estimated row count: 0 (missing stats)
                │ table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
                │ lookup condition: (crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (column1 = id)
                │ pred: crdb_region_default != crdb_region
                │
                └── • project
                    │ columns: (column1, crdb_region_default)
                    │
                    └── • scan buffer
                          columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, check1, check2)
                          estimated row count: 0 (missing stats)
                          label: buffer 1

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_sec_key (id, email) VALUES (4321, 'some_email') ON CONFLICT (email) DO UPDATE SET email = 'bad_email';
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • upsert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│   │ arbiter constraints: idx_uniq_hash_email
│   │
│   └── • buffer
│       │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
│       │ label: buffer 1
│       │
│       └── • project
│           │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
│           │
│           └── • render
│               │ columns: (check1, check2, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_id, upsert_email, upsert_crdb_region, upsert_crdb_internal_email_shard_16)
│               │ render check1: upsert_crdb_internal_email_shard_16 IN (0, 1, __more10_100__, 15)
│               │ render check2: upsert_crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│               │ render column1: column1
│               │ render column2: column2
│               │ render crdb_region_default: crdb_region_default
│               │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│               │ render id: id
│               │ render email: email
│               │ render crdb_region: crdb_region
│               │ render crdb_internal_email_shard_16: crdb_internal_email_shard_16
│               │ render upsert_id: upsert_id
│               │ render upsert_email: upsert_email
│               │ render upsert_crdb_region: upsert_crdb_region
│               │ render upsert_crdb_internal_email_shard_16: upsert_crdb_internal_email_shard_16
│               │
│               └── • render
│                   │ columns: (upsert_id, upsert_email, upsert_crdb_region, upsert_crdb_internal_email_shard_16, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16)
│                   │ render upsert_id: CASE WHEN crdb_region IS NULL THEN column1 ELSE id END
│                   │ render upsert_email: CASE WHEN crdb_region IS NULL THEN column2 ELSE 'bad_email' END
│                   │ render upsert_crdb_region: CASE WHEN crdb_region IS NULL THEN crdb_region_default ELSE crdb_region END
│                   │ render upsert_crdb_internal_email_shard_16: CASE WHEN crdb_region IS NULL THEN crdb_internal_email_shard_16_comp ELSE 9 END
│                   │ render column1: column1
│                   │ render column2: column2
│                   │ render crdb_region_default: crdb_region_default
│                   │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│                   │ render id: id
│                   │ render email: email
│                   │ render crdb_region: crdb_region
│                   │ render crdb_internal_email_shard_16: crdb_internal_email_shard_16
│                   │
│                   └── • cross join (left outer)
│                       │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_internal_email_shard_16, id, email, crdb_region)
│                       │ estimated row count: 1 (missing stats)
│                       │
│                       ├── • values
│                       │     columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
│                       │     size: 4 columns, 1 row
│                       │     row 0, expr 0: 4321
│                       │     row 0, expr 1: 'some_email'
│                       │     row 0, expr 2: 'ap-southeast-2'
│                       │     row 0, expr 3: 1
│                       │
│                       └── • render
│                           │ columns: (crdb_internal_email_shard_16, id, email, crdb_region)
│                           │ render crdb_internal_email_shard_16: mod(fnv32(md5(crdb_internal.datums_to_bytes(email))), 16)
│                           │ render id: id
│                           │ render email: email
│                           │ render crdb_region: crdb_region
│                           │
│                           └── • union all
│                               │ columns: (id, email, crdb_region)
│                               │ estimated row count: 1 (missing stats)
│                               │ limit: 1
│                               │
│                               ├── • scan
│                               │     columns: (id, email, crdb_region)
│                               │     estimated row count: 1 (missing stats)
│                               │     table: t_unique_hash_sec_key@idx_uniq_hash_email
│                               │     spans: /"@"/1/"some_email"/0
│                               │
│                               └── • scan
│                                     columns: (id, email, crdb_region)
│                                     estimated row count: 1 (missing stats)
│                                     table: t_unique_hash_sec_key@idx_uniq_hash_email
│                                     spans: /"\x80"/1/"some_email"/0 /"\xc0"/1/"some_email"/0
│                                     parallel
│
├── • constraint-check
│   │
│   └── • error if rows
│       │ columns: ()
│       │
│       └── • project
│           │ columns: (upsert_id)
│           │
│           └── • lookup join (semi)
│               │ columns: (upsert_id, upsert_crdb_region)
│               │ estimated row count: 0 (missing stats)
│               │ table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
│               │ lookup condition: (crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (upsert_id = id)
│               │ pred: upsert_crdb_region != crdb_region
│               │
│               └── • project
│                   │ columns: (upsert_id, upsert_crdb_region)
│                   │
│                   └── • scan buffer
│                         columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
│                         estimated row count: 1 (missing stats)
│                         label: buffer 1
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (email)
            │
            └── • project
                │ columns: (id, email, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                    │ estimated row count: 0 (missing stats)
                    │ table: t_unique_hash_sec_key@idx_uniq_hash_email
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (email = email)
                    │ pred: (id != id) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                        │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(upsert_email))), 16)
                        │ render id: upsert_id
                        │ render email: upsert_email
                        │ render crdb_region: upsert_crdb_region
                        │
                        └── • project
                            │ columns: (upsert_id, upsert_email, upsert_crdb_region)
                            │
                            └── • scan buffer
                                  columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
                                  estimated row count: 1 (missing stats)
                                  label: buffer 1

query T
EXPLAIN (VERBOSE) INSERT INTO t_unique_hash_sec_key (id, email) VALUES (4321, 'some_email'), (8765, 'another_email') ON CONFLICT (email) DO UPDATE SET email = 'bad_email';
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • upsert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│   │ arbiter constraints: idx_uniq_hash_email
│   │
│   └── • buffer
│       │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
│       │ label: buffer 1
│       │
│       └── • project
│           │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
│           │
│           └── • render
│               │ columns: (check1, check2, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_id, upsert_email, upsert_crdb_region, upsert_crdb_internal_email_shard_16)
│               │ render check1: upsert_crdb_internal_email_shard_16 IN (0, 1, __more10_100__, 15)
│               │ render check2: upsert_crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│               │ render column1: column1
│               │ render column2: column2
│               │ render crdb_region_default: crdb_region_default
│               │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│               │ render id: id
│               │ render email: email
│               │ render crdb_region: crdb_region
│               │ render crdb_internal_email_shard_16: crdb_internal_email_shard_16
│               │ render upsert_id: upsert_id
│               │ render upsert_email: upsert_email
│               │ render upsert_crdb_region: upsert_crdb_region
│               │ render upsert_crdb_internal_email_shard_16: upsert_crdb_internal_email_shard_16
│               │
│               └── • render
│                   │ columns: (upsert_id, upsert_email, upsert_crdb_region, upsert_crdb_internal_email_shard_16, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16)
│                   │ render upsert_id: CASE WHEN crdb_region IS NULL THEN column1 ELSE id END
│                   │ render upsert_email: CASE WHEN crdb_region IS NULL THEN column2 ELSE 'bad_email' END
│                   │ render upsert_crdb_region: CASE WHEN crdb_region IS NULL THEN crdb_region_default ELSE crdb_region END
│                   │ render upsert_crdb_internal_email_shard_16: CASE WHEN crdb_region IS NULL THEN crdb_internal_email_shard_16_comp ELSE 9 END
│                   │ render column1: column1
│                   │ render column2: column2
│                   │ render crdb_region_default: crdb_region_default
│                   │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│                   │ render id: id
│                   │ render email: email
│                   │ render crdb_region: crdb_region
│                   │ render crdb_internal_email_shard_16: crdb_internal_email_shard_16
│                   │
│                   └── • render
│                       │ columns: (crdb_internal_email_shard_16, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region)
│                       │ render crdb_internal_email_shard_16: CASE id IS NULL WHEN true THEN CAST(NULL AS INT8) ELSE mod(fnv32(md5(crdb_internal.datums_to_bytes(email))), 16) END
│                       │ render column1: column1
│                       │ render column2: column2
│                       │ render crdb_region_default: crdb_region_default
│                       │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│                       │ render id: id
│                       │ render email: email
│                       │ render crdb_region: crdb_region
│                       │
│                       └── • project
│                           │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region)
│                           │
│                           └── • project
│                               │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16)
│                               │
│                               └── • lookup join (left outer)
│                                   │ columns: (crdb_internal_email_shard_16_eq, crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2, id, email, crdb_region, crdb_internal_email_shard_16)
│                                   │ estimated row count: 2 (missing stats)
│                                   │ table: t_unique_hash_sec_key@idx_uniq_hash_email
│                                   │ equality cols are key
│                                   │ lookup condition: ((crdb_region = 'ap-southeast-2') AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
│                                   │ remote lookup condition: ((crdb_region IN ('ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (column2 = email)
│                                   │ locking strength: for update
│                                   │
│                                   └── • render
│                                       │ columns: (crdb_internal_email_shard_16_eq, crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
│                                       │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
│                                       │ render crdb_internal_email_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
│                                       │ render crdb_region_default: 'ap-southeast-2'
│                                       │ render column1: column1
│                                       │ render column2: column2
│                                       │
│                                       └── • values
│                                             columns: (column1, column2)
│                                             size: 2 columns, 2 rows
│                                             row 0, expr 0: 4321
│                                             row 0, expr 1: 'some_email'
│                                             row 1, expr 0: 8765
│                                             row 1, expr 1: 'another_email'
│
├── • constraint-check
│   │
│   └── • error if rows
│       │ columns: ()
│       │
│       └── • project
│           │ columns: (upsert_id)
│           │
│           └── • lookup join (semi)
│               │ columns: (upsert_id, upsert_crdb_region)
│               │ estimated row count: 1 (missing stats)
│               │ table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
│               │ lookup condition: (crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (upsert_id = id)
│               │ pred: upsert_crdb_region != crdb_region
│               │
│               └── • project
│                   │ columns: (upsert_id, upsert_crdb_region)
│                   │
│                   └── • scan buffer
│                         columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
│                         estimated row count: 2 (missing stats)
│                         label: buffer 1
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (email)
            │
            └── • project
                │ columns: (id, email, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                    │ estimated row count: 1 (missing stats)
                    │ table: t_unique_hash_sec_key@idx_uniq_hash_email
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (email = email)
                    │ pred: (id != id) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                        │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(upsert_email))), 16)
                        │ render id: upsert_id
                        │ render email: upsert_email
                        │ render crdb_region: upsert_crdb_region
                        │
                        └── • project
                            │ columns: (upsert_id, upsert_email, upsert_crdb_region)
                            │
                            └── • scan buffer
                                  columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, upsert_email, upsert_crdb_internal_email_shard_16, crdb_region, check1, check2, upsert_id, upsert_crdb_region)
                                  estimated row count: 2 (missing stats)
                                  label: buffer 1

query T
EXPLAIN (VERBOSE) UPSERT INTO t_unique_hash_sec_key VALUES (1, 'email1');
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • upsert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│   │ arbiter constraints: t_unique_hash_sec_key_pkey
│   │
│   └── • buffer
│       │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_region, check1, check2, upsert_id)
│       │ label: buffer 1
│       │
│       └── • project
│           │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_region, check1, check2, upsert_id)
│           │
│           └── • render
│               │ columns: (check1, check2, upsert_id, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16)
│               │ render check1: crdb_internal_email_shard_16_comp IN (0, 1, __more10_100__, 15)
│               │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│               │ render upsert_id: CASE WHEN crdb_region IS NULL THEN column1 ELSE id END
│               │ render column1: column1
│               │ render column2: column2
│               │ render crdb_region_default: crdb_region_default
│               │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│               │ render id: id
│               │ render email: email
│               │ render crdb_region: crdb_region
│               │ render crdb_internal_email_shard_16: crdb_internal_email_shard_16
│               │
│               └── • cross join (left outer)
│                   │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_internal_email_shard_16, id, email, crdb_region)
│                   │ estimated row count: 1 (missing stats)
│                   │
│                   ├── • values
│                   │     columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp)
│                   │     size: 4 columns, 1 row
│                   │     row 0, expr 0: 1
│                   │     row 0, expr 1: 'email1'
│                   │     row 0, expr 2: 'ap-southeast-2'
│                   │     row 0, expr 3: 12
│                   │
│                   └── • render
│                       │ columns: (crdb_internal_email_shard_16, id, email, crdb_region)
│                       │ render crdb_internal_email_shard_16: mod(fnv32(md5(crdb_internal.datums_to_bytes(email))), 16)
│                       │ render id: id
│                       │ render email: email
│                       │ render crdb_region: crdb_region
│                       │
│                       └── • union all
│                           │ columns: (id, email, crdb_region)
│                           │ estimated row count: 1 (missing stats)
│                           │ limit: 1
│                           │
│                           ├── • scan
│                           │     columns: (id, email, crdb_region)
│                           │     estimated row count: 1 (missing stats)
│                           │     table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
│                           │     spans: /"@"/1/0
│                           │
│                           └── • scan
│                                 columns: (id, email, crdb_region)
│                                 estimated row count: 1 (missing stats)
│                                 table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
│                                 spans: /"\x80"/1/0 /"\xc0"/1/0
│                                 parallel
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (email)
            │
            └── • project
                │ columns: (id, email, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                    │ estimated row count: 0 (missing stats)
                    │ table: t_unique_hash_sec_key@idx_uniq_hash_email
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (email = email)
                    │ pred: (id != id) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                        │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
                        │ render id: upsert_id
                        │ render email: column2
                        │ render crdb_region: crdb_region_default
                        │
                        └── • project
                            │ columns: (upsert_id, column2, crdb_region_default)
                            │
                            └── • scan buffer
                                  columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_region, check1, check2, upsert_id)
                                  estimated row count: 1 (missing stats)
                                  label: buffer 1

query T
EXPLAIN (VERBOSE) UPSERT INTO t_unique_hash_sec_key VALUES (1, 'email1'), (8765, 'email2');
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • upsert
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ into: t_unique_hash_sec_key(id, email, crdb_region, crdb_internal_email_shard_16)
│   │ arbiter constraints: t_unique_hash_sec_key_pkey
│   │
│   └── • buffer
│       │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_region, check1, check2, upsert_id)
│       │ label: buffer 1
│       │
│       └── • project
│           │ columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_region, check1, check2, upsert_id)
│           │
│           └── • render
│               │ columns: (check1, check2, upsert_id, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16)
│               │ render check1: crdb_internal_email_shard_16_comp IN (0, 1, __more10_100__, 15)
│               │ render check2: crdb_region_default IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')
│               │ render upsert_id: CASE WHEN crdb_region IS NULL THEN column1 ELSE id END
│               │ render column1: column1
│               │ render column2: column2
│               │ render crdb_region_default: crdb_region_default
│               │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│               │ render id: id
│               │ render email: email
│               │ render crdb_region: crdb_region
│               │ render crdb_internal_email_shard_16: crdb_internal_email_shard_16
│               │
│               └── • render
│                   │ columns: (crdb_internal_email_shard_16, column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region)
│                   │ render crdb_internal_email_shard_16: CASE id IS NULL WHEN true THEN CAST(NULL AS INT8) ELSE mod(fnv32(md5(crdb_internal.datums_to_bytes(email))), 16) END
│                   │ render column1: column1
│                   │ render column2: column2
│                   │ render crdb_region_default: crdb_region_default
│                   │ render crdb_internal_email_shard_16_comp: crdb_internal_email_shard_16_comp
│                   │ render id: id
│                   │ render email: email
│                   │ render crdb_region: crdb_region
│                   │
│                   └── • lookup join (left outer)
│                       │ columns: (crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2, id, email, crdb_region)
│                       │ estimated row count: 2 (missing stats)
│                       │ table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
│                       │ equality cols are key
│                       │ lookup condition: (crdb_region = 'ap-southeast-2') AND (column1 = id)
│                       │ remote lookup condition: (crdb_region IN ('ca-central-1', 'us-east-1')) AND (column1 = id)
│                       │ locking strength: for update
│                       │
│                       └── • render
│                           │ columns: (crdb_internal_email_shard_16_comp, crdb_region_default, column1, column2)
│                           │ render crdb_internal_email_shard_16_comp: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
│                           │ render crdb_region_default: 'ap-southeast-2'
│                           │ render column1: column1
│                           │ render column2: column2
│                           │
│                           └── • values
│                                 columns: (column1, column2)
│                                 size: 2 columns, 2 rows
│                                 row 0, expr 0: 1
│                                 row 0, expr 1: 'email1'
│                                 row 1, expr 0: 8765
│                                 row 1, expr 1: 'email2'
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (email)
            │
            └── • project
                │ columns: (id, email, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                    │ estimated row count: 1 (missing stats)
                    │ table: t_unique_hash_sec_key@idx_uniq_hash_email
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (email = email)
                    │ pred: (id != id) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                        │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(column2))), 16)
                        │ render id: upsert_id
                        │ render email: column2
                        │ render crdb_region: crdb_region_default
                        │
                        └── • project
                            │ columns: (upsert_id, column2, crdb_region_default)
                            │
                            └── • scan buffer
                                  columns: (column1, column2, crdb_region_default, crdb_internal_email_shard_16_comp, id, email, crdb_region, crdb_internal_email_shard_16, column2, crdb_region_default, crdb_internal_email_shard_16_comp, crdb_region, check1, check2, upsert_id)
                                  estimated row count: 2 (missing stats)
                                  label: buffer 1

query T
EXPLAIN (VERBOSE) UPDATE t_unique_hash_sec_key SET email = 'email1' WHERE id = 2;
----
distribution: local
vectorized: true
·
• root
│ columns: ()
│
├── • update
│   │ columns: ()
│   │ estimated row count: 0 (missing stats)
│   │ table: t_unique_hash_sec_key
│   │ set: email, crdb_internal_email_shard_16
│   │
│   └── • buffer
│       │ columns: (id, email, crdb_region, crdb_internal_email_shard_16, email_new, crdb_internal_email_shard_16_comp, check1)
│       │ label: buffer 1
│       │
│       └── • render
│           │ columns: (id, email, crdb_region, crdb_internal_email_shard_16, email_new, crdb_internal_email_shard_16_comp, check1)
│           │ render check1: true
│           │ render crdb_internal_email_shard_16_comp: 12
│           │ render email_new: 'email1'
│           │ render crdb_internal_email_shard_16: mod(fnv32(md5(crdb_internal.datums_to_bytes(email))), 16)
│           │ render id: id
│           │ render email: email
│           │ render crdb_region: crdb_region
│           │
│           └── • union all
│               │ columns: (id, email, crdb_region)
│               │ estimated row count: 1 (missing stats)
│               │ limit: 1
│               │
│               ├── • scan
│               │     columns: (id, email, crdb_region)
│               │     estimated row count: 1 (missing stats)
│               │     table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
│               │     spans: /"@"/2/0
│               │
│               └── • scan
│                     columns: (id, email, crdb_region)
│                     estimated row count: 1 (missing stats)
│                     table: t_unique_hash_sec_key@t_unique_hash_sec_key_pkey
│                     spans: /"\x80"/2/0 /"\xc0"/2/0
│                     parallel
│
└── • constraint-check
    │
    └── • error if rows
        │ columns: ()
        │
        └── • project
            │ columns: (email)
            │
            └── • project
                │ columns: (id, email, crdb_region)
                │
                └── • lookup join (semi)
                    │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                    │ estimated row count: 0 (missing stats)
                    │ table: t_unique_hash_sec_key@idx_uniq_hash_email
                    │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (crdb_internal_email_shard_16_eq = crdb_internal_email_shard_16)) AND (email = email)
                    │ pred: (id != id) OR (crdb_region != crdb_region)
                    │
                    └── • render
                        │ columns: (crdb_internal_email_shard_16_eq, id, email, crdb_region)
                        │ render crdb_internal_email_shard_16_eq: mod(fnv32(md5(crdb_internal.datums_to_bytes(email_new))), 16)
                        │ render id: id
                        │ render email: email_new
                        │ render crdb_region: crdb_region
                        │
                        └── • project
                            │ columns: (id, email_new, crdb_region)
                            │
                            └── • scan buffer
                                  columns: (id, email, crdb_region, crdb_internal_email_shard_16, email_new, crdb_internal_email_shard_16_comp, check1)
                                  estimated row count: 1 (missing stats)
                                  label: buffer 1
