Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: improve pg sync latency by reducing # of queries (#2705)

Danny Xu53bbf3a7a170
diff --git a/plugin/db/pg/sync.go b/plugin/db/pg/sync.go
index 4e75d03235..5e092c606c 100644
--- a/plugin/db/pg/sync.go
+++ b/plugin/db/pg/sync.go
@@ -317,12 +317,15 @@ func getPgTables(txn *sql.Tx) ([]*tableSchema, error) {
}
var tables []*tableSchema
- query := "" +
- "SELECT tbl.schemaname, tbl.tablename, tbl.tableowner, " +
- "pg_table_size((quote_ident(tbl.schemaname) || '.' || quote_ident(tbl.tablename))::regclass), " +
- "pg_indexes_size((quote_ident(tbl.schemaname) || '.' || quote_ident(tbl.tablename))::regclass) " +
- "FROM pg_catalog.pg_tables tbl " +
- "WHERE tbl.schemaname NOT IN ('pg_catalog', 'information_schema');"
+ query := `
+ SELECT tbl.schemaname, tbl.tablename, tbl.tableowner,
+ pg_table_size(format('%s.%s', quote_ident(tbl.schemaname), quote_ident(tbl.tablename))::regclass),
+ pg_indexes_size(format('%s.%s', quote_ident(tbl.schemaname), quote_ident(tbl.tablename))::regclass),
+ GREATEST(pc.reltuples::bigint, 0::BIGINT) AS estimate,
+ obj_description(format('%s.%s', quote_ident(tbl.schemaname), quote_ident(tbl.tablename))::regclass) AS comment
+ FROM pg_catalog.pg_tables tbl
+ LEFT JOIN pg_class as pc ON pc.oid = format('%s.%s', quote_ident(tbl.schemaname), quote_ident(tbl.tablename))::regclass
+ WHERE tbl.schemaname NOT IN ('pg_catalog', 'information_schema');`
rows, err := txn.Query(query)
if err != nil {
return nil, err
@@ -332,8 +335,9 @@ func getPgTables(txn *sql.Tx) ([]*tableSchema, error) {
for rows.Next() {
var tbl tableSchema
var schemaname, tablename, tableowner string
- var tableSizeByte, indexSizeByte int64
- if err := rows.Scan(&schemaname, &tablename, &tableowner, &tableSizeByte, &indexSizeByte); err != nil {
+ var tableSizeByte, indexSizeByte, rowCountEstimate int64
+ var comment sql.NullString
+ if err := rows.Scan(&schemaname, &tablename, &tableowner, &tableSizeByte, &indexSizeByte, &rowCountEstimate, &comment); err != nil {
return nil, err
}
tbl.schemaName = schemaname
@@ -341,6 +345,10 @@ func getPgTables(txn *sql.Tx) ([]*tableSchema, error) {
tbl.tableowner = tableowner
tbl.tableSizeByte = tableSizeByte
tbl.indexSizeByte = indexSizeByte
+ tbl.rowCount = rowCountEstimate
+ if comment.Valid {
+ tbl.comment = comment.String
+ }
tables = append(tables, &tbl)
}
@@ -349,10 +357,6 @@ func getPgTables(txn *sql.Tx) ([]*tableSchema, error) {
}
for _, tbl := range tables {
- if err := getTable(txn, tbl); err != nil {
- return nil, errors.Wrapf(err, "failed to call getTable(%q, %q)", tbl.schemaName, tbl.name)
- }
-
key := fmt.Sprintf("%s.%s", tbl.schemaName, tbl.name)
tbl.constraints = constraints[key]
tbl.columns = columns[key]
@@ -360,40 +364,6 @@ func getPgTables(txn *sql.Tx) ([]*tableSchema, error) {
return tables, nil
}
-func getTable(txn *sql.Tx, tbl *tableSchema) error {
- countQuery := fmt.Sprintf(`SELECT GREATEST(reltuples::bigint, 0::BIGINT) AS estimate FROM pg_class WHERE oid = (quote_ident('%s') || '.' || quote_ident('%s'))::regclass;`, tbl.schemaName, tbl.name)
- rows, err := txn.Query(countQuery)
- if err != nil {
- return err
- }
- defer rows.Close()
-
- for rows.Next() {
- if err := rows.Scan(&tbl.rowCount); err != nil {
- return err
- }
- }
- if err := rows.Err(); err != nil {
- return err
- }
-
- commentQuery := "SELECT obj_description(format('%s.%s', quote_ident($1), quote_ident($2))::regclass);"
- crows, err := txn.Query(commentQuery, tbl.schemaName, tbl.name)
- if err != nil {
- return err
- }
- defer crows.Close()
-
- for crows.Next() {
- var comment sql.NullString
- if err := crows.Scan(&comment); err != nil {
- return err
- }
- tbl.comment = comment.String
- }
- return crows.Err()
-}
-
// getTableColumns gets the columns of a table.
func getTableColumns(txn *sql.Tx) (map[string][]*columnSchema, error) {
query := `
@@ -409,7 +379,7 @@ func getTableColumns(txn *sql.Tx) (map[string][]*columnSchema, error) {
cols.collation_name,
cols.udt_schema,
cols.udt_name,
- pg_catalog.col_description((quote_ident(table_schema) || '.' || quote_ident(table_name))::regclass, cols.ordinal_position::int) as column_comment
+ pg_catalog.col_description(format('%s.%s', quote_ident(table_schema), quote_ident(table_name))::regclass, cols.ordinal_position::int) as column_comment
FROM INFORMATION_SCHEMA.COLUMNS AS cols
WHERE cols.table_schema NOT IN ('pg_catalog', 'information_schema');`
rows, err := txn.Query(query)
@@ -560,7 +530,8 @@ func getIndices(txn *sql.Tx) ([]*indexSchema, error) {
AND constraint_name = idx.indexname
AND table_schema = idx.schemaname
AND table_name = idx.tablename
- AND constraint_type = 'PRIMARY KEY')
+ AND constraint_type = 'PRIMARY KEY') AS primary,
+ obj_description(format('%s.%s', quote_ident(idx.schemaname), quote_ident(idx.indexname))::regclass) AS comment
FROM pg_indexes AS idx WHERE idx.schemaname NOT IN ('pg_catalog', 'information_schema');`
var indices []*indexSchema
@@ -573,7 +544,8 @@ func getIndices(txn *sql.Tx) ([]*indexSchema, error) {
for rows.Next() {
var idx indexSchema
var primary sql.NullInt32
- if err := rows.Scan(&idx.schemaName, &idx.tableName, &idx.name, &idx.statement, &primary); err != nil {
+ var comment sql.NullString
+ if err := rows.Scan(&idx.schemaName, &idx.tableName, &idx.name, &idx.statement, &primary, &comment); err != nil {
return nil, err
}
idx.unique = strings.Contains(idx.statement, " UNIQUE INDEX ")
@@ -585,39 +557,18 @@ func getIndices(txn *sql.Tx) ([]*indexSchema, error) {
if primary.Valid && primary.Int32 == 1 {
idx.primary = true
}
+ if comment.Valid {
+ idx.comment = comment.String
+ }
indices = append(indices, &idx)
}
if err := rows.Err(); err != nil {
return nil, err
}
- for _, idx := range indices {
- if err = getIndex(txn, idx); err != nil {
- return nil, errors.Wrapf(err, "failed to call getIndex(%q, %q)", idx.schemaName, idx.name)
- }
- }
-
return indices, nil
}
-func getIndex(txn *sql.Tx, idx *indexSchema) error {
- commentQuery := "SELECT obj_description(format('%s.%s', quote_ident($1), quote_ident($2))::regclass);"
- rows, err := txn.Query(commentQuery, idx.schemaName, idx.name)
- if err != nil {
- return err
- }
- defer rows.Close()
-
- for rows.Next() {
- var comment sql.NullString
- if err := rows.Scan(&comment); err != nil {
- return err
- }
- idx.comment = comment.String
- }
- return rows.Err()
-}
-
func convertBoolFromYesNo(s string) (bool, error) {
switch s {
case "YES":