@@ -23,10 +23,14 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_operator(opid oid) RETURNS TEXT A
2323end if;
2424
2525select nspname, oprname, oprleft, oprright
26- from pg_operatorinner join pg_namespace
26+ from pg_catalog . pg_operator inner join pg_catalog . pg_namespace
2727on oprnamespace= pg_namespace .oid
2828where pg_operator .oid = opid
2929into r;
30+
31+ if r isnull then
32+ raise exception' operator % does not exist' , opid;
33+ end if;
3034
3135ifr .oprleft = 0 then
3236ltype := ' NONE' ;
@@ -53,7 +57,7 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_type(typid oid) RETURNS TEXT AS $
5357
5458BEGIN
5559select quote_ident(nspname)|| ' .' || quote_ident(typname)
56- from pg_typeinner join pg_namespace
60+ from pg_catalog . pg_type inner join pg_catalog . pg_namespace
5761on typnamespace= pg_namespace .oid
5862where pg_type .oid = typid
5963into result;
@@ -63,13 +67,13 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_type(typid oid) RETURNS TEXT AS $
6367$$ LANGUAGE plpgsql;
6468
6569
66- CREATE FUNCTION to_schema_qualified_relname (reloidoid ) RETURNSTEXT AS $$
70+ CREATE FUNCTION to_schema_qualified_relation (reloidoid ) RETURNSTEXT AS $$
6771DECLARE
6872resulttext ;
6973
7074BEGIN
7175select quote_ident(nspname)|| ' .' || quote_ident(relname)
72- from pg_classinner join pg_namespace
76+ from pg_catalog . pg_class inner join pg_catalog . pg_namespace
7377on relnamespace= pg_namespace .oid
7478where pg_class .oid = reloid
7579into result;
@@ -84,12 +88,13 @@ CREATE FUNCTION to_attname(relation text, colnum int2) RETURNS TEXT AS $$
8488resulttext ;
8589
8690BEGIN
87- select attnamefrom pg_attribute
91+ select attname
92+ from pg_catalog .pg_attribute
8893where attrelid= relation::regclassand attnum= colnum
8994into result;
9095
9196if result isnull then
92- raisenotice ' attribute #% of relation % not found' ,
97+ raiseexception ' attribute #% of relation % not found' ,
9398colnum, quote_literal(relation);
9499end if;
95100
@@ -103,12 +108,13 @@ CREATE FUNCTION to_attnum(relation text, col text) RETURNS INT2 AS $$
103108result int2;
104109
105110BEGIN
106- select attnumfrom pg_attribute
111+ select attnum
112+ from pg_catalog .pg_attribute
107113where attrelid= relation::regclassand attname= col
108114into result;
109115
110116if result isnull then
111- raisenotice ' attribute % of relation % not found' ,
117+ raiseexception ' attribute % of relation % not found' ,
112118quote_literal(col), quote_literal(relation);
113119end if;
114120
@@ -122,12 +128,13 @@ CREATE FUNCTION to_atttype(relation text, col text) RETURNS TEXT AS $$
122128resulttext ;
123129
124130BEGIN
125- select to_schema_qualified_type(atttypid)from pg_attribute
131+ select to_schema_qualified_type(atttypid)
132+ from pg_catalog .pg_attribute
126133where attrelid= relation::regclassand attname= col
127134into result;
128135
129136if result isnull then
130- raisenotice ' attribute % of relation % not found' ,
137+ raiseexception ' attribute % of relation % not found' ,
131138quote_literal(col), quote_literal(relation);
132139end if;
133140
@@ -141,12 +148,13 @@ CREATE FUNCTION to_atttype(relation text, colnum int2) RETURNS TEXT AS $$
141148resulttext ;
142149
143150BEGIN
144- select to_schema_qualified_type(atttypid)from pg_attribute
151+ select to_schema_qualified_type(atttypid)
152+ from pg_catalog .pg_attribute
145153where attrelid= relation::regclassand attnum= colnum
146154into result;
147155
148156if result isnull then
149- raisenotice ' attribute #% of relation % not found' ,
157+ raiseexception ' attribute #% of relation % not found' ,
150158colnum, quote_literal(relation);
151159end if;
152160
@@ -160,7 +168,8 @@ CREATE FUNCTION to_namespace(nsp text) RETURNS OID AS $$
160168resultoid ;
161169
162170BEGIN
163- select oid from pg_namespace
171+ select oid
172+ from pg_catalog .pg_namespace
164173where nspname= nsp
165174into result;
166175
@@ -174,7 +183,8 @@ CREATE FUNCTION get_namespace(relation oid) RETURNS OID AS $$
174183resultoid ;
175184
176185BEGIN
177- select relnamespacefrom pg_class
186+ select relnamespace
187+ from pg_catalog .pg_class
178188where oid = relation
179189into result;
180190
@@ -183,7 +193,7 @@ CREATE FUNCTION get_namespace(relation oid) RETURNS OID AS $$
183193$$ LANGUAGE plpgsql;
184194
185195
186- CREATE FUNCTION dump_statistic () RETURNS SETOFTEXT AS $$
196+ CREATE FUNCTION dump_statistic (relid oid ) RETURNS SETOFTEXT AS $$
187197DECLARE
188198resulttext ;
189199
@@ -212,11 +222,12 @@ CREATE FUNCTION dump_statistic() RETURNS SETOF TEXT AS $$
212222
213223BEGIN
214224for rin
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
218229
219- relname := to_schema_qualified_relname (r .starelid );
230+ relname := to_schema_qualified_relation (r .starelid );
220231attname := quote_literal(to_attname(relname,r .staattnum ));
221232atttype := quote_literal(to_atttype(relname,r .staattnum ));
222233relname := quote_literal(relname);-- redefine relname
@@ -226,7 +237,7 @@ CREATE FUNCTION dump_statistic() RETURNS SETOF TEXT AS $$
226237
227238cmd := ' WITH upsert as (' ||
228239' UPDATE pg_catalog.pg_statistic SET %s' ||
229- ' WHEREto_schema_qualified_relname (starelid) =' || relname|| ' '
240+ ' WHEREto_schema_qualified_relation (starelid) =' || relname|| ' '
230241' AND to_attname(' || relname|| ' , staattnum) =' || attname|| ' '
231242' AND to_atttype(' || relname|| ' , staattnum) =' || atttype|| ' '
232243' AND stainherit =' || r .stainherit || ' ' ||
@@ -362,4 +373,68 @@ CREATE FUNCTION dump_statistic() RETURNS SETOF TEXT AS $$
362373
363374return;
364375END;
365- $$ LANGUAGE plpgsql;
376+ $$ LANGUAGE plpgsql;
377+
378+
379+ CREATE FUNCTION dump_statistic (schema_nametext , table_nametext ) RETURNS SETOFTEXT AS $$
380+ DECLARE
381+ qual_relnametext ;
382+ relidoid ;
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_nametext ) RETURNS SETOFTEXT AS $$
403+ DECLARE
404+ relidoid ;
405+ itext ;
406+
407+ BEGIN
408+ for relidin
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 iin 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 SETOFTEXT AS $$
424+ DECLARE
425+ relidoid ;
426+ itext ;
427+
428+ BEGIN
429+ for relidin
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 iin select dump_statistic(relid) loop
436+ return next i;
437+ end loop;
438+ end loop;
439+ END;
440+ $$ LANGUAGE plpgsql;