Skip to content

Commit

Permalink
GetTupleStatistics callback
Browse files Browse the repository at this point in the history
  • Loading branch information
RekGRpth committed May 24, 2024
1 parent 14c9a7e commit 9812a2c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
18 changes: 6 additions & 12 deletions backup/queries_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type TupleStatistic struct {
RelTuples float64
}

func getTupleStatisticsRows(connectionPool *dbconn.DBConn, tables []Table) (*sqlx.Rows, error) {
func GetTupleStatistics(connectionPool *dbconn.DBConn, tables []Table, processRow func(tupleStat *TupleStatistic)) {
tablenames := make([]string, 0)
for _, table := range tables {
tablenames = append(tablenames, table.FQN())
Expand All @@ -163,19 +163,13 @@ func getTupleStatisticsRows(connectionPool *dbconn.DBConn, tables []Table) (*sql
ORDER BY n.nspname, c.relname`,
SchemaFilterClause("n"), utils.SliceToQuotedString(tablenames))

return connectionPool.Query(query)
}

func GetTupleStatistics(connectionPool *dbconn.DBConn, tables []Table) map[uint32]TupleStatistic {
results, err := getTupleStatisticsRows(connectionPool, tables)
rows, err := connectionPool.Query(query)
gplog.FatalOnError(err)
stats := make(map[uint32]TupleStatistic)
for results.Next() {
for rows.Next() {
var tupleStat TupleStatistic
err = results.StructScan(&tupleStat)
err = rows.StructScan(&tupleStat)
gplog.FatalOnError(err)
stats[tupleStat.Oid] = tupleStat
processRow(&tupleStat)
}
gplog.FatalOnError(results.Err())
return stats
gplog.FatalOnError(rows.Err())
}
12 changes: 3 additions & 9 deletions backup/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,15 +777,9 @@ func backupTableStatistics(statisticsFile *utils.FileWithByteCount, tables []Tab
for _, table := range tables {
tablesMap[table.Oid] = table
}
tupleStats, err := getTupleStatisticsRows(connectionPool, tables)
gplog.FatalOnError(err)
for tupleStats.Next() {
var tupleStat TupleStatistic
err = tupleStats.StructScan(&tupleStat)
gplog.FatalOnError(err)
printTupleStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[tupleStat.Oid], tupleStat)
}
gplog.FatalOnError(tupleStats.Err())
GetTupleStatistics(connectionPool, tables, func(tupleStat *TupleStatistic) {
printTupleStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[tupleStat.Oid], *tupleStat)
})
attStats, err := getAttributeStatisticsRows(connectionPool, tables)
gplog.FatalOnError(err)
for attStats.Next() {
Expand Down
20 changes: 16 additions & 4 deletions integration/statistics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ var _ = Describe("backup integration tests", func() {
tables[0].Oid = oldTableOid

beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables)
beforeTupleStats := backup.GetTupleStatistics(connectionPool, tables)
beforeTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
beforeTupleStats[tupleStat.Oid] = *tupleStat
})
beforeTupleStat := beforeTupleStats[oldTableOid]

// Drop and recreate the table to clear the statistics
Expand All @@ -45,7 +48,10 @@ var _ = Describe("backup integration tests", func() {
newTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo", backup.TYPE_RELATION)
tables[0].Oid = newTableOid
afterAttStats := backup.GetAttributeStatistics(connectionPool, tables)
afterTupleStats := backup.GetTupleStatistics(connectionPool, tables)
afterTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
afterTupleStats[tupleStat.Oid] = *tupleStat
})
afterTupleStat := afterTupleStats[newTableOid]

oldAtts := beforeAttStats[oldTableOid]
Expand Down Expand Up @@ -75,7 +81,10 @@ var _ = Describe("backup integration tests", func() {
tables[0].Oid = oldTableOid

beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables)
beforeTupleStats := backup.GetTupleStatistics(connectionPool, tables)
beforeTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
beforeTupleStats[tupleStat.Oid] = *tupleStat
})
beforeTupleStat := beforeTupleStats[oldTableOid]

// Drop and recreate the table to clear the statistics
Expand All @@ -89,7 +98,10 @@ var _ = Describe("backup integration tests", func() {
newTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo''\"''''bar", backup.TYPE_RELATION)
tables[0].Oid = newTableOid
afterAttStats := backup.GetAttributeStatistics(connectionPool, tables)
afterTupleStats := backup.GetTupleStatistics(connectionPool, tables)
afterTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
afterTupleStats[tupleStat.Oid] = *tupleStat
})
afterTupleStat := afterTupleStats[newTableOid]

oldAtts := beforeAttStats[oldTableOid]
Expand Down
5 changes: 4 additions & 1 deletion integration/statistics_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ var _ = Describe("backup integration tests", func() {
})
Describe("GetTupleStatistics", func() {
It("returns tuple statistics for a table", func() {
tupleStats := backup.GetTupleStatistics(connectionPool, tables)
tupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
tupleStats[tupleStat.Oid] = *tupleStat
})
Expect(tupleStats).To(HaveLen(1))
tableTupleStats := tupleStats[tableOid]

Expand Down

0 comments on commit 9812a2c

Please sign in to comment.