|
| 1 | +-- Generic extended statistics support |
| 2 | +-- Ensure stats are dropped sanely |
| 3 | +CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER); |
| 4 | +CREATE STATISTICS ab1_a_b_stats ON (a, b) FROM ab1; |
| 5 | +DROP STATISTICS ab1_a_b_stats; |
| 6 | +CREATE SCHEMA regress_schema_2; |
| 7 | +CREATE STATISTICS regress_schema_2.ab1_a_b_stats ON (a, b) FROM ab1; |
| 8 | +DROP STATISTICS regress_schema_2.ab1_a_b_stats; |
| 9 | +-- Ensure statistics are dropped when columns are |
| 10 | +CREATE STATISTICS ab1_b_c_stats ON (b, c) FROM ab1; |
| 11 | +CREATE STATISTICS ab1_a_b_c_stats ON (a, b, c) FROM ab1; |
| 12 | +CREATE STATISTICS ab1_a_b_stats ON (a, b) FROM ab1; |
| 13 | +ALTER TABLE ab1 DROP COLUMN a; |
| 14 | +\d ab1 |
| 15 | + Table "public.ab1" |
| 16 | + Column | Type | Collation | Nullable | Default |
| 17 | +--------+---------+-----------+----------+--------- |
| 18 | + b | integer | | | |
| 19 | + c | integer | | | |
| 20 | +Statistics: |
| 21 | + "public.ab1_b_c_stats" WITH (ndistinct) ON (b, c) |
| 22 | + |
| 23 | +DROP TABLE ab1; |
| 24 | +-- Ensure things work sanely with SET STATISTICS 0 |
| 25 | +CREATE TABLE ab1 (a INTEGER, b INTEGER); |
| 26 | +ALTER TABLE ab1 ALTER a SET STATISTICS 0; |
| 27 | +INSERT INTO ab1 SELECT a, a%23 FROM generate_series(1, 1000) a; |
| 28 | +CREATE STATISTICS ab1_a_b_stats ON (a, b) FROM ab1; |
| 29 | +ANALYZE ab1; |
| 30 | +ERROR: extended statistics could not be collected for column "a" of relation public.ab1 |
| 31 | +HINT: Consider ALTER TABLE "public"."ab1" ALTER "a" SET STATISTICS -1 |
| 32 | +ALTER TABLE ab1 ALTER a SET STATISTICS -1; |
| 33 | +ANALYZE ab1; |
| 34 | +DROP TABLE ab1; |
| 35 | +-- n-distinct tests |
| 36 | +CREATE TABLE ndistinct ( |
| 37 | + filler1 TEXT, |
| 38 | + filler2 NUMERIC, |
| 39 | + a INT, |
| 40 | + b INT, |
| 41 | + filler3 DATE, |
| 42 | + c INT, |
| 43 | + d INT |
| 44 | +); |
| 45 | +-- unknown column |
| 46 | +CREATE STATISTICS s10 ON (unknown_column) FROM ndistinct; |
| 47 | +ERROR: column "unknown_column" referenced in statistics does not exist |
| 48 | +-- single column |
| 49 | +CREATE STATISTICS s10 ON (a) FROM ndistinct; |
| 50 | +ERROR: statistics require at least 2 columns |
| 51 | +-- single column, duplicated |
| 52 | +CREATE STATISTICS s10 ON (a,a) FROM ndistinct; |
| 53 | +ERROR: duplicate column name in statistics definition |
| 54 | +-- two columns, one duplicated |
| 55 | +CREATE STATISTICS s10 ON (a, a, b) FROM ndistinct; |
| 56 | +ERROR: duplicate column name in statistics definition |
| 57 | +-- correct command |
| 58 | +CREATE STATISTICS s10 ON (a, b, c) FROM ndistinct; |
| 59 | +-- perfectly correlated groups |
| 60 | +INSERT INTO ndistinct (a, b, c, filler1) |
| 61 | + SELECT i/100, i/100, i/100, cash_words(i::money) |
| 62 | + FROM generate_series(1,10000) s(i); |
| 63 | +ANALYZE ndistinct; |
| 64 | +SELECT staenabled, standistinct |
| 65 | + FROM pg_statistic_ext WHERE starelid = 'ndistinct'::regclass; |
| 66 | + staenabled | standistinct |
| 67 | +------------+------------------------------------------------------------------------------------------------ |
| 68 | + {d} | [{(b 3 4), 101.000000}, {(b 3 6), 101.000000}, {(b 4 6), 101.000000}, {(b 3 4 6), 101.000000}] |
| 69 | +(1 row) |
| 70 | + |
| 71 | +EXPLAIN (COSTS off) |
| 72 | + SELECT COUNT(*) FROM ndistinct GROUP BY a, b; |
| 73 | + QUERY PLAN |
| 74 | +----------------------------- |
| 75 | + HashAggregate |
| 76 | + Group Key: a, b |
| 77 | + -> Seq Scan on ndistinct |
| 78 | +(3 rows) |
| 79 | + |
| 80 | +EXPLAIN (COSTS off) |
| 81 | + SELECT COUNT(*) FROM ndistinct GROUP BY a, b, c; |
| 82 | + QUERY PLAN |
| 83 | +----------------------------- |
| 84 | + HashAggregate |
| 85 | + Group Key: a, b, c |
| 86 | + -> Seq Scan on ndistinct |
| 87 | +(3 rows) |
| 88 | + |
| 89 | +EXPLAIN (COSTS off) |
| 90 | + SELECT COUNT(*) FROM ndistinct GROUP BY a, b, c, d; |
| 91 | + QUERY PLAN |
| 92 | +----------------------------- |
| 93 | + HashAggregate |
| 94 | + Group Key: a, b, c, d |
| 95 | + -> Seq Scan on ndistinct |
| 96 | +(3 rows) |
| 97 | + |
| 98 | +TRUNCATE TABLE ndistinct; |
| 99 | +-- partially correlated groups |
| 100 | +INSERT INTO ndistinct (a, b, c) |
| 101 | + SELECT i/50, i/100, i/200 FROM generate_series(1,10000) s(i); |
| 102 | +ANALYZE ndistinct; |
| 103 | +SELECT staenabled, standistinct |
| 104 | + FROM pg_statistic_ext WHERE starelid = 'ndistinct'::regclass; |
| 105 | + staenabled | standistinct |
| 106 | +------------+------------------------------------------------------------------------------------------------ |
| 107 | + {d} | [{(b 3 4), 201.000000}, {(b 3 6), 201.000000}, {(b 4 6), 101.000000}, {(b 3 4 6), 201.000000}] |
| 108 | +(1 row) |
| 109 | + |
| 110 | +EXPLAIN |
| 111 | + SELECT COUNT(*) FROM ndistinct GROUP BY a, b; |
| 112 | + QUERY PLAN |
| 113 | +--------------------------------------------------------------------- |
| 114 | + HashAggregate (cost=225.00..227.01 rows=201 width=16) |
| 115 | + Group Key: a, b |
| 116 | + -> Seq Scan on ndistinct (cost=0.00..150.00 rows=10000 width=8) |
| 117 | +(3 rows) |
| 118 | + |
| 119 | +EXPLAIN |
| 120 | + SELECT COUNT(*) FROM ndistinct GROUP BY a, b, c; |
| 121 | + QUERY PLAN |
| 122 | +---------------------------------------------------------------------- |
| 123 | + HashAggregate (cost=250.00..252.01 rows=201 width=20) |
| 124 | + Group Key: a, b, c |
| 125 | + -> Seq Scan on ndistinct (cost=0.00..150.00 rows=10000 width=12) |
| 126 | +(3 rows) |
| 127 | + |
| 128 | +EXPLAIN |
| 129 | + SELECT COUNT(*) FROM ndistinct GROUP BY a, b, c, d; |
| 130 | + QUERY PLAN |
| 131 | +---------------------------------------------------------------------- |
| 132 | + HashAggregate (cost=275.00..285.00 rows=1000 width=24) |
| 133 | + Group Key: a, b, c, d |
| 134 | + -> Seq Scan on ndistinct (cost=0.00..150.00 rows=10000 width=16) |
| 135 | +(3 rows) |
| 136 | + |
| 137 | +EXPLAIN |
| 138 | + SELECT COUNT(*) FROM ndistinct GROUP BY b, c, d; |
| 139 | + QUERY PLAN |
| 140 | +---------------------------------------------------------------------- |
| 141 | + HashAggregate (cost=250.00..260.00 rows=1000 width=20) |
| 142 | + Group Key: b, c, d |
| 143 | + -> Seq Scan on ndistinct (cost=0.00..150.00 rows=10000 width=12) |
| 144 | +(3 rows) |
| 145 | + |
| 146 | +EXPLAIN |
| 147 | + SELECT COUNT(*) FROM ndistinct GROUP BY a, d; |
| 148 | + QUERY PLAN |
| 149 | +--------------------------------------------------------------------- |
| 150 | + HashAggregate (cost=225.00..235.00 rows=1000 width=16) |
| 151 | + Group Key: a, d |
| 152 | + -> Seq Scan on ndistinct (cost=0.00..150.00 rows=10000 width=8) |
| 153 | +(3 rows) |
| 154 | + |
| 155 | +DROP TABLE ndistinct; |
0 commit comments