# knob-opt: sync-event-log
# LogicTest: multiregion-9node-3region-3azs multiregion-9node-3region-3azs-tenant multiregion-9node-3region-3azs-no-los

query TTTTT colnames,rowsort
SHOW REGIONS
----
region          zones                   database_names  primary_region_of  secondary_region_of
ap-southeast-2  {ap-az1,ap-az2,ap-az3}  {}              {}                 {}
ca-central-1    {ca-az1,ca-az2,ca-az3}  {}              {}                 {}
us-east-1       {us-az1,us-az2,us-az3}  {}              {}                 {}

query TT colnames,rowsort
SHOW REGIONS FROM CLUSTER
----
region          zones
ap-southeast-2  {ap-az1,ap-az2,ap-az3}
ca-central-1    {ca-az1,ca-az2,ca-az3}
us-east-1       {us-az1,us-az2,us-az3}

statement error cannot set LOCALITY on a table in a database that is not multi-region enabled\nHINT: database must first be multi-region enabled using ALTER DATABASE ... SET PRIMARY REGION <region>
CREATE TABLE regional_by_table_table (pk int) LOCALITY REGIONAL BY TABLE

statement error cannot set LOCALITY on a table in a database that is not multi-region enabled\nHINT: database must first be multi-region enabled using ALTER DATABASE ... SET PRIMARY REGION <region>
CREATE TABLE global_table (pk int) LOCALITY GLOBAL

statement ok
CREATE DATABASE region_test_db PRIMARY REGION "ap-southeast-2" SURVIVE ZONE FAILURE

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

statement error at least 3 regions are required for surviving a region failure\nHINT: you must add additional regions to the database or change the survivability goal
ALTER DATABASE multi_region_test_db DROP REGION "ap-southeast-2"

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

statement error current database test is not multi-region enabled
SELECT default_to_database_primary_region(gateway_region())

query T
SELECT gateway_region()
----
ap-southeast-2

query T nodeidx=3
SELECT gateway_region()
----
ca-central-1

query T nodeidx=6
SELECT gateway_region()
----
us-east-1

query TT
USE region_test_db;
SELECT gateway_region(), default_to_database_primary_region(gateway_region())
----
ap-southeast-2  ap-southeast-2

query TT nodeidx=6
USE region_test_db;
SELECT gateway_region(), default_to_database_primary_region(gateway_region())
----
us-east-1  ap-southeast-2

query TT
USE multi_region_test_db;
SELECT gateway_region(), default_to_database_primary_region(gateway_region())
----
ap-southeast-2  ap-southeast-2

query TT nodeidx=6
USE multi_region_test_db;
SELECT gateway_region(), default_to_database_primary_region(gateway_region())
----
us-east-1  us-east-1

statement ok
CREATE DATABASE deleted_database;

statement ok
SET sql_safe_updates = false;

statement ok
USE deleted_database;

statement ok
DROP DATABASE deleted_database

statement error database.*deleted_database.*does not exist
SELECT default_to_database_primary_region('ajstorm-db-1')

statement ok
SET sql_safe_updates = true;
USE multi_region_test_db

# Ensure that the region types were created for all the MR databases above.
query ITT colnames,rowsort
SELECT
  e."parentSchemaID",
  parent.name AS db_name,
  e.name AS enum_name
FROM system.namespace e
JOIN system.namespace parent ON (e."parentID" = parent.id)
WHERE e.name='crdb_internal_region'
----
parentSchemaID  db_name                                       enum_name
111             multi_region_test_db                          crdb_internal_region
115             multi_region_test_explicit_primary_region_db  crdb_internal_region
107             region_test_db                                crdb_internal_region

query TTTT colnames
SHOW ENUMS FROM region_test_db.public
----
schema  name                   values   owner
public  crdb_internal_region  {ap-southeast-2}  root

query TTTT colnames
SHOW ENUMS FROM multi_region_test_db.public
----
schema  name                   values               owner
public  crdb_internal_region  {ap-southeast-2,ca-central-1,us-east-1}  root

query TTTT colnames
SHOW ENUMS FROM multi_region_test_explicit_primary_region_db.public
----
schema  name                   values               owner
public  crdb_internal_region  {ap-southeast-2,ca-central-1,us-east-1}  root

statement ok
SELECT 'ap-southeast-2'::region_test_db.public.crdb_internal_region

statement error invalid input value for enum crdb_internal_region: "ca-central-1"
SELECT 'ca-central-1'::region_test_db.public.crdb_internal_region

statement ok
SELECT 'ap-southeast-2'::multi_region_test_db.public.crdb_internal_region

statement ok
SELECT 'ca-central-1'::multi_region_test_db.public.crdb_internal_region

statement error "multi_region_test_db.public.crdb_internal_region" is a multi-region enum and cannot be modified directly
DROP TYPE multi_region_test_db.public.crdb_internal_region

statement error "multi_region_test_db.public.crdb_internal_region" is a multi-region enum and can't be modified using the alter type command
ALTER TYPE multi_region_test_db.public.crdb_internal_region ADD VALUE 'us-east-1'

statement error "multi_region_test_db.public.crdb_internal_region" is a multi-region enum and can't be modified using the alter type command
ALTER TYPE multi_region_test_db.public.crdb_internal_region DROP VALUE 'us-east-1'

statement ok
GRANT ALL ON DATABASE multi_region_test_db TO testuser;
ALTER TYPE multi_region_test_db.public.crdb_internal_region OWNER TO testuser

query T
SELECT owner FROM [SHOW TYPES] WHERE name = 'crdb_internal_region' AND schema = 'public'
----
testuser

statement error region "region_no_exists" does not exist\nHINT:.*valid regions: ap-southeast-2, ca-central-1, us-east-1
CREATE DATABASE invalid_region_db PRIMARY REGION "region_no_exists" REGION "region_no_exists"

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

query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name                                 owner  primary_region  secondary_region  regions                                  survival_goal
defaultdb                                     root   NULL            NULL              {}                                       NULL
multi_region_test_db                          root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_explicit_primary_region_db  root   ap-southeast-2  ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_survive_zone_failure_db     root   us-east-1       ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
postgres                                      root   NULL            NULL              {}                                       NULL
region_test_db                                root   ap-southeast-2  ·                 {ap-southeast-2}                         zone
system                                        node   NULL            NULL              {}                                       NULL
test                                          root   NULL            NULL              {}                                       NULL

query TTTT colnames,rowsort
SHOW REGIONS FROM ALL DATABASES
----
database_name                                 regions                                  primary_region  secondary_region
defaultdb                                     {}                                       NULL            NULL
multi_region_test_db                          {ap-southeast-2,ca-central-1,us-east-1}  ca-central-1    ·
multi_region_test_explicit_primary_region_db  {ap-southeast-2,ca-central-1,us-east-1}  ap-southeast-2  ·
multi_region_test_survive_zone_failure_db     {ap-southeast-2,ca-central-1,us-east-1}  us-east-1       ·
postgres                                      {}                                       NULL            NULL
region_test_db                                {ap-southeast-2}                         ap-southeast-2  ·
system                                        {}                                       NULL            NULL
test                                          {}                                       NULL            NULL

statement ok
USE multi_region_test_db

query TTBBT colnames,rowsort
SHOW REGIONS FROM DATABASE
----
database              region          primary  secondary  zones
multi_region_test_db  ca-central-1    true     false      {ca-az1,ca-az2,ca-az3}
multi_region_test_db  ap-southeast-2  false    false      {ap-az1,ap-az2,ap-az3}
multi_region_test_db  us-east-1       false    false      {us-az1,us-az2,us-az3}

query TTTTT colnames,rowsort
SHOW REGIONS
----
region          zones                   database_names                                                                                                                primary_region_of                                              secondary_region_of
ap-southeast-2  {ap-az1,ap-az2,ap-az3}  {multi_region_test_db,multi_region_test_explicit_primary_region_db,multi_region_test_survive_zone_failure_db,region_test_db}  {multi_region_test_explicit_primary_region_db,region_test_db}  {}
ca-central-1    {ca-az1,ca-az2,ca-az3}  {multi_region_test_db,multi_region_test_explicit_primary_region_db,multi_region_test_survive_zone_failure_db}                 {multi_region_test_db}                                         {}
us-east-1       {us-az1,us-az2,us-az3}  {multi_region_test_db,multi_region_test_explicit_primary_region_db,multi_region_test_survive_zone_failure_db}                 {multi_region_test_survive_zone_failure_db}                    {}

query TT
SHOW SURVIVAL GOAL FROM DATABASE
----
multi_region_test_db  region

query TTBBT colnames
SHOW REGIONS FROM DATABASE region_test_db
----
database        region          primary  secondary  zones
region_test_db  ap-southeast-2  true     false      {ap-az1,ap-az2,ap-az3}

query TT
SHOW SURVIVAL GOAL FROM DATABASE region_test_db
----
region_test_db  zone

query TT
SHOW ZONE CONFIGURATION FOR DATABASE region_test_db
----
DATABASE region_test_db  ALTER DATABASE region_test_db CONFIGURE ZONE USING
                           range_min_bytes = 134217728,
                           range_max_bytes = 536870912,
                           gc.ttlseconds = 14400,
                           num_replicas = 3,
                           num_voters = 3,
                           constraints = '{+region=ap-southeast-2: 1}',
                           voter_constraints = '[+region=ap-southeast-2]',
                           lease_preferences = '[[+region=ap-southeast-2]]'

# copy of the previous test but using FROM instead of FOR
query TT
SHOW ZONE CONFIGURATION FROM DATABASE region_test_db
----
DATABASE region_test_db  ALTER DATABASE region_test_db CONFIGURE ZONE USING
                           range_min_bytes = 134217728,
                           range_max_bytes = 536870912,
                           gc.ttlseconds = 14400,
                           num_replicas = 3,
                           num_voters = 3,
                           constraints = '{+region=ap-southeast-2: 1}',
                           voter_constraints = '[+region=ap-southeast-2]',
                           lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR DATABASE multi_region_test_db
----
DATABASE multi_region_test_db  ALTER DATABASE multi_region_test_db CONFIGURE ZONE USING
                                 range_min_bytes = 134217728,
                                 range_max_bytes = 536870912,
                                 gc.ttlseconds = 14400,
                                 num_replicas = 5,
                                 num_voters = 5,
                                 constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                 voter_constraints = '{+region=ca-central-1: 2}',
                                 lease_preferences = '[[+region=ca-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR DATABASE multi_region_test_explicit_primary_region_db
----
DATABASE multi_region_test_explicit_primary_region_db  ALTER DATABASE multi_region_test_explicit_primary_region_db CONFIGURE ZONE USING
                                                         range_min_bytes = 134217728,
                                                         range_max_bytes = 536870912,
                                                         gc.ttlseconds = 14400,
                                                         num_replicas = 5,
                                                         num_voters = 5,
                                                         constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                                         voter_constraints = '{+region=ap-southeast-2: 2}',
                                                         lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR DATABASE multi_region_test_survive_zone_failure_db
----
DATABASE multi_region_test_survive_zone_failure_db  ALTER DATABASE multi_region_test_survive_zone_failure_db CONFIGURE ZONE USING
                                                      range_min_bytes = 134217728,
                                                      range_max_bytes = 536870912,
                                                      gc.ttlseconds = 14400,
                                                      num_replicas = 5,
                                                      num_voters = 3,
                                                      constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                                      voter_constraints = '[+region=us-east-1]',
                                                      lease_preferences = '[[+region=us-east-1]]'

statement error PRIMARY REGION must be specified when using SURVIVE
CREATE DATABASE no_primary_region SURVIVE ZONE FAILURE

statement error PRIMARY REGION must be specified if REGIONS are specified
CREATE DATABASE no_primary_region_db REGIONS "ap-southeast-2"

statement error at least 3 regions are required for surviving a region failure
CREATE DATABASE not_enough_regions_db PRIMARY REGION "ap-southeast-2" REGIONS "ap-southeast-2", "ca-central-1" SURVIVE REGION FAILURE

statement error region "region_no_exists" does not exist\nHINT:.*valid regions: ap-southeast-2, ca-central-1, us-east-1
CREATE DATABASE invalid_region_db PRIMARY REGION "region_no_exists"

statement error region "ap-southeast-2" defined multiple times
CREATE DATABASE duplicate_region_name_db PRIMARY REGION "ap-southeast-2" REGIONS "ap-southeast-2", "ap-southeast-2"

statement error multi-region tables containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY
)
PARTITION BY LIST (id) (
  PARTITION "one" VALUES IN (1)
)

statement error multi-region tables containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY
)
PARTITION BY LIST (id) (
  PARTITION "one" VALUES IN (1)
)
LOCALITY GLOBAL

statement error multi-region tables containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY
)
PARTITION BY LIST (id) (
  PARTITION "one" VALUES IN (1)
)
LOCALITY REGIONAL BY TABLE

statement error multi-region tables with an INDEX containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY,
  INDEX idx(id) PARTITION BY LIST (id) (
    PARTITION "one" VALUES IN (1)
  )
)

statement error multi-region tables with an INDEX containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY,
  INDEX idx(id) PARTITION BY LIST (id) (
    PARTITION "one" VALUES IN (1)
  )
) LOCALITY GLOBAL

statement error multi-region tables with an INDEX containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY,
  INDEX idx(id) PARTITION BY LIST (id) (
    PARTITION "one" VALUES IN (1)
  )
) LOCALITY REGIONAL BY TABLE

statement error multi-region tables with an UNIQUE constraint containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY,
  UNIQUE INDEX idx(id) PARTITION BY LIST (id) (
    PARTITION "one" VALUES IN (1)
  )
)

statement error multi-region tables with an UNIQUE constraint containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY,
  UNIQUE INDEX idx(id) PARTITION BY LIST (id) (
    PARTITION "one" VALUES IN (1)
  )
)
LOCALITY GLOBAL

statement error multi-region tables with an UNIQUE constraint containing PARTITION BY are not supported
CREATE TABLE partition_by_table_in_mr_database (
  id INT PRIMARY KEY,
  UNIQUE INDEX idx(id) PARTITION BY LIST (id) (
    PARTITION "one" VALUES IN (1)
  )
)
LOCALITY REGIONAL BY TABLE

statement ok
CREATE TABLE regional_primary_region_table (a int) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query T
SELECT create_statement FROM [SHOW CREATE TABLE regional_primary_region_table]
----
CREATE TABLE public.regional_primary_region_table (
                                                a INT8 NULL,
                                                rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
                                                CONSTRAINT regional_primary_region_table_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW ZONE CONFIGURATION FOR TABLE regional_primary_region_table
----
DATABASE multi_region_test_db  ALTER DATABASE multi_region_test_db CONFIGURE ZONE USING
                                 range_min_bytes = 134217728,
                                 range_max_bytes = 536870912,
                                 gc.ttlseconds = 14400,
                                 num_replicas = 5,
                                 num_voters = 5,
                                 constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                 voter_constraints = '{+region=ca-central-1: 2}',
                                 lease_preferences = '[[+region=ca-central-1]]'

statement error cannot set PARTITION BY on a table in a multi-region enabled database
ALTER TABLE regional_primary_region_table PARTITION BY LIST (id) (
  PARTITION "one" VALUES IN (1)
)

statement error cannot define PARTITION BY on a new INDEX in a multi-region database
CREATE INDEX bad_idx ON regional_primary_region_table(a) PARTITION BY LIST (a) (
  PARTITION one VALUES IN (1)
)

statement ok
CREATE TABLE regional_implicit_primary_region_table (a int) LOCALITY REGIONAL BY TABLE

query T
SELECT create_statement FROM [SHOW CREATE TABLE regional_implicit_primary_region_table]
----
CREATE TABLE public.regional_implicit_primary_region_table (
                                                a INT8 NULL,
                                                rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
                                                CONSTRAINT regional_implicit_primary_region_table_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW ZONE CONFIGURATION FOR TABLE regional_implicit_primary_region_table
----
DATABASE multi_region_test_db  ALTER DATABASE multi_region_test_db CONFIGURE ZONE USING
                                 range_min_bytes = 134217728,
                                 range_max_bytes = 536870912,
                                 gc.ttlseconds = 14400,
                                 num_replicas = 5,
                                 num_voters = 5,
                                 constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                 voter_constraints = '{+region=ca-central-1: 2}',
                                 lease_preferences = '[[+region=ca-central-1]]'

statement ok
CREATE TABLE "regional_us-east-1_table" (a int) LOCALITY REGIONAL BY TABLE IN "us-east-1"

query T
SELECT create_statement FROM [SHOW CREATE TABLE "regional_us-east-1_table"]
----
CREATE TABLE public."regional_us-east-1_table" (
                                             a INT8 NULL,
                                             rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
                                             CONSTRAINT "regional_us-east-1_table_pkey" PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN "us-east-1"

query TT
SHOW ZONE CONFIGURATION FOR TABLE "regional_us-east-1_table"
----
TABLE "regional_us-east-1_table"  ALTER TABLE "regional_us-east-1_table" CONFIGURE ZONE USING
                                    range_min_bytes = 134217728,
                                    range_max_bytes = 536870912,
                                    gc.ttlseconds = 14400,
                                    num_replicas = 5,
                                    num_voters = 5,
                                    constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                    voter_constraints = '{+region=us-east-1: 2}',
                                    lease_preferences = '[[+region=us-east-1]]'

statement error region "test4" has not been added to database "multi_region_test_db"\nHINT: available regions: ap-southeast-2, ca-central-1, us-east-1
CREATE TABLE regional_test4_table (a int) LOCALITY REGIONAL BY TABLE IN "test4"

statement ok
CREATE TABLE global_table (a int) LOCALITY GLOBAL

query T
SELECT create_statement FROM [SHOW CREATE TABLE global_table]
----
CREATE TABLE public.global_table (
                   a INT8 NULL,
                   rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
                   CONSTRAINT global_table_pkey PRIMARY KEY (rowid ASC)
) LOCALITY GLOBAL

query TT
SHOW ZONE CONFIGURATION FOR TABLE global_table
----
TABLE global_table  ALTER TABLE global_table CONFIGURE ZONE USING
                      range_min_bytes = 134217728,
                      range_max_bytes = 536870912,
                      gc.ttlseconds = 14400,
                      global_reads = true,
                      num_replicas = 5,
                      num_voters = 5,
                      constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                      voter_constraints = '{+region=ca-central-1: 2}',
                      lease_preferences = '[[+region=ca-central-1]]'

statement error cannot set PARTITION BY on a table in a multi-region enabled database
ALTER TABLE global_table PARTITION BY LIST (id) (
  PARTITION "one" VALUES IN (1)
)

statement error cannot define PARTITION BY on a new INDEX in a multi-region database
CREATE INDEX bad_idx ON global_table(a) PARTITION BY LIST (a) (
  PARTITION one VALUES IN (1)
)

query TTTTIT colnames
SELECT * FROM [SHOW TABLES] ORDER BY table_name
----
schema_name  table_name                              type   owner  estimated_row_count  locality
public       global_table                            table  root   0                    GLOBAL
public       regional_implicit_primary_region_table  table  root   0                    REGIONAL BY TABLE IN PRIMARY REGION
public       regional_primary_region_table           table  root   0                    REGIONAL BY TABLE IN PRIMARY REGION
public       regional_us-east-1_table                table  root   0                    REGIONAL BY TABLE IN "us-east-1"

statement ok
CREATE DATABASE new_db

statement ok
USE new_db

statement error cannot set LOCALITY on a table in a database that is not multi-region enabled
CREATE TABLE cannot_create_table_no_multiregion (a int) LOCALITY GLOBAL

# For now, just test to see that non-root admin users cannot make the system
# database a multi-region database.

statement ok
GRANT admin TO testuser

user testuser

# Test adding a primary region to the system database as a secondary tenant.
onlyif config multiregion-9node-3region-3azs-tenant
statement error pgcode 42501 user testuser may not modify the system database
ALTER DATABASE system PRIMARY REGION "ap-southeast-2"

# Note that nobody can add a region to the system database of the system tenant.
# However, because of logic to metamorphically run as a secondary tenant, we
# cannot rely on the fact that in these configs not marked "tenant", that we
# are in the system tenant. Hence the lack of error code checking and the
# regex matching both possible outcomes.
skipif config multiregion-9node-3region-3azs-tenant
statement error (user testuser may not modify the system database|modifying the regions of system database is not supported)
ALTER DATABASE system PRIMARY REGION "ap-southeast-2"

user root

statement ok
REVOKE admin FROM testuser

statement ok
CREATE DATABASE alter_test_db primary region "ca-central-1"

statement ok
USE alter_test_db

query TTTT colnames
SHOW ENUMS FROM alter_test_db.public
----
schema  name                  values   owner
public  crdb_internal_region  {ca-central-1}  root

# Test adding regions before first region.
statement ok
ALTER DATABASE alter_test_db ADD REGION "ap-southeast-2"

query TTBBT colnames,rowsort
show regions from database alter_test_db
----
database       region          primary  secondary  zones
alter_test_db  ca-central-1    true     false      {ca-az1,ca-az2,ca-az3}
alter_test_db  ap-southeast-2  false    false      {ap-az1,ap-az2,ap-az3}

query TTTT colnames
SHOW ENUMS FROM alter_test_db.public
----
schema  name                  values         owner
public  crdb_internal_region  {ap-southeast-2,ca-central-1}  root

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_test_db
----
DATABASE alter_test_db  ALTER DATABASE alter_test_db CONFIGURE ZONE USING
                          range_min_bytes = 134217728,
                          range_max_bytes = 536870912,
                          gc.ttlseconds = 14400,
                          num_replicas = 4,
                          num_voters = 3,
                          constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1}',
                          voter_constraints = '[+region=ca-central-1]',
                          lease_preferences = '[[+region=ca-central-1]]'

# Test adding a region after first region.
query T noticetrace
ALTER DATABASE alter_test_db ADD REGION IF NOT EXISTS "us-east-1"
----

query TTBBT colnames,rowsort
show regions from database alter_test_db
----
database       region          primary  secondary  zones
alter_test_db  ca-central-1    true     false      {ca-az1,ca-az2,ca-az3}
alter_test_db  ap-southeast-2  false    false      {ap-az1,ap-az2,ap-az3}
alter_test_db  us-east-1       false    false      {us-az1,us-az2,us-az3}

query TTTT colnames
SHOW ENUMS FROM alter_test_db.public
----
schema  name                  values               owner
public  crdb_internal_region  {ap-southeast-2,ca-central-1,us-east-1}  root

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_test_db
----
DATABASE alter_test_db  ALTER DATABASE alter_test_db CONFIGURE ZONE USING
                          range_min_bytes = 134217728,
                          range_max_bytes = 536870912,
                          gc.ttlseconds = 14400,
                          num_replicas = 5,
                          num_voters = 3,
                          constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                          voter_constraints = '[+region=ca-central-1]',
                          lease_preferences = '[[+region=ca-central-1]]'

# Failure testing for ADD REGION.
statement error region "test" does not exist
ALTER DATABASE alter_test_db ADD REGION "test"

statement error pgcode 42710 region "ap-southeast-2" already added to database
ALTER DATABASE alter_test_db ADD REGION "ap-southeast-2"

query T noticetrace
ALTER DATABASE alter_test_db ADD REGION IF NOT EXISTS "ap-southeast-2"
----
NOTICE: region "ap-southeast-2" already exists; skipping

statement error cannot add region
ALTER DATABASE new_db ADD REGION "us-west-1"

statement error pq: database has no regions to drop
ALTER DATABASE new_db DROP REGION "us-west-1"

query TTBBT colnames,rowsort
show regions from database alter_test_db
----
database       region          primary  secondary  zones
alter_test_db  ca-central-1    true     false      {ca-az1,ca-az2,ca-az3}
alter_test_db  ap-southeast-2  false    false      {ap-az1,ap-az2,ap-az3}
alter_test_db  us-east-1       false    false      {us-az1,us-az2,us-az3}

statement ok
CREATE DATABASE alter_primary_region_db

# TODO(ajstorm) add test here to validate that REGIONAL BY TABLE tables behave appropriately
statement ok
use alter_primary_region_db

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_primary_region_db
----
RANGE default  ALTER RANGE default CONFIGURE ZONE USING
                 range_min_bytes = 134217728,
                 range_max_bytes = 536870912,
                 gc.ttlseconds = 14400,
                 num_replicas = 3,
                 constraints = '[]',
                 lease_preferences = '[]'

query TTBBT colnames
show regions from database alter_primary_region_db
----
database  region  primary  secondary  zones

query TTTT colnames
SHOW ENUMS FROM alter_primary_region_db.public
----
schema  name  values  owner

statement error region "test" does not exist
ALTER DATABASE alter_primary_region_db PRIMARY REGION "test"

# Since the last statement was rejected, ensure that no changes were made to the
# database's zone configuration
query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_primary_region_db
----
RANGE default  ALTER RANGE default CONFIGURE ZONE USING
                 range_min_bytes = 134217728,
                 range_max_bytes = 536870912,
                 gc.ttlseconds = 14400,
                 num_replicas = 3,
                 constraints = '[]',
                 lease_preferences = '[]'

statement ok
ALTER DATABASE alter_primary_region_db PRIMARY REGION "ca-central-1"

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_primary_region_db
----
DATABASE alter_primary_region_db  ALTER DATABASE alter_primary_region_db CONFIGURE ZONE USING
                                    range_min_bytes = 134217728,
                                    range_max_bytes = 536870912,
                                    gc.ttlseconds = 14400,
                                    num_replicas = 3,
                                    num_voters = 3,
                                    constraints = '{+region=ca-central-1: 1}',
                                    voter_constraints = '[+region=ca-central-1]',
                                    lease_preferences = '[[+region=ca-central-1]]'

query TTBBT colnames
show regions from database alter_primary_region_db
----
database                 region        primary  secondary  zones
alter_primary_region_db  ca-central-1  true     false      {ca-az1,ca-az2,ca-az3}

query TTTT colnames
SHOW ENUMS FROM alter_primary_region_db.public
----
schema  name                  values          owner
public  crdb_internal_region  {ca-central-1}  root

statement error region "ap-southeast-2" has not been added to the database
ALTER DATABASE alter_primary_region_db PRIMARY REGION "ap-southeast-2"

statement ok
ALTER DATABASE alter_primary_region_db ADD REGION "ap-southeast-2"

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_primary_region_db
----
DATABASE alter_primary_region_db  ALTER DATABASE alter_primary_region_db CONFIGURE ZONE USING
                                    range_min_bytes = 134217728,
                                    range_max_bytes = 536870912,
                                    gc.ttlseconds = 14400,
                                    num_replicas = 4,
                                    num_voters = 3,
                                    constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1}',
                                    voter_constraints = '[+region=ca-central-1]',
                                    lease_preferences = '[[+region=ca-central-1]]'

query TTTT colnames
SHOW ENUMS FROM alter_primary_region_db.public
----
schema  name                  values                         owner
public  crdb_internal_region  {ap-southeast-2,ca-central-1}  root

query TTBBT colnames,rowsort
show regions from database alter_primary_region_db
----
database                 region          primary  secondary  zones
alter_primary_region_db  ca-central-1    true     false      {ca-az1,ca-az2,ca-az3}
alter_primary_region_db  ap-southeast-2  false    false      {ap-az1,ap-az2,ap-az3}

statement ok
ALTER DATABASE alter_primary_region_db PRIMARY REGION "ap-southeast-2"

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_primary_region_db
----
DATABASE alter_primary_region_db  ALTER DATABASE alter_primary_region_db CONFIGURE ZONE USING
                                    range_min_bytes = 134217728,
                                    range_max_bytes = 536870912,
                                    gc.ttlseconds = 14400,
                                    num_replicas = 4,
                                    num_voters = 3,
                                    constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1}',
                                    voter_constraints = '[+region=ap-southeast-2]',
                                    lease_preferences = '[[+region=ap-southeast-2]]'

query TTTT colnames
SHOW ENUMS FROM alter_primary_region_db.public
----
schema  name                  values                         owner
public  crdb_internal_region  {ap-southeast-2,ca-central-1}  root

query TTBBT colnames,rowsort
show regions from database alter_primary_region_db
----
database                 region          primary  secondary  zones
alter_primary_region_db  ap-southeast-2  true     false      {ap-az1,ap-az2,ap-az3}
alter_primary_region_db  ca-central-1    false    false      {ca-az1,ca-az2,ca-az3}

statement ok
create database alter_survive_db

statement ok
use alter_survive_db

statement error database must have associated regions before a survival goal can be set
alter database alter_survive_db survive region failure

statement ok
alter database alter_survive_db primary region "ca-central-1"

statement error at least 3 regions are required for surviving a region failure
alter database alter_survive_db survive region failure

statement ok
alter database alter_survive_db add region "ap-southeast-2"

statement ok
alter database alter_survive_db add region "us-east-1"

# Create some tables to validate that their zone configurations are adjusted appropriately.
query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_survive_db
----
DATABASE alter_survive_db  ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 5,
                             num_voters = 3,
                             constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                             voter_constraints = '[+region=ca-central-1]',
                             lease_preferences = '[[+region=ca-central-1]]'

statement ok
create table t_no_locality (i int)

query TT
SHOW ZONE CONFIGURATION FOR TABLE t_no_locality
----
DATABASE alter_survive_db  ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 5,
                             num_voters = 3,
                             constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                             voter_constraints = '[+region=ca-central-1]',
                             lease_preferences = '[[+region=ca-central-1]]'

statement ok
CREATE TABLE t_global (i int) LOCALITY GLOBAL

query TT
SHOW ZONE CONFIGURATION FOR TABLE t_global
----
TABLE t_global  ALTER TABLE t_global CONFIGURE ZONE USING
                  range_min_bytes = 134217728,
                  range_max_bytes = 536870912,
                  gc.ttlseconds = 14400,
                  global_reads = true,
                  num_replicas = 5,
                  num_voters = 3,
                  constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                  voter_constraints = '[+region=ca-central-1]',
                  lease_preferences = '[[+region=ca-central-1]]'

query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name                                 owner  primary_region  secondary_region  regions                                  survival_goal
alter_primary_region_db                       root   ap-southeast-2  ·                 {ap-southeast-2,ca-central-1}            zone
alter_survive_db                              root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
alter_test_db                                 root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
defaultdb                                     root   NULL            NULL              {}                                       NULL
multi_region_test_db                          root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_explicit_primary_region_db  root   ap-southeast-2  ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_survive_zone_failure_db     root   us-east-1       ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
new_db                                        root   NULL            NULL              {}                                       NULL
postgres                                      root   NULL            NULL              {}                                       NULL
region_test_db                                root   ap-southeast-2  ·                 {ap-southeast-2}                         zone
system                                        node   NULL            NULL              {}                                       NULL
test                                          root   NULL            NULL              {}                                       NULL

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_survive_db
----
DATABASE alter_survive_db  ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 5,
                             num_voters = 3,
                             constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                             voter_constraints = '[+region=ca-central-1]',
                             lease_preferences = '[[+region=ca-central-1]]'

statement ok
alter database alter_survive_db survive region failure

query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name                                 owner  primary_region  secondary_region  regions                                  survival_goal
alter_primary_region_db                       root   ap-southeast-2  ·                 {ap-southeast-2,ca-central-1}            zone
alter_survive_db                              root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
alter_test_db                                 root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
defaultdb                                     root   NULL            NULL              {}                                       NULL
multi_region_test_db                          root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_explicit_primary_region_db  root   ap-southeast-2  ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_survive_zone_failure_db     root   us-east-1       ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
new_db                                        root   NULL            NULL              {}                                       NULL
postgres                                      root   NULL            NULL              {}                                       NULL
region_test_db                                root   ap-southeast-2  ·                 {ap-southeast-2}                         zone
system                                        node   NULL            NULL              {}                                       NULL
test                                          root   NULL            NULL              {}                                       NULL

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_survive_db
----
DATABASE alter_survive_db  ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 5,
                             num_voters = 5,
                             constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                             voter_constraints = '{+region=ca-central-1: 2}',
                             lease_preferences = '[[+region=ca-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE t_no_locality
----
DATABASE alter_survive_db  ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 5,
                             num_voters = 5,
                             constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                             voter_constraints = '{+region=ca-central-1: 2}',
                             lease_preferences = '[[+region=ca-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE t_global
----
TABLE t_global  ALTER TABLE t_global CONFIGURE ZONE USING
                  range_min_bytes = 134217728,
                  range_max_bytes = 536870912,
                  gc.ttlseconds = 14400,
                  global_reads = true,
                  num_replicas = 5,
                  num_voters = 5,
                  constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                  voter_constraints = '{+region=ca-central-1: 2}',
                  lease_preferences = '[[+region=ca-central-1]]'

statement ok
alter database alter_survive_db survive zone failure

query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name                                 owner  primary_region  secondary_region  regions                                  survival_goal
alter_primary_region_db                       root   ap-southeast-2  ·                 {ap-southeast-2,ca-central-1}            zone
alter_survive_db                              root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
alter_test_db                                 root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
defaultdb                                     root   NULL            NULL              {}                                       NULL
multi_region_test_db                          root   ca-central-1    ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_explicit_primary_region_db  root   ap-southeast-2  ·                 {ap-southeast-2,ca-central-1,us-east-1}  region
multi_region_test_survive_zone_failure_db     root   us-east-1       ·                 {ap-southeast-2,ca-central-1,us-east-1}  zone
new_db                                        root   NULL            NULL              {}                                       NULL
postgres                                      root   NULL            NULL              {}                                       NULL
region_test_db                                root   ap-southeast-2  ·                 {ap-southeast-2}                         zone
system                                        node   NULL            NULL              {}                                       NULL
test                                          root   NULL            NULL              {}                                       NULL

query TT
SHOW ZONE CONFIGURATION FOR DATABASE alter_survive_db
----
DATABASE alter_survive_db  ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 5,
                             num_voters = 3,
                             constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                             voter_constraints = '[+region=ca-central-1]',
                             lease_preferences = '[[+region=ca-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE t_no_locality
----
DATABASE alter_survive_db  ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 5,
                             num_voters = 3,
                             constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                             voter_constraints = '[+region=ca-central-1]',
                             lease_preferences = '[[+region=ca-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE t_global
----
TABLE t_global  ALTER TABLE t_global CONFIGURE ZONE USING
                  range_min_bytes = 134217728,
                  range_max_bytes = 536870912,
                  gc.ttlseconds = 14400,
                  global_reads = true,
                  num_replicas = 5,
                  num_voters = 3,
                  constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                  voter_constraints = '[+region=ca-central-1]',
                  lease_preferences = '[[+region=ca-central-1]]'

statement ok
CREATE DATABASE no_initial_region;
USE no_initial_region

statement ok
CREATE TABLE table_with_partition_by (
  id INT PRIMARY KEY
) PARTITION BY LIST (id) (
  PARTITION "one" VALUES IN (1)
)

statement error cannot convert database no_initial_region to a multi-region database\nDETAIL: cannot convert table table_with_partition_by to a multi-region table as it is partitioned
ALTER DATABASE no_initial_region SET PRIMARY REGION "us-east-1"

statement ok
DROP TABLE table_with_partition_by;
CREATE TABLE idx_with_partition_by (
  id INT PRIMARY KEY,
  a INT,
  INDEX(a) PARTITION BY LIST (a) (
    PARTITION "one" VALUES IN (1)
  )
)

statement error cannot convert database no_initial_region to a multi-region database\nDETAIL: cannot convert table idx_with_partition_by to a multi-region table as it has index/constraint idx_with_partition_by_a_idx with partitioning
ALTER DATABASE no_initial_region SET PRIMARY REGION "us-east-1"

statement ok
DROP TABLE idx_with_partition_by;
CREATE TABLE no_initial_region.t(k INT)

statement ok
ALTER DATABASE no_initial_region PRIMARY REGION "us-east-1"

query TT
SHOW ZONE CONFIGURATION FOR TABLE no_initial_region.t
----
DATABASE no_initial_region  ALTER DATABASE no_initial_region CONFIGURE ZONE USING
                              range_min_bytes = 134217728,
                              range_max_bytes = 536870912,
                              gc.ttlseconds = 14400,
                              num_replicas = 3,
                              num_voters = 3,
                              constraints = '{+region=us-east-1: 1}',
                              voter_constraints = '[+region=us-east-1]',
                              lease_preferences = '[[+region=us-east-1]]'

query T
SELECT create_statement FROM [SHOW CREATE TABLE no_initial_region.t]
----
CREATE TABLE public.t (
                                                k INT8 NULL,
                                                rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
                                                CONSTRAINT t_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

statement ok
CREATE DATABASE non_multi_region_db

statement error pq: database has no regions to drop
ALTER DATABASE non_multi_region_db DROP REGION "ca-central-1"

query T noticetrace
ALTER DATABASE non_multi_region_db DROP REGION IF EXISTS "ca-central-1"
----
NOTICE: region "ca-central-1" is not defined on the database; skipping

statement ok
CREATE DATABASE drop_region_db PRIMARY REGION "ca-central-1" REGIONS "ap-southeast-2", "us-east-1";
USE drop_region_db

statement error pgcode 42P12 cannot drop region "ca-central-1"\nHINT: You must designate another region as the primary region using ALTER DATABASE drop_region_db PRIMARY REGION <region name> or remove all other regions before attempting to drop region "ca-central-1"
ALTER DATABASE drop_region_db DROP REGION "ca-central-1"

query T noticetrace
ALTER DATABASE drop_region_db DROP REGION IF EXISTS "us-east-1"
----

# Ensure that events are generated for dropping the region
query IT
SELECT "reportingID", info::JSONB - 'Timestamp' - 'DescriptorID'
FROM system.eventlog
WHERE "eventType" = 'alter_database_drop_region'
----
1  {"DatabaseName": "drop_region_db", "EventType": "alter_database_drop_region", "RegionName": "\"us-east-1\"", "Statement": "ALTER DATABASE drop_region_db DROP REGION IF EXISTS \"us-east-1\"", "Tag": "ALTER DATABASE", "User": "root"}

query T noticetrace
ALTER DATABASE drop_region_db DROP REGION IF EXISTS "us-east-1"
----
NOTICE: region "us-east-1" is not defined on the database; skipping

statement error pgcode 42704 region "non-existent-region" has not been added to the database
ALTER DATABASE drop_region_db DROP REGION "non-existent-region"

query TTBBT colnames,rowsort
SHOW REGIONS FROM DATABASE drop_region_db
----
database        region          primary  secondary  zones
drop_region_db  ca-central-1    true     false      {ca-az1,ca-az2,ca-az3}
drop_region_db  ap-southeast-2  false    false      {ap-az1,ap-az2,ap-az3}

query TTTT colnames
SHOW ENUMS FROM drop_region_db
----
schema  name                  values                         owner
public  crdb_internal_region  {ap-southeast-2,ca-central-1}  root

statement error pq: relation "t" \([0-9]+\): invalid locality config: region "us-east-1" has not been added to database "drop_region_db"
CREATE TABLE drop_region_db.public.t(a int) LOCALITY REGIONAL BY TABLE IN "us-east-1"

statement ok
CREATE TABLE regional_by_table () LOCALITY REGIONAL BY TABLE

statement ok
CREATE TABLE drop_region_db.public.t(a int) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

statement ok
CREATE TABLE global_table () LOCALITY GLOBAL

statement error pq: could not remove enum value "ap-southeast-2" as it is the home region for table "t"
ALTER DATABASE drop_region_db DROP REGION "ap-southeast-2"

statement ok
DROP TABLE drop_region_db.public.t

statement ok
ALTER DATABASE drop_region_db DROP REGION "ap-southeast-2"

statement ok
ALTER DATABASE drop_region_db DROP REGION "ca-central-1"

query TT colnames,rowsort
SELECT table_name, locality FROM [SHOW TABLES]
----
table_name         locality
regional_by_table  NULL
global_table       NULL

query TT
SHOW ZONE CONFIGURATION FOR TABLE global_table
----
RANGE default  ALTER RANGE default CONFIGURE ZONE USING
                 range_min_bytes = 134217728,
                 range_max_bytes = 536870912,
                 gc.ttlseconds = 14400,
                 num_replicas = 3,
                 constraints = '[]',
                 lease_preferences = '[]'

# Test a table that is implicitly homed in the primary region because it was
# created before the first region was added to the multi-region DB.
statement ok
CREATE DATABASE start_off_non_multi_region;

statement ok
CREATE TABLE start_off_non_multi_region.public.t(a INT);

statement ok
ALTER DATABASE start_off_non_multi_region PRIMARY REGION "ca-central-1";

statement ok
ALTER DATABASE start_off_non_multi_region ADD REGION "ap-southeast-2"

statement error pgcode 42P12 cannot drop region "ca-central-1"\nHINT: You must designate another region as the primary region using ALTER DATABASE start_off_non_multi_region PRIMARY REGION <region name> or remove all other regions before attempting to drop region "ca-central-1"
ALTER DATABASE start_off_non_multi_region DROP REGION "ca-central-1"

statement ok
ALTER DATABASE start_off_non_multi_region PRIMARY REGION "ap-southeast-2"

# Ensure that the table t does not disallow us from dropping the old primary region,
# even though it was homed there before the primary region was switched.
statement ok
ALTER DATABASE start_off_non_multi_region DROP REGION "ca-central-1"

# Test drops in a transaction.
statement ok
CREATE DATABASE  txn_database_drop_regions PRIMARY REGION "ca-central-1" REGIONS "ap-southeast-2", "us-east-1"

query TTBBT colnames,rowsort
SHOW REGIONS FROM DATABASE txn_database_drop_regions
----
database                   region          primary  secondary  zones
txn_database_drop_regions  ca-central-1    true     false      {ca-az1,ca-az2,ca-az3}
txn_database_drop_regions  ap-southeast-2  false    false      {ap-az1,ap-az2,ap-az3}
txn_database_drop_regions  us-east-1       false    false      {us-az1,us-az2,us-az3}

statement ok
BEGIN;
ALTER DATABASE txn_database_drop_regions DROP REGION "us-east-1";
ALTER DATABASE txn_database_drop_regions DROP REGION "ap-southeast-2";
COMMIT;

query TTBBT colnames
SHOW REGIONS FROM DATABASE txn_database_drop_regions
----
database                   region        primary  secondary  zones
txn_database_drop_regions  ca-central-1  true     false      {ca-az1,ca-az2,ca-az3}

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

statement ok
USE drop_regions_alter_patterns

statement ok
CREATE TABLE east() LOCALITY REGIONAL BY TABLE IN "us-east-1"

statement ok
CREATE TABLE southeast() LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

statement error pq: could not remove enum value "ap-southeast-2" as it is the home region for table "southeast"
ALTER DATABASE drop_regions_alter_patterns DROP REGION "ap-southeast-2"

statement ok
ALTER TABLE southeast SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

statement ok
ALTER DATABASE drop_regions_alter_patterns DROP REGION "ap-southeast-2"

statement error pq: could not remove enum value "us-east-1" as it is the home region for table "east"
ALTER DATABASE drop_regions_alter_patterns DROP REGION "us-east-1"

statement ok
ALTER TABLE east SET LOCALITY GLOBAL

statement ok
ALTER DATABASE drop_regions_alter_patterns DROP REGION "us-east-1"

# Drop the tables that held a dependency with the type descriptor before their
# alter statements. This ensures that the type descriptor dependency is
# successfully unlinked, as we expect validation to run after the test on the
# type descriptor.
statement ok
DROP TABLE east;
DROP TABLE southeast;

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

statement ok
CREATE TABLE drop_primary_regions_db.primary(k INT PRIMARY KEY) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION;
CREATE TABLE drop_primary_regions_db.east(k INT PRIMARY KEY) LOCALITY REGIONAL BY TABLE IN "us-east-1";
CREATE TABLE drop_primary_regions_db.ca(k INT PRIMARY KEY) LOCALITY REGIONAL BY TABLE IN "ca-central-1"

statement ok
ALTER DATABASE drop_primary_regions_db PRIMARY REGION "us-east-1"

statement error pq: could not remove enum value "ca-central-1" as it is the home region for table "ca"
ALTER DATABASE drop_primary_regions_db DROP REGION "ca-central-1"

statement ok
DROP TABLE drop_primary_regions_db.ca

# As the primary region has been changed to "us-east-1", this drop should succeed.
statement ok
ALTER DATABASE drop_primary_regions_db DROP REGION "ca-central-1"

# Cannot drop the primary region yet, as there are other regions in the db.
statement error pgcode 42P12 cannot drop region "us-east-1"\nHINT: You must designate another region as the primary region using ALTER DATABASE drop_primary_regions_db PRIMARY REGION <region name> or remove all other regions before attempting to drop region "us-east-1"
ALTER DATABASE drop_primary_regions_db DROP REGION "us-east-1"

statement ok
ALTER DATABASE drop_primary_regions_db DROP REGION "ap-southeast-2"

statement error pq: error removing primary region from database drop_primary_regions_db: cannot drop type "crdb_internal_region" because other objects \(\[drop_primary_regions_db.public.east\]\) still depend on it
ALTER DATABASE drop_primary_regions_db DROP REGION "us-east-1"

statement ok
DROP TABLE drop_primary_regions_db.east

# This should succeed, even though the table "primary" hasn't been dropped, as
# homing RTTs in primary regions does not block drops.
statement ok
ALTER DATABASE drop_primary_regions_db DROP REGION "us-east-1"

query TT
SHOW ZONE CONFIGURATION FOR DATABASE drop_primary_regions_db
----
RANGE default  ALTER RANGE default CONFIGURE ZONE USING
                 range_min_bytes = 134217728,
                 range_max_bytes = 536870912,
                 gc.ttlseconds = 14400,
                 num_replicas = 3,
                 constraints = '[]',
                 lease_preferences = '[]'

# Accessing the table should work without an issue even after the database is
# no longer multi-region.
statement ok
SELECT * FROM drop_primary_regions_db.primary

# Adding a region to the multi-region database should be possible as well,
# with no trace that this database was ever a multi-region db before.
statement ok
ALTER DATABASE drop_primary_regions_db PRIMARY REGION "ca-central-1"

query TT colnames
SHOW ZONE CONFIGURATION FOR TABLE drop_primary_regions_db.primary
----
target                            raw_config_sql
DATABASE drop_primary_regions_db  ALTER DATABASE drop_primary_regions_db CONFIGURE ZONE USING
                                    range_min_bytes = 134217728,
                                    range_max_bytes = 536870912,
                                    gc.ttlseconds = 14400,
                                    num_replicas = 3,
                                    num_voters = 3,
                                    constraints = '{+region=ca-central-1: 1}',
                                    voter_constraints = '[+region=ca-central-1]',
                                    lease_preferences = '[[+region=ca-central-1]]'



statement ok
ALTER TABLE drop_primary_regions_db.primary SET LOCALITY REGIONAL BY TABLE IN "ca-central-1"

statement error pq: error removing primary region from database drop_primary_regions_db: cannot drop type "crdb_internal_region" because other objects \(\[drop_primary_regions_db.public."primary"\]\) still depend on it
ALTER DATABASE drop_primary_regions_db DROP REGION "ca-central-1"

statement ok
ALTER TABLE drop_primary_regions_db.primary SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

statement ok
ALTER DATABASE drop_primary_regions_db DROP REGION "ca-central-1"

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

query TT
SHOW ZONE CONFIGURATION FOR DATABASE zone_config_drop_region
----
DATABASE zone_config_drop_region  ALTER DATABASE zone_config_drop_region CONFIGURE ZONE USING
                                    range_min_bytes = 134217728,
                                    range_max_bytes = 536870912,
                                    gc.ttlseconds = 14400,
                                    num_replicas = 5,
                                    num_voters = 3,
                                    constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                    voter_constraints = '[+region=ca-central-1]',
                                    lease_preferences = '[[+region=ca-central-1]]'

statement ok
ALTER DATABASE zone_config_drop_region DROP REGION "us-east-1"

query TT
SHOW ZONE CONFIGURATION FOR DATABASE zone_config_drop_region
----
DATABASE zone_config_drop_region  ALTER DATABASE zone_config_drop_region CONFIGURE ZONE USING
                                    range_min_bytes = 134217728,
                                    range_max_bytes = 536870912,
                                    gc.ttlseconds = 14400,
                                    num_replicas = 4,
                                    num_voters = 3,
                                    constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1}',
                                    voter_constraints = '[+region=ca-central-1]',
                                    lease_preferences = '[[+region=ca-central-1]]'

#
# Tests with views and sequences.
#

statement ok
CREATE DATABASE db_with_views_and_sequences;
USE db_with_views_and_sequences

statement ok
CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT);

statement ok
INSERT INTO t VALUES (1, 2, 3), (4, 5, 6);

statement ok
CREATE SEQUENCE s;

statement ok
CREATE VIEW v AS SELECT id, a, b FROM t;

statement ok
CREATE MATERIALIZED VIEW mat_view AS SELECT id, a, b FROM t

statement ok
ALTER DATABASE db_with_views_and_sequences SET PRIMARY REGION "ap-southeast-2"

query TT colnames
SELECT table_name, locality FROM [SHOW TABLES] ORDER BY 1, 2
----
table_name  locality
mat_view    GLOBAL
s           REGIONAL BY TABLE IN PRIMARY REGION
t           REGIONAL BY TABLE IN PRIMARY REGION
v           REGIONAL BY TABLE IN PRIMARY REGION

statement ok
CREATE SEQUENCE s2;

statement ok
CREATE VIEW v2 AS SELECT id, a, b FROM t;

statement ok
CREATE MATERIALIZED VIEW mat_view2 AS SELECT id, a, b FROM t

query TT colnames
SELECT table_name, locality FROM [SHOW TABLES] ORDER BY 1, 2
----
table_name  locality
mat_view    GLOBAL
mat_view2   GLOBAL
s           REGIONAL BY TABLE IN PRIMARY REGION
s2          REGIONAL BY TABLE IN PRIMARY REGION
t           REGIONAL BY TABLE IN PRIMARY REGION
v           REGIONAL BY TABLE IN PRIMARY REGION
v2          REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW ZONE CONFIGURATION FROM TABLE mat_view2
----
TABLE mat_view2  ALTER TABLE mat_view2 CONFIGURE ZONE USING
                   range_min_bytes = 134217728,
                   range_max_bytes = 536870912,
                   gc.ttlseconds = 14400,
                   global_reads = true,
                   num_replicas = 3,
                   num_voters = 3,
                   constraints = '{+region=ap-southeast-2: 1}',
                   voter_constraints = '[+region=ap-southeast-2]',
                   lease_preferences = '[[+region=ap-southeast-2]]'

statement ok
ALTER DATABASE db_with_views_and_sequences DROP REGION "ap-southeast-2"

query TT colnames,rowsort
SELECT table_name, locality FROM [SHOW TABLES]
----
table_name  locality
t           NULL
s           NULL
v           NULL
mat_view    NULL
s2          NULL
v2          NULL
mat_view2   NULL

subtest enum_name_clash

statement ok
CREATE DATABASE db_enum_name_clash;

statement ok
CREATE TYPE db_enum_name_clash.crdb_internal_region AS ENUM ();

statement error pq: type "db_enum_name_clash.public.crdb_internal_region" already exists\nHINT: object "crdb_internal_regions" must be renamed or dropped before adding the primary region\nDETAIL: multi-region databases employ an internal enum called crdb_internal_region to manage regions which conflicts with the existing object
ALTER DATABASE db_enum_name_clash SET PRIMARY REGION "us-east-1"

# Once the offending table is dropped, we should be able to add a region to the
# database.
statement ok
DROP TYPE db_enum_name_clash.crdb_internal_region;

statement ok
ALTER DATABASE db_enum_name_clash SET PRIMARY REGION "us-east-1"


# Verify we can create a view off of a regional by row table.
# Regression for #63191.
subtest regional_by_row_view

statement ok
CREATE DATABASE db;

statement ok
USE db;
ALTER DATABASE db PRIMARY REGION "us-east-1";

statement ok
CREATE TABLE kv (k INT PRIMARY KEY, v INT) LOCALITY REGIONAL BY ROW

statement ok
CREATE VIEW v AS SELECT v FROM kv

# Verify that we can use CREATE TABLE AS from a REGIONAL BY ROW table, and
# end up with something that resembles a REGIONAL BY ROW table.  Note that
# this is not simple, as we need to explicitly select the crdb_region
# column, and then convert that column to be generated and hidden.
subtest create_table_as_regional_by_row

statement ok
CREATE DATABASE "mr-create-table-as" PRIMARY REGION "ap-southeast-2" REGIONS "ap-southeast-2", "ca-central-1", "us-east-1";

statement ok
USE "mr-create-table-as"

statement ok
CREATE TABLE t (i int PRIMARY KEY) LOCALITY REGIONAL BY ROW

statement ok
INSERT INTO t VALUES (1),(2),(3)

statement ok
CREATE TABLE t_as AS SELECT i, crdb_region FROM t

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t_as]
----
create_statement
CREATE TABLE public.t_as (
  i INT8 NULL,
  crdb_region public.crdb_internal_region NULL,
  rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
  CONSTRAINT t_as_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION


statement error cannot use column crdb_region for REGIONAL BY ROW table as it may contain NULL values
ALTER TABLE t_as SET LOCALITY REGIONAL BY ROW AS crdb_region

statement ok
ALTER TABLE t_as ALTER COLUMN crdb_region SET NOT NULL

statement ok
ALTER TABLE t_as ALTER COLUMN crdb_region SET NOT VISIBLE

statement ok
ALTER TABLE t_as ALTER COLUMN crdb_region SET DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region

statement ok
ALTER TABLE t_as SET LOCALITY REGIONAL BY ROW AS crdb_region

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t_as]
----
create_statement
CREATE TABLE public.t_as (
  i INT8 NULL,
  crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
  rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
  CONSTRAINT t_as_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY ROW AS crdb_region

query TI colnames,rowsort
SELECT crdb_region, i FROM t_as
----
crdb_region     i
ap-southeast-2  1
ap-southeast-2  2
ap-southeast-2  3

statement ok
INSERT INTO t_as VALUES (4)

# Validate that hidden column works
query TI colnames,rowsort
SELECT crdb_region, i FROM t_as
----
crdb_region     i
ap-southeast-2  1
ap-southeast-2  2
ap-southeast-2  3
ap-southeast-2  4

statement ok
ALTER TABLE t_as ALTER COLUMN i SET NOT NULL

statement ok
ALTER TABLE t_as ALTER PRIMARY KEY USING COLUMNS (i)

statement ok
SET sql_safe_updates = false;
ALTER TABLE t_as DROP COLUMN rowid;
SET sql_safe_updates = true

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t_as]
----
create_statement
CREATE TABLE public.t_as (
  i INT8 NOT NULL,
  crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
  CONSTRAINT t_as_pkey PRIMARY KEY (i ASC)
) LOCALITY REGIONAL BY ROW AS crdb_region

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t]
----
create_statement
CREATE TABLE public.t (
  i INT8 NOT NULL,
  crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
  CONSTRAINT t_pkey PRIMARY KEY (i ASC)
) LOCALITY REGIONAL BY ROW

# Declare victory, as this is the closest we can get.
statement ok
DROP TABLE t;
DROP TABLE t_as

statement ok
SET sql_safe_updates = false;
DROP DATABASE "mr-create-table-as";
SET sql_safe_updates = true

# Checks that DROP REGION works when index expressions exist on tables,
# which will create public inaccessible columns (#126549).
subtest drop_region_126549

statement ok
CREATE DATABASE  drop_region_126549 PRIMARY REGION "ca-central-1" REGIONS "ap-southeast-2", "us-east-1";
CREATE TABLE drop_region_126549.t1 (n string PRIMARY KEY, INDEX((lower(n)))) LOCALITY REGIONAL BY ROW;

statement ok
ALTER DATABASE drop_region_126549 DROP REGION "us-east-1";

subtest alter_type_mr

statement ok
USE region_test_db;

statement ok
CREATE TABLE chgme (C1 INT) LOCALITY REGIONAL BY ROW;

statement ok
INSERT INTO chgme VALUES (0),(1);

let $sql_safe_updates_state
SELECT value FROM information_schema.session_variables where variable='sql_safe_updates';

statement ok
SET sql_safe_updates = false;

statement ok
ALTER TABLE chgme ALTER COLUMN C1 SET DATA TYPE TEXT;

statement ok
SET sql_safe_updates = $sql_safe_updates_state;

query T
SELECT * FROM chgme ORDER BY C1;
----
0
1

statement ok
DROP TABLE chgme;

subtest end
