@@ -17,11 +17,8 @@ package pgcatalog
1717import (
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.
3128const 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.
3437func 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.
114104type 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
123111var _ sql.RowIter = (* pgTablesRowIter )(nil )
124112
125113// Next implements the interface sql.RowIter.
126114func (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