diff --git a/backup/queries_statistics.go b/backup/queries_statistics.go index e6e773a53..58f82ed4b 100644 --- a/backup/queries_statistics.go +++ b/backup/queries_statistics.go @@ -11,7 +11,6 @@ import ( "github.com/greenplum-db/gp-common-go-libs/dbconn" "github.com/greenplum-db/gp-common-go-libs/gplog" "github.com/greenplum-db/gpbackup/utils" - "github.com/jmoiron/sqlx" "github.com/lib/pq" ) @@ -54,7 +53,7 @@ type AttributeStatistic struct { Values5 pq.StringArray `db:"stavalues5"` } -func getAttributeStatisticsRows(connectionPool *dbconn.DBConn, tables []Table) (*sqlx.Rows, error) { +func GetAttributeStatistics(connectionPool *dbconn.DBConn, tables []Table, processRow func(attStat *AttributeStatistic)) { inheritClause := "" statSlotClause := "" if connectionPool.Version.AtLeast("6") { @@ -120,21 +119,14 @@ func getAttributeStatisticsRows(connectionPool *dbconn.DBConn, tables []Table) ( inheritClause, statSlotClause, statCollationClause, SchemaFilterClause("n"), utils.SliceToQuotedString(tablenames)) - return connectionPool.Query(query) -} - -func GetAttributeStatistics(connectionPool *dbconn.DBConn, tables []Table) map[uint32][]AttributeStatistic { - results, err := getAttributeStatisticsRows(connectionPool, tables) - gplog.FatalOnError(err) - stats := make(map[uint32][]AttributeStatistic) - for results.Next() { + rows, err := connectionPool.Query(query) + for rows.Next() { var attStat AttributeStatistic - err = results.StructScan(&attStat) + err = rows.StructScan(&attStat) gplog.FatalOnError(err) - stats[attStat.Oid] = append(stats[attStat.Oid], attStat) + processRow(&attStat) } - gplog.FatalOnError(results.Err()) - return stats + gplog.FatalOnError(rows.Err()) } type TupleStatistic struct { diff --git a/backup/wrappers.go b/backup/wrappers.go index 49f102728..bedc0c5a3 100644 --- a/backup/wrappers.go +++ b/backup/wrappers.go @@ -780,15 +780,9 @@ func backupTableStatistics(statisticsFile *utils.FileWithByteCount, tables []Tab GetTupleStatistics(connectionPool, tables, func(tupleStat *TupleStatistic) { printTupleStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[tupleStat.Oid], *tupleStat) }) - attStats, err := getAttributeStatisticsRows(connectionPool, tables) - gplog.FatalOnError(err) - for attStats.Next() { - var attStat AttributeStatistic - err = attStats.StructScan(&attStat) - gplog.FatalOnError(err) - printAttributeStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[attStat.Oid], attStat) - } - gplog.FatalOnError(attStats.Err()) + GetAttributeStatistics(connectionPool, tables, func(attStat *AttributeStatistic) { + printAttributeStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[attStat.Oid], *attStat) + }) } func backupIncrementalMetadata() { diff --git a/integration/statistics_create_test.go b/integration/statistics_create_test.go index 6ec35f052..2611dd958 100644 --- a/integration/statistics_create_test.go +++ b/integration/statistics_create_test.go @@ -30,7 +30,10 @@ var _ = Describe("backup integration tests", func() { oldTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo", backup.TYPE_RELATION) tables[0].Oid = oldTableOid - beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables) + beforeAttStats := make(map[uint32][]backup.AttributeStatistic) + backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) { + beforeAttStats[attStat.Oid] = append(beforeAttStats[attStat.Oid], *attStat) + }) beforeTupleStats := make(map[uint32]backup.TupleStatistic) backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) { beforeTupleStats[tupleStat.Oid] = *tupleStat @@ -47,7 +50,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) + afterAttStats := make(map[uint32][]backup.AttributeStatistic) + backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) { + afterAttStats[attStat.Oid] = append(afterAttStats[attStat.Oid], *attStat) + }) afterTupleStats := make(map[uint32]backup.TupleStatistic) backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) { afterTupleStats[tupleStat.Oid] = *tupleStat @@ -80,7 +86,10 @@ var _ = Describe("backup integration tests", func() { oldTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo''\"''''bar", backup.TYPE_RELATION) tables[0].Oid = oldTableOid - beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables) + beforeAttStats := make(map[uint32][]backup.AttributeStatistic) + backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) { + beforeAttStats[attStat.Oid] = append(beforeAttStats[attStat.Oid], *attStat) + }) beforeTupleStats := make(map[uint32]backup.TupleStatistic) backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) { beforeTupleStats[tupleStat.Oid] = *tupleStat @@ -97,7 +106,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) + afterAttStats := make(map[uint32][]backup.AttributeStatistic) + backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) { + afterAttStats[attStat.Oid] = append(afterAttStats[attStat.Oid], *attStat) + }) afterTupleStats := make(map[uint32]backup.TupleStatistic) backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) { afterTupleStats[tupleStat.Oid] = *tupleStat diff --git a/integration/statistics_queries_test.go b/integration/statistics_queries_test.go index aa45501d4..ea1cf8eb3 100644 --- a/integration/statistics_queries_test.go +++ b/integration/statistics_queries_test.go @@ -31,7 +31,10 @@ var _ = Describe("backup integration tests", func() { }) Describe("GetAttributeStatistics", func() { It("returns attribute statistics for a table", func() { - attStats := backup.GetAttributeStatistics(connectionPool, tables) + attStats := make(map[uint32][]backup.AttributeStatistic) + backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) { + attStats[attStat.Oid] = append(attStats[attStat.Oid], *attStat) + }) Expect(attStats).To(HaveLen(1)) Expect(attStats[tableOid]).To(HaveLen(3)) tableAttStatsI := attStats[tableOid][0]