github.com/tuhaihe/gpbackup@v1.0.3/integration/statistics_create_test.go (about) 1 package integration 2 3 import ( 4 "github.com/tuhaihe/gp-common-go-libs/structmatcher" 5 "github.com/tuhaihe/gp-common-go-libs/testhelper" 6 "github.com/tuhaihe/gpbackup/backup" 7 "github.com/tuhaihe/gpbackup/testutils" 8 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("backup integration tests", func() { 14 BeforeEach(func() { 15 tocfile, backupfile = testutils.InitializeTestTOC(buffer, "predata") 16 }) 17 Describe("PrintStatisticsStatementsForTable", func() { 18 It("prints attribute and tuple statistics for a table", func() { 19 tables := []backup.Table{ 20 {Relation: backup.Relation{SchemaOid: 2200, Schema: "public", Name: "foo"}}, 21 } 22 23 // Create and ANALYZE a table to generate statistics 24 testhelper.AssertQueryRuns(connectionPool, "CREATE TABLE public.foo(i int, j text, k bool)") 25 defer testhelper.AssertQueryRuns(connectionPool, "DROP TABLE public.foo") 26 testhelper.AssertQueryRuns(connectionPool, "INSERT INTO public.foo VALUES (1, 'a', 't')") 27 testhelper.AssertQueryRuns(connectionPool, "INSERT INTO public.foo VALUES (2, 'b', 'f')") 28 testhelper.AssertQueryRuns(connectionPool, "ANALYZE public.foo") 29 30 oldTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo", backup.TYPE_RELATION) 31 tables[0].Oid = oldTableOid 32 33 beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables) 34 beforeTupleStats := backup.GetTupleStatistics(connectionPool, tables) 35 beforeTupleStat := beforeTupleStats[oldTableOid] 36 37 // Drop and recreate the table to clear the statistics 38 testhelper.AssertQueryRuns(connectionPool, "DROP TABLE public.foo") 39 testhelper.AssertQueryRuns(connectionPool, "CREATE TABLE public.foo(i int, j text, k bool)") 40 41 // Reload the retrieved statistics into the new table 42 backup.PrintStatisticsStatements(backupfile, tocfile, tables, beforeAttStats, beforeTupleStats) 43 testhelper.AssertQueryRuns(connectionPool, buffer.String()) 44 45 newTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo", backup.TYPE_RELATION) 46 tables[0].Oid = newTableOid 47 afterAttStats := backup.GetAttributeStatistics(connectionPool, tables) 48 afterTupleStats := backup.GetTupleStatistics(connectionPool, tables) 49 afterTupleStat := afterTupleStats[newTableOid] 50 51 oldAtts := beforeAttStats[oldTableOid] 52 newAtts := afterAttStats[newTableOid] 53 54 // Ensure the statistics match 55 Expect(afterTupleStats).To(HaveLen(len(beforeTupleStats))) 56 structmatcher.ExpectStructsToMatchExcluding(&beforeTupleStat, &afterTupleStat, "Oid") 57 Expect(oldAtts).To(HaveLen(3)) 58 Expect(newAtts).To(HaveLen(3)) 59 for i := range oldAtts { 60 structmatcher.ExpectStructsToMatchExcluding(&oldAtts[i], &newAtts[i], "Oid", "Relid") 61 } 62 }) 63 It("prints attribute and tuple statistics for a quoted table", func() { 64 tables := []backup.Table{ 65 {Relation: backup.Relation{SchemaOid: 2200, Schema: "public", Name: "\"foo'\"\"''bar\""}}, 66 } 67 68 // Create and ANALYZE the tables to generate statistics 69 testhelper.AssertQueryRuns(connectionPool, "CREATE TABLE public.\"foo'\"\"''bar\"(i int, j text, k bool)") 70 defer testhelper.AssertQueryRuns(connectionPool, "DROP TABLE public.\"foo'\"\"''bar\"") 71 testhelper.AssertQueryRuns(connectionPool, "INSERT INTO public.\"foo'\"\"''bar\" VALUES (1, 'a', 't')") 72 testhelper.AssertQueryRuns(connectionPool, "ANALYZE public.\"foo'\"\"''bar\"") 73 74 oldTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo''\"''''bar", backup.TYPE_RELATION) 75 tables[0].Oid = oldTableOid 76 77 beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables) 78 beforeTupleStats := backup.GetTupleStatistics(connectionPool, tables) 79 beforeTupleStat := beforeTupleStats[oldTableOid] 80 81 // Drop and recreate the table to clear the statistics 82 testhelper.AssertQueryRuns(connectionPool, "DROP TABLE public.\"foo'\"\"''bar\"") 83 testhelper.AssertQueryRuns(connectionPool, "CREATE TABLE public.\"foo'\"\"''bar\"(i int, j text, k bool)") 84 85 // Reload the retrieved statistics into the new table 86 backup.PrintStatisticsStatements(backupfile, tocfile, tables, beforeAttStats, beforeTupleStats) 87 testhelper.AssertQueryRuns(connectionPool, buffer.String()) 88 89 newTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo''\"''''bar", backup.TYPE_RELATION) 90 tables[0].Oid = newTableOid 91 afterAttStats := backup.GetAttributeStatistics(connectionPool, tables) 92 afterTupleStats := backup.GetTupleStatistics(connectionPool, tables) 93 afterTupleStat := afterTupleStats[newTableOid] 94 95 oldAtts := beforeAttStats[oldTableOid] 96 newAtts := afterAttStats[newTableOid] 97 98 // Ensure the statistics match 99 Expect(afterTupleStats).To(HaveLen(len(beforeTupleStats))) 100 structmatcher.ExpectStructsToMatchExcluding(&beforeTupleStat, &afterTupleStat, "Oid") 101 Expect(oldAtts).To(HaveLen(3)) 102 Expect(newAtts).To(HaveLen(3)) 103 for i := range oldAtts { 104 structmatcher.ExpectStructsToMatchExcluding(&oldAtts[i], &newAtts[i], "Oid", "Relid") 105 } 106 }) 107 }) 108 })