Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
75753: sql,server: provide an approximate creation time for indexes r=ajwerner a=ajwerner

This change plumbs a timestamp through to the index descriptor when it is
created. The timestamp used is based on the transaction start time. The
timestamp is then plumbed into the `crdb_internal.table_indexes` table and
exposed as a new NULL-able TIMESTAMP column, `created_at`.

Then, lastly, the timestamp is plumbed through to the status server via the
`TableIndexStatsRequest`.

Fixes #72626.

Release note (sql change): The database now records the approximate time when
an index was created it. This information is exposed via a new NULL-able
TIMESTAMP column, `created_at`, on `crdb_internal.table_indexes`.

76285: sql: do not show FAMILY if there is only one on SHOW CREATE TABLE r=rafiss a=otan

Release note (sql change): SHOW CREATE TABLE no longer shows the FAMILY
clause if there is only the PRIMARY family clause.

76343: sql: add response admission control to tableWriterBase.finalize r=sumeerbhola a=sumeerbhola

This was overlooked in the previous instrumentation that only
did response admission in tableWriterBase.flushAndStartNewBatch,
which is only exercised when there are multiple batches.

Release note: None

Co-authored-by: Andrew Werner <[email protected]>
Co-authored-by: Oliver Tan <[email protected]>
Co-authored-by: sumeerbhola <[email protected]>
  • Loading branch information
4 people committed Feb 10, 2022
4 parents 46972ea + b732f2d + 7f58596 + a8eab3f commit d10188f
Show file tree
Hide file tree
Showing 105 changed files with 700 additions and 788 deletions.
1 change: 1 addition & 0 deletions docs/generated/http/full.md
Original file line number Diff line number Diff line change
Expand Up @@ -4181,6 +4181,7 @@ Response object returned by TableIndexStatsResponse.
| index_name | [string](#cockroach.server.serverpb.TableIndexStatsResponse-string) | | index_name is the name of the index. | [reserved](#support-status) |
| index_type | [string](#cockroach.server.serverpb.TableIndexStatsResponse-string) | | index_type is the type of the index i.e. primary, secondary. | [reserved](#support-status) |
| create_statement | [string](#cockroach.server.serverpb.TableIndexStatsResponse-string) | | create_statement is the SQL statement that would re-create the current index if executed. | [reserved](#support-status) |
| created_at | [google.protobuf.Timestamp](#cockroach.server.serverpb.TableIndexStatsResponse-google.protobuf.Timestamp) | | CreatedAt is an approximate timestamp at which the index was created. Note that it may not always be populated. | [reserved](#support-status) |



Expand Down
18 changes: 7 additions & 11 deletions pkg/ccl/backupccl/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ ORDER BY object_type, object_name`, full)
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT tablea_pkey PRIMARY KEY (a ASC),
INDEX tablea_b_idx (b ASC),
FAMILY "primary" (a, b)
INDEX tablea_b_idx (b ASC)
)`
expectedCreateView := "CREATE VIEW viewa (\n\ta\n) AS SELECT a FROM data.public.tablea"
expectedCreateSeq := `CREATE SEQUENCE seqa MINVALUE 1 MAXVALUE 20 INCREMENT 2 START 1`
Expand Down Expand Up @@ -271,15 +270,13 @@ ORDER BY object_type, object_name`, full)
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC),
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES public.fksrc(a),
FAMILY "primary" (a, b)
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES public.fksrc(a)
)`
wantDiffDB := `CREATE TABLE fkreftable (
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC),
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES data.public.fksrc(a),
FAMILY "primary" (a, b)
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES data.public.fksrc(a)
)`

showBackupRows = sqlDBRestore.QueryStr(t, fmt.Sprintf(`SELECT create_statement FROM [SHOW BACKUP SCHEMAS '%s'] WHERE object_type='table'`, includedFK))
Expand All @@ -303,8 +300,7 @@ ORDER BY object_type, object_name`, full)
want := `CREATE TABLE fkreftable (
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC),
FAMILY "primary" (a, b)
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC)
)`

showBackupRows = sqlDBRestore.QueryStr(t, fmt.Sprintf(`SELECT create_statement FROM [SHOW BACKUP SCHEMAS '%s'] WHERE object_type='table'`, missingFK))
Expand Down Expand Up @@ -607,15 +603,15 @@ func showUpgradedForeignKeysTest(exportDir string) func(t *testing.T) {
for _, tc := range []testCase{
{
"circular",
"CONSTRAINT self_fk FOREIGN KEY \\(selfid\\) REFERENCES public\\.circular\\(selfid\\) NOT VALID,",
"CONSTRAINT self_fk FOREIGN KEY \\(selfid\\) REFERENCES public\\.circular\\(selfid\\) NOT VALID",
},
{
"child",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\),",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\)",
},
{
"child_pk",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\),",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\)",
},
} {
results := sqlDB.QueryStr(t, `
Expand Down
3 changes: 1 addition & 2 deletions pkg/ccl/backupccl/testdata/backup-restore/virtual-columns
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ d.public.tab CREATE TABLE public.tab (
v INT8 NULL AS (a + b) VIRTUAL,
CONSTRAINT tab_pkey PRIMARY KEY (k ASC),
INDEX tab_v_idx (v ASC),
UNIQUE INDEX tab_a_b_key (a ASC, b ASC) WHERE v > 0:::INT8,
FAMILY "primary" (k, a, b)
UNIQUE INDEX tab_a_b_key (a ASC, b ASC) WHERE v > 0:::INT8
)
36 changes: 12 additions & 24 deletions pkg/ccl/importccl/import_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,14 +877,12 @@ END;
k INT8 NULL,
CONSTRAINT a_pkey PRIMARY KEY (i ASC),
CONSTRAINT a_i_fkey FOREIGN KEY (i) REFERENCES public.b(j) NOT VALID,
CONSTRAINT a_k_fkey FOREIGN KEY (k) REFERENCES public.a(i) NOT VALID,
FAMILY "primary" (i, k)
CONSTRAINT a_k_fkey FOREIGN KEY (k) REFERENCES public.a(i) NOT VALID
)`}, {
`CREATE TABLE public.b (
j INT8 NOT NULL,
CONSTRAINT b_pkey PRIMARY KEY (j ASC),
CONSTRAINT b_j_fkey FOREIGN KEY (j) REFERENCES public.a(i) NOT VALID,
FAMILY "primary" (j)
CONSTRAINT b_j_fkey FOREIGN KEY (j) REFERENCES public.a(i) NOT VALID
)`,
}},

Expand Down Expand Up @@ -996,8 +994,7 @@ END;
`CREATE TABLE public.t (
a INT8 NOT NULL,
b INT8 NOT NULL,
CONSTRAINT t_pkey PRIMARY KEY (a ASC),
FAMILY "primary" (a, b)
CONSTRAINT t_pkey PRIMARY KEY (a ASC)
)`,
},
},
Expand All @@ -1019,8 +1016,7 @@ END;
a INT8 NOT NULL,
b INT8 NOT VISIBLE NULL,
c INT8 NULL,
CONSTRAINT t_pkey PRIMARY KEY (a ASC),
FAMILY "primary" (a, b, c)
CONSTRAINT t_pkey PRIMARY KEY (a ASC)
)`,
},
},
Expand All @@ -1040,8 +1036,7 @@ END;
`CREATE TABLE public.t (
a INT8 NOT NULL,
b INT8 NULL DEFAULT 8:::INT8,
CONSTRAINT t_pkey PRIMARY KEY (a ASC),
FAMILY "primary" (a, b)
CONSTRAINT t_pkey PRIMARY KEY (a ASC)
)`,
},
},
Expand Down Expand Up @@ -1470,8 +1465,7 @@ END
const (
testPgdumpCreateCities = `CREATE TABLE public.cities (
city VARCHAR(80) NOT NULL,
CONSTRAINT cities_pkey PRIMARY KEY (city ASC),
FAMILY "primary" (city)
CONSTRAINT cities_pkey PRIMARY KEY (city ASC)
)`
testPgdumpCreateWeather = `CREATE TABLE public.weather (
city VARCHAR(80) NULL,
Expand All @@ -1481,8 +1475,7 @@ const (
date DATE NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT weather_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT weather_city_fkey FOREIGN KEY (city) REFERENCES public.cities(city) NOT VALID,
FAMILY "primary" (city, temp_lo, temp_hi, prcp, date, rowid)
CONSTRAINT weather_city_fkey FOREIGN KEY (city) REFERENCES public.cities(city) NOT VALID
)`
testPgdumpFk = `
CREATE TABLE public.cities (
Expand Down Expand Up @@ -5479,8 +5472,7 @@ func TestImportPgDump(t *testing.T) {
b BYTES NULL,
CONSTRAINT simple_pkey PRIMARY KEY (i ASC),
UNIQUE INDEX simple_b_s_idx (b ASC, s ASC),
INDEX simple_s_idx (s ASC),
FAMILY "primary" (i, s, b)
INDEX simple_s_idx (s ASC)
)`,
}})

Expand Down Expand Up @@ -5518,8 +5510,7 @@ func TestImportPgDump(t *testing.T) {
"second", `CREATE TABLE public.second (
i INT8 NOT NULL,
s STRING NULL,
CONSTRAINT second_pkey PRIMARY KEY (i ASC),
FAMILY "primary" (i, s)
CONSTRAINT second_pkey PRIMARY KEY (i ASC)
)`,
}})
res := sqlDB.QueryStr(t, "SELECT * FROM second ORDER BY i")
Expand All @@ -5545,8 +5536,7 @@ func TestImportPgDump(t *testing.T) {
a INT8 NULL DEFAULT nextval('public.a_seq'::REGCLASS),
b INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT seqtable_pkey PRIMARY KEY (rowid ASC),
FAMILY "primary" (a, b, rowid)
CONSTRAINT seqtable_pkey PRIMARY KEY (rowid ASC)
)`,
}})
sqlDB.CheckQueryResults(t, `SHOW CREATE SEQUENCE a_seq`, [][]string{{
Expand Down Expand Up @@ -6198,16 +6188,14 @@ func TestImportCockroachDump(t *testing.T) {
i INT8 NOT NULL,
t STRING NULL,
CONSTRAINT "primary" PRIMARY KEY (i ASC),
INDEX t_t_idx (t ASC),
FAMILY "primary" (i, t)
INDEX t_t_idx (t ASC)
)`},
})
sqlDB.CheckQueryResults(t, "SHOW CREATE TABLE a", [][]string{
{"a", `CREATE TABLE public.a (
i INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (i ASC),
CONSTRAINT fk_i_ref_t FOREIGN KEY (i) REFERENCES public.t(i) NOT VALID,
FAMILY "primary" (i)
CONSTRAINT fk_i_ref_t FOREIGN KEY (i) REFERENCES public.t(i) NOT VALID
)`},
})
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/ccl/importccl/import_table_creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
Expand Down Expand Up @@ -168,6 +169,7 @@ func MakeSimpleTableDescriptor(
Regions: makeImportRegionOperator(""),
SessionDataStack: sessiondata.NewStack(&sessiondata.SessionData{}),
ClientNoticeSender: &faketreeeval.DummyClientNoticeSender{},
TxnTimestamp: timeutil.Unix(0, walltime),
Settings: st,
}
affected := make(map[descpb.ID]*tabledesc.Mutable)
Expand Down
6 changes: 2 additions & 4 deletions pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,7 @@ SHOW CREATE TABLE regional_by_table_no_region
regional_by_table_no_region CREATE TABLE public.regional_by_table_no_region (
i INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (rowid ASC),
FAMILY "primary" (i, rowid)
CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
Expand Down Expand Up @@ -1273,8 +1272,7 @@ SHOW CREATE TABLE regional_by_table_no_region
regional_by_table_no_region CREATE TABLE public.regional_by_table_no_region (
i INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (rowid ASC),
FAMILY "primary" (i, rowid)
CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (rowid ASC)
) LOCALITY GLOBAL

query TTT
Expand Down
6 changes: 2 additions & 4 deletions pkg/ccl/logictestccl/testdata/logic_test/auto_rehoming
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ db.public.rbr CREATE TABLE public.rbr (
p INT8 NOT NULL,
s STRING NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region ON UPDATE rehome_row()::public.crdb_internal_region,
CONSTRAINT rbr_pkey PRIMARY KEY (p ASC),
FAMILY "primary" (p, s, crdb_region)
CONSTRAINT rbr_pkey PRIMARY KEY (p ASC)
) LOCALITY REGIONAL BY ROW


Expand All @@ -40,6 +39,5 @@ SHOW CREATE TABLE db.rbr_altered
db.public.rbr_altered CREATE TABLE public.rbr_altered (
p 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 ON UPDATE rehome_row()::public.crdb_internal_region,
CONSTRAINT rbr_altered_pkey PRIMARY KEY (p ASC),
FAMILY "primary" (p, crdb_region)
CONSTRAINT rbr_altered_pkey PRIMARY KEY (p ASC)
) LOCALITY REGIONAL BY ROW
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ SHOW CREATE TABLE p
----
p CREATE TABLE public.p (
a INT8 NOT NULL,
CONSTRAINT p_pkey PRIMARY KEY (a ASC),
FAMILY "primary" (a)
CONSTRAINT p_pkey PRIMARY KEY (a ASC)
) PARTITION BY LIST (a) (
PARTITION p1 VALUES IN ((1)),
PARTITION "P1" VALUES IN ((2)),
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ SELECT * FROM crdb_internal.table_columns WHERE descriptor_name = ''
----
descriptor_id descriptor_name column_id column_name column_type nullable default_expr hidden

query ITITTBBB colnames
query ITITTBBBT colnames
SELECT * FROM crdb_internal.table_indexes WHERE descriptor_name = ''
----
descriptor_id descriptor_name index_id index_name index_type is_unique is_inverted is_sharded
descriptor_id descriptor_name index_id index_name index_type is_unique is_inverted is_sharded created_at

query ITITTITTB colnames
SELECT * FROM crdb_internal.index_columns WHERE descriptor_name = ''
Expand Down
27 changes: 9 additions & 18 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ 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),
FAMILY "primary" (a, rowid)
CONSTRAINT regional_primary_region_table_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
Expand Down Expand Up @@ -421,8 +420,7 @@ SELECT create_statement FROM [SHOW CREATE TABLE regional_implicit_primary_region
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),
FAMILY "primary" (a, rowid)
CONSTRAINT regional_implicit_primary_region_table_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
Expand All @@ -447,8 +445,7 @@ 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),
FAMILY "primary" (a, rowid)
CONSTRAINT "regional_us-east-1_table_pkey" PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN "us-east-1"

query TT
Expand Down Expand Up @@ -476,8 +473,7 @@ 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),
FAMILY "primary" (a, rowid)
CONSTRAINT global_table_pkey PRIMARY KEY (rowid ASC)
) LOCALITY GLOBAL

query TT
Expand Down Expand Up @@ -1030,8 +1026,7 @@ 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),
FAMILY "primary" (k, rowid)
CONSTRAINT t_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

statement ok
Expand Down Expand Up @@ -1457,8 +1452,7 @@ 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),
FAMILY "primary" (i, crdb_region, rowid)
CONSTRAINT t_as_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION


Expand All @@ -1485,8 +1479,7 @@ 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),
FAMILY "primary" (i, crdb_region, rowid)
CONSTRAINT t_as_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY ROW AS crdb_region

query TI colnames
Expand Down Expand Up @@ -1528,8 +1521,7 @@ 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),
FAMILY "primary" (i, crdb_region)
CONSTRAINT t_as_pkey PRIMARY KEY (i ASC)
) LOCALITY REGIONAL BY ROW AS crdb_region

query T colnames
Expand All @@ -1539,8 +1531,7 @@ 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),
FAMILY "primary" (i, crdb_region)
CONSTRAINT t_pkey PRIMARY KEY (i ASC)
) LOCALITY REGIONAL BY ROW

# Declare victory, as this is the closest we can get.
Expand Down
3 changes: 1 addition & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region_backup
Original file line number Diff line number Diff line change
Expand Up @@ -1241,8 +1241,7 @@ SHOW CREATE TABLE non_mr_table
non_mr_table CREATE TABLE public.non_mr_table (
i INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT non_mr_table_pkey PRIMARY KEY (rowid ASC),
FAMILY "primary" (i, rowid)
CONSTRAINT non_mr_table_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,5 @@ team CREATE TABLE public.team (
name STRING NULL,
likes STRING[] NULL,
dislikes STRING[] NULL,
CONSTRAINT team_pkey PRIMARY KEY (id ASC),
FAMILY "primary" (id, name, likes, dislikes)
CONSTRAINT team_pkey PRIMARY KEY (id ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION
Loading

0 comments on commit d10188f

Please sign in to comment.