Skip to content

Commit a2d93a0

Browse files
Merge pull request #2644 from dolthub/nathan/systemTables
pg_catalog changes
2 parents 1d10426 + fb9c68f commit a2d93a0

7 files changed

Lines changed: 101 additions & 264 deletions

File tree

server/tables/pgcatalog/init.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,11 @@
1414

1515
package pgcatalog
1616

17-
import "os"
18-
1917
// PgCatalogName is a constant to the pg_catalog name.
2018
const PgCatalogName = "pg_catalog"
2119

22-
// includeSystemTables is a flag to determine whether to include system tables in the pg_catalog tables.
23-
var includeSystemTables = true
24-
2520
// Init initializes everything necessary for the pg_catalog tables.
2621
func Init() {
27-
InitIncludeSystemTables()
2822
InitPgAggregate()
2923
InitPgAm()
3024
InitPgAmop()
@@ -165,11 +159,3 @@ func Init() {
165159
InitPgUserMappings()
166160
InitPgViews()
167161
}
168-
169-
func InitIncludeSystemTables() {
170-
if _, ok := os.LookupEnv("REGRESSION_TESTING"); ok {
171-
// In CI regression tests, we exclude system tables to make them faster.
172-
// None of them rely on the presence of system tables.
173-
includeSystemTables = false
174-
}
175-
}

server/tables/pgcatalog/pg_attribute.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ import (
1818
"io"
1919
"math"
2020

21-
"github.com/cockroachdb/errors"
22-
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
23-
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
2421
"github.com/dolthub/go-mysql-server/sql"
2522

26-
"github.com/dolthub/doltgresql/core"
2723
"github.com/dolthub/doltgresql/core/id"
2824
"github.com/dolthub/doltgresql/server/functions"
2925
"github.com/dolthub/doltgresql/server/tables"
@@ -131,68 +127,6 @@ func cachePgAttributes(ctx *sql.Context, pgCatalogCache *pgCatalogCache) error {
131127
return err
132128
}
133129

134-
if includeSystemTables {
135-
_, root, err := core.GetRootFromContext(ctx)
136-
if err != nil {
137-
return err
138-
}
139-
140-
systemTables, err := resolve.GetGeneratedSystemTables(ctx, root)
141-
if err != nil {
142-
return err
143-
}
144-
145-
db := ctx.GetCurrentDatabase()
146-
for _, tblName := range systemTables {
147-
tbl, err := core.GetSqlTableFromContext(ctx, db, tblName)
148-
if err != nil {
149-
// Some of the system tables exist conditionally when accessed, so just skip them in this case
150-
if errors.Is(doltdb.ErrTableNotFound, err) {
151-
continue
152-
}
153-
return err
154-
}
155-
156-
schema := tbl.Schema(ctx)
157-
for i, col := range schema {
158-
typeOid := id.Null
159-
if doltgresType, ok := col.Type.(*pgtypes.DoltgresType); ok {
160-
typeOid = doltgresType.ID.AsId()
161-
} else {
162-
dt := pgtypes.FromGmsType(col.Type)
163-
typeOid = dt.ID.AsId()
164-
}
165-
166-
generated := ""
167-
if col.Generated != nil {
168-
generated = "s"
169-
}
170-
171-
dimensions := int16(0)
172-
if s, ok := col.Type.(sql.SetType); ok {
173-
dimensions = int16(s.NumberOfElements())
174-
}
175-
176-
hasDefault := col.Default != nil
177-
178-
attr := &pgAttribute{
179-
attrelid: id.NewTable(tblName.Schema, tblName.Name).AsId(),
180-
attrelidNative: id.Cache().ToOID(id.NewTable(tblName.Schema, tblName.Name).AsId()),
181-
attname: col.Name,
182-
atttypid: typeOid,
183-
attnum: int16(i + 1),
184-
attndims: dimensions,
185-
attnotnull: !col.Nullable,
186-
atthasdef: hasDefault,
187-
attgenerated: generated,
188-
}
189-
attrelidIdx.Add(attr)
190-
attrelidAttnameIdx.Add(attr)
191-
attributes = append(attributes, attr)
192-
}
193-
}
194-
}
195-
196130
pgCatalogCache.pgAttributes = &pgAttributeCache{
197131
attributes: attributes,
198132
attrelidIdx: attrelidIdx,

server/tables/pgcatalog/pg_catalog_cache.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package pgcatalog
1616

1717
import (
1818
"github.com/cockroachdb/errors"
19-
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
20-
2119
"github.com/dolthub/go-mysql-server/sql"
2220

2321
"github.com/dolthub/doltgresql/core"
@@ -75,8 +73,7 @@ type pgCatalogCache struct {
7573
viewSchemas []string
7674

7775
// pg_tables
78-
tables []sql.Table
79-
systemTables []doltdb.TableName
76+
tables []pgTableRow
8077
}
8178

8279
// pgClassCache holds cached data for the pg_class table, including two btree indexes for fast lookups by OID and

server/tables/pgcatalog/pg_class.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import (
1919
"io"
2020
"math"
2121

22-
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
22+
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
2323
"github.com/dolthub/go-mysql-server/sql"
2424

25-
"github.com/dolthub/doltgresql/core"
2625
"github.com/dolthub/doltgresql/core/id"
2726
"github.com/dolthub/doltgresql/server/functions"
2827
"github.com/dolthub/doltgresql/server/tables"
@@ -97,6 +96,7 @@ func cachePgClasses(ctx *sql.Context, pgCatalogCache *pgCatalogCache) error {
9796
kind: "i",
9897
schemaOid: schemaOid.AsId(),
9998
schemaOidNative: id.Cache().ToOID(schemaOid.AsId()),
99+
relType: id.Null,
100100
}
101101
nameIdx.Add(class)
102102
oidIdx.Add(class)
@@ -113,6 +113,7 @@ func cachePgClasses(ctx *sql.Context, pgCatalogCache *pgCatalogCache) error {
113113
kind: "r",
114114
schemaOid: schema.OID.AsId(),
115115
schemaOidNative: id.Cache().ToOID(schema.OID.AsId()),
116+
relType: id.NewType(table.OID.SchemaName(), table.OID.SchemaName()).AsId(),
116117
}
117118
nameIdx.Add(class)
118119
oidIdx.Add(class)
@@ -128,6 +129,7 @@ func cachePgClasses(ctx *sql.Context, pgCatalogCache *pgCatalogCache) error {
128129
kind: "v",
129130
schemaOid: schema.OID.AsId(),
130131
schemaOidNative: id.Cache().ToOID(schema.OID.AsId()),
132+
relType: id.NewType(view.OID.SchemaName(), view.OID.SchemaName()).AsId(),
131133
}
132134
nameIdx.Add(class)
133135
oidIdx.Add(class)
@@ -143,6 +145,7 @@ func cachePgClasses(ctx *sql.Context, pgCatalogCache *pgCatalogCache) error {
143145
kind: "S",
144146
schemaOid: schema.OID.AsId(),
145147
schemaOidNative: id.Cache().ToOID(schema.OID.AsId()),
148+
relType: id.Null,
146149
}
147150
nameIdx.Add(class)
148151
oidIdx.Add(class)
@@ -154,30 +157,6 @@ func cachePgClasses(ctx *sql.Context, pgCatalogCache *pgCatalogCache) error {
154157
return err
155158
}
156159

157-
if includeSystemTables {
158-
_, root, err := core.GetRootFromContext(ctx)
159-
if err != nil {
160-
return err
161-
}
162-
163-
systemTables, err := resolve.GetGeneratedSystemTables(ctx, root)
164-
if err != nil {
165-
return err
166-
}
167-
168-
for _, tblName := range systemTables {
169-
class := &pgClass{
170-
oid: id.NewTable(tblName.Schema, tblName.Name).AsId(),
171-
name: tblName.Name,
172-
schemaOid: id.NewNamespace(tblName.Schema).AsId(),
173-
kind: "r",
174-
}
175-
nameIdx.Add(class)
176-
oidIdx.Add(class)
177-
classes = append(classes, class)
178-
}
179-
}
180-
181160
pgCatalogCache.pgClasses = &pgClassCache{
182161
classes: classes,
183162
nameIdx: nameIdx,
@@ -192,6 +171,12 @@ func formatIndexName(idx sql.Index) string {
192171
if idx.ID() == "PRIMARY" {
193172
return fmt.Sprintf("%s_pkey", idx.Table())
194173
}
174+
175+
switch idx.(type) {
176+
case *index.BranchNameIndex, *index.CommitIndex:
177+
return fmt.Sprintf("%s_%s_key", idx.Table(), idx.ID())
178+
}
179+
195180
return idx.ID()
196181
// TODO: Unnamed indexes should have below format
197182
// return fmt.Sprintf("%s_%s_key", idx.Table(), idx.ID())
@@ -394,6 +379,7 @@ type pgClass struct {
394379
schemaOidNative uint32
395380
hasIndexes bool
396381
kind string // r = ordinary table, i = index, S = sequence, t = TOAST table, v = view, m = materialized view, c = composite type, f = foreign table, p = partitioned table, I = partitioned index
382+
relType id.Id
397383
}
398384

399385
// lessOid is a sort function for pgClass based on oid.
@@ -442,7 +428,7 @@ func pgClassToRow(class *pgClass) sql.Row {
442428
class.oid, // oid
443429
class.name, // relname
444430
class.schemaOid, // relnamespace
445-
id.Null, // reltype
431+
class.relType, // reltype
446432
id.Null, // reloftype
447433
id.Null, // relowner
448434
relam, // relam

server/tables/pgcatalog/pg_tables.go

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ package pgcatalog
1717
import (
1818
"io"
1919

20-
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
21-
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
2220
"github.com/dolthub/go-mysql-server/sql"
2321

24-
"github.com/dolthub/doltgresql/core"
2522
"github.com/dolthub/doltgresql/server/functions"
2623
"github.com/dolthub/doltgresql/server/tables"
2724
pgtypes "github.com/dolthub/doltgresql/server/types"
@@ -30,6 +27,12 @@ import (
3027
// PgTablesName is a constant to the pg_tables name.
3128
const PgTablesName = "pg_tables"
3229

30+
// pgTableRow stores the data needed for a row in pg_tables: the table data and the schema it exists in.
31+
type pgTableRow struct {
32+
Table sql.Table
33+
TableSchema string
34+
}
35+
3336
// InitPgTables handles registration of the pg_tables handler.
3437
func InitPgTables() {
3538
tables.AddHandler(PgCatalogName, PgTablesName, PgTablesHandler{})
@@ -46,47 +49,34 @@ func (p PgTablesHandler) Name() string {
4649
}
4750

4851
// RowIter implements the interface tables.Handler.
49-
func (p PgTablesHandler) RowIter(ctx *sql.Context, partition sql.Partition) (sql.RowIter, error) {
52+
func (p PgTablesHandler) RowIter(ctx *sql.Context, _ sql.Partition) (sql.RowIter, error) {
5053
// Use cached data from this process if it exists
5154
pgCatalogCache, err := getPgCatalogCache(ctx)
5255
if err != nil {
5356
return nil, err
5457
}
5558

5659
if pgCatalogCache.tables == nil {
57-
var tables []sql.Table
58-
var tableSchemas []string
59-
// TODO: This should include a few information_schema tables
60+
var tables []pgTableRow
61+
// TODO: This should include information_schema tables
62+
// TODO: However, information schema is currently incorrect for Doltgres, so we exclude it.
6063
err := functions.IterateCurrentDatabase(ctx, functions.Callbacks{
6164
Table: func(ctx *sql.Context, schema functions.ItemSchema, table functions.ItemTable) (cont bool, err error) {
62-
tables = append(tables, table.Item)
63-
tableSchemas = append(tableSchemas, schema.Item.SchemaName())
65+
if schema.Item.SchemaName() != sql.InformationSchemaDatabaseName {
66+
tables = append(tables, pgTableRow{table.Item, schema.Item.SchemaName()})
67+
}
6468
return true, nil
6569
},
6670
})
6771
if err != nil {
6872
return nil, err
6973
}
7074

71-
if includeSystemTables {
72-
_, root, err := core.GetRootFromContext(ctx)
73-
if err != nil {
74-
return nil, err
75-
}
76-
77-
systemTables, err := resolve.GetGeneratedSystemTables(ctx, root)
78-
if err != nil {
79-
return nil, err
80-
}
81-
pgCatalogCache.systemTables = systemTables
82-
}
83-
8475
pgCatalogCache.tables = tables
8576
}
8677

8778
return &pgTablesRowIter{
88-
userTables: pgCatalogCache.tables,
89-
systemTableNames: pgCatalogCache.systemTables,
79+
tables: pgCatalogCache.tables,
9080
}, nil
9181
}
9282

@@ -112,55 +102,37 @@ var pgTablesSchema = sql.Schema{
112102

113103
// pgTablesRowIter is the sql.RowIter for the pg_tables table.
114104
type pgTablesRowIter struct {
115-
// userTable are the set of user-defined tables
116-
userTables []sql.Table
117-
// systemTableNames is the names of all system tables
118-
systemTableNames []doltdb.TableName
119-
// idx is the current index in the iteration through both slices
105+
// tables are the set of tables and the name of the schema they belong in
106+
tables []pgTableRow
107+
// idx is the current index in the iteration through the above slice
120108
idx int
121109
}
122110

123111
var _ sql.RowIter = (*pgTablesRowIter)(nil)
124112

125113
// Next implements the interface sql.RowIter.
126114
func (iter *pgTablesRowIter) Next(ctx *sql.Context) (sql.Row, error) {
127-
if iter.idx >= len(iter.userTables)+len(iter.systemTableNames) {
115+
if iter.idx >= len(iter.tables) {
128116
return nil, io.EOF
129117
}
130118
defer func() {
131119
iter.idx++
132120
}()
133121

134-
var tableName string
135-
var hasIndexes bool
136-
var schema string
137-
138-
if iter.idx < len(iter.userTables) {
139-
table := iter.userTables[iter.idx]
122+
table := iter.tables[iter.idx].Table
123+
schema := iter.tables[iter.idx].TableSchema
124+
tableName := table.Name()
140125

141-
switch table := table.(type) {
142-
case sql.DatabaseSchemaTable:
143-
schema = table.DatabaseSchema().SchemaName()
144-
default:
145-
schema = "information_schema"
126+
var hasIndexes bool
127+
if it, ok := table.(sql.IndexAddressable); ok {
128+
idxs, err := it.GetIndexes(ctx)
129+
if err != nil {
130+
return nil, err
146131
}
147132

148-
tableName = table.Name()
149-
150-
if it, ok := table.(sql.IndexAddressable); ok {
151-
idxs, err := it.GetIndexes(ctx)
152-
if err != nil {
153-
return nil, err
154-
}
155-
156-
if len(idxs) > 0 {
157-
hasIndexes = true
158-
}
133+
if len(idxs) > 0 {
134+
hasIndexes = true
159135
}
160-
} else {
161-
tblName := iter.systemTableNames[iter.idx-len(iter.userTables)]
162-
tableName = tblName.Name
163-
schema = tblName.Schema
164136
}
165137

166138
return sql.Row{

0 commit comments

Comments
 (0)