@@ -23,10 +23,14 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_operator(opid oid) RETURNS TEXT A
23
23
end if;
24
24
25
25
select nspname, oprname, oprleft, oprright
26
- from pg_operator inner join pg_namespace
26
+ from pg_catalog . pg_operator inner join pg_catalog . pg_namespace
27
27
on oprnamespace = pg_namespace .oid
28
28
where pg_operator .oid = opid
29
29
into r;
30
+
31
+ if r is null then
32
+ raise exception ' operator % does not exist' , opid;
33
+ end if;
30
34
31
35
if r .oprleft = 0 then
32
36
ltype := ' NONE' ;
@@ -53,7 +57,7 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_type(typid oid) RETURNS TEXT AS $
53
57
54
58
BEGIN
55
59
select quote_ident(nspname) || ' .' || quote_ident(typname)
56
- from pg_type inner join pg_namespace
60
+ from pg_catalog . pg_type inner join pg_catalog . pg_namespace
57
61
on typnamespace = pg_namespace .oid
58
62
where pg_type .oid = typid
59
63
into result;
@@ -63,13 +67,13 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_type(typid oid) RETURNS TEXT AS $
63
67
$$ LANGUAGE plpgsql;
64
68
65
69
66
- CREATE FUNCTION to_schema_qualified_relname (reloid oid ) RETURNS TEXT AS $$
70
+ CREATE FUNCTION to_schema_qualified_relation (reloid oid ) RETURNS TEXT AS $$
67
71
DECLARE
68
72
result text ;
69
73
70
74
BEGIN
71
75
select quote_ident(nspname) || ' .' || quote_ident(relname)
72
- from pg_class inner join pg_namespace
76
+ from pg_catalog . pg_class inner join pg_catalog . pg_namespace
73
77
on relnamespace = pg_namespace .oid
74
78
where pg_class .oid = reloid
75
79
into result;
@@ -84,12 +88,13 @@ CREATE FUNCTION to_attname(relation text, colnum int2) RETURNS TEXT AS $$
84
88
result text ;
85
89
86
90
BEGIN
87
- select attname from pg_attribute
91
+ select attname
92
+ from pg_catalog .pg_attribute
88
93
where attrelid = relation::regclass and attnum = colnum
89
94
into result;
90
95
91
96
if result is null then
92
- raise notice ' attribute #% of relation % not found' ,
97
+ raise exception ' attribute #% of relation % not found' ,
93
98
colnum, quote_literal(relation);
94
99
end if;
95
100
@@ -103,12 +108,13 @@ CREATE FUNCTION to_attnum(relation text, col text) RETURNS INT2 AS $$
103
108
result int2;
104
109
105
110
BEGIN
106
- select attnum from pg_attribute
111
+ select attnum
112
+ from pg_catalog .pg_attribute
107
113
where attrelid = relation::regclass and attname = col
108
114
into result;
109
115
110
116
if result is null then
111
- raise notice ' attribute % of relation % not found' ,
117
+ raise exception ' attribute % of relation % not found' ,
112
118
quote_literal(col), quote_literal(relation);
113
119
end if;
114
120
@@ -122,12 +128,13 @@ CREATE FUNCTION to_atttype(relation text, col text) RETURNS TEXT AS $$
122
128
result text ;
123
129
124
130
BEGIN
125
- select to_schema_qualified_type(atttypid) from pg_attribute
131
+ select to_schema_qualified_type(atttypid)
132
+ from pg_catalog .pg_attribute
126
133
where attrelid = relation::regclass and attname = col
127
134
into result;
128
135
129
136
if result is null then
130
- raise notice ' attribute % of relation % not found' ,
137
+ raise exception ' attribute % of relation % not found' ,
131
138
quote_literal(col), quote_literal(relation);
132
139
end if;
133
140
@@ -141,12 +148,13 @@ CREATE FUNCTION to_atttype(relation text, colnum int2) RETURNS TEXT AS $$
141
148
result text ;
142
149
143
150
BEGIN
144
- select to_schema_qualified_type(atttypid) from pg_attribute
151
+ select to_schema_qualified_type(atttypid)
152
+ from pg_catalog .pg_attribute
145
153
where attrelid = relation::regclass and attnum = colnum
146
154
into result;
147
155
148
156
if result is null then
149
- raise notice ' attribute #% of relation % not found' ,
157
+ raise exception ' attribute #% of relation % not found' ,
150
158
colnum, quote_literal(relation);
151
159
end if;
152
160
@@ -160,7 +168,8 @@ CREATE FUNCTION to_namespace(nsp text) RETURNS OID AS $$
160
168
result oid ;
161
169
162
170
BEGIN
163
- select oid from pg_namespace
171
+ select oid
172
+ from pg_catalog .pg_namespace
164
173
where nspname = nsp
165
174
into result;
166
175
@@ -174,7 +183,8 @@ CREATE FUNCTION get_namespace(relation oid) RETURNS OID AS $$
174
183
result oid ;
175
184
176
185
BEGIN
177
- select relnamespace from pg_class
186
+ select relnamespace
187
+ from pg_catalog .pg_class
178
188
where oid = relation
179
189
into result;
180
190
@@ -183,7 +193,7 @@ CREATE FUNCTION get_namespace(relation oid) RETURNS OID AS $$
183
193
$$ LANGUAGE plpgsql;
184
194
185
195
186
- CREATE FUNCTION dump_statistic () RETURNS SETOF TEXT AS $$
196
+ CREATE FUNCTION dump_statistic (relid oid ) RETURNS SETOF TEXT AS $$
187
197
DECLARE
188
198
result text ;
189
199
@@ -212,11 +222,12 @@ CREATE FUNCTION dump_statistic() RETURNS SETOF TEXT AS $$
212
222
213
223
BEGIN
214
224
for r in
215
- select * from pg_catalog .pg_statistic
216
- where get_namespace(starelid) != to_namespace(' pg_catalog' )
217
- and get_namespace(starelid) != to_namespace(' information_schema' ) loop
225
+ select * from pg_catalog .pg_statistic
226
+ where starelid = relid
227
+ and get_namespace(starelid) != to_namespace(' information_schema' )
228
+ and get_namespace(starelid) != to_namespace(' pg_catalog' ) loop
218
229
219
- relname := to_schema_qualified_relname (r .starelid );
230
+ relname := to_schema_qualified_relation (r .starelid );
220
231
attname := quote_literal(to_attname(relname, r .staattnum ));
221
232
atttype := quote_literal(to_atttype(relname, r .staattnum ));
222
233
relname := quote_literal(relname); -- redefine relname
@@ -226,7 +237,7 @@ CREATE FUNCTION dump_statistic() RETURNS SETOF TEXT AS $$
226
237
227
238
cmd := ' WITH upsert as ( ' ||
228
239
' UPDATE pg_catalog.pg_statistic SET %s ' ||
229
- ' WHERE to_schema_qualified_relname (starelid) = ' || relname || ' '
240
+ ' WHERE to_schema_qualified_relation (starelid) = ' || relname || ' '
230
241
' AND to_attname(' || relname || ' , staattnum) = ' || attname || ' '
231
242
' AND to_atttype(' || relname || ' , staattnum) = ' || atttype || ' '
232
243
' AND stainherit = ' || r .stainherit || ' ' ||
@@ -362,4 +373,68 @@ CREATE FUNCTION dump_statistic() RETURNS SETOF TEXT AS $$
362
373
363
374
return;
364
375
END;
365
- $$ LANGUAGE plpgsql;
376
+ $$ LANGUAGE plpgsql;
377
+
378
+
379
+ CREATE FUNCTION dump_statistic (schema_name text , table_name text ) RETURNS SETOF TEXT AS $$
380
+ DECLARE
381
+ qual_relname text ;
382
+ relid oid ;
383
+
384
+ BEGIN
385
+ qual_relname := quote_ident(schema_name) ||
386
+ ' .' || quote_ident(table_name);
387
+
388
+ return next dump_statistic(qual_relname::regclass);
389
+ return;
390
+
391
+ EXCEPTION
392
+ when invalid_schema_name then
393
+ raise exception ' schema % does not exist' ,
394
+ quote_literal(schema_name);
395
+ when undefined_table then
396
+ raise exception ' relation % does not exist' ,
397
+ quote_literal(qual_relname);
398
+ END;
399
+ $$ LANGUAGE plpgsql;
400
+
401
+
402
+ CREATE FUNCTION dump_statistic (schema_name text ) RETURNS SETOF TEXT AS $$
403
+ DECLARE
404
+ relid oid ;
405
+ i text ;
406
+
407
+ BEGIN
408
+ for relid in
409
+ select pg_class .oid
410
+ from pg_catalog .pg_namespace
411
+ inner join pg_catalog .pg_class
412
+ on relnamespace = pg_namespace .oid
413
+ where nspname = schema_name loop
414
+
415
+ for i in select dump_statistic(relid) loop
416
+ return next i;
417
+ end loop;
418
+ end loop;
419
+ END;
420
+ $$ LANGUAGE plpgsql;
421
+
422
+
423
+ CREATE FUNCTION dump_statistic () RETURNS SETOF TEXT AS $$
424
+ DECLARE
425
+ relid oid ;
426
+ i text ;
427
+
428
+ BEGIN
429
+ for relid in
430
+ select pg_class .oid
431
+ from pg_catalog .pg_namespace
432
+ inner join pg_catalog .pg_class
433
+ on relnamespace = pg_namespace .oid loop
434
+
435
+ for i in select dump_statistic(relid) loop
436
+ return next i;
437
+ end loop;
438
+ end loop;
439
+ END;
440
+ $$ LANGUAGE plpgsql;
0 commit comments