From 2c3e47527a6f53cd1d98887fdb9e770c118954ca Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 27 Mar 2017 00:53:59 -0300 Subject: Fix a couple of problems in pg_get_statisticsextdef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was a thinko whereby we tested the wrong tuple after fetching it from cache; avoid that by using generate_relation_name instead, which is simpler. Also, the statistics name was not qualified, so add that. (It could be argued that qualification should be conditional on the schema not being on search path. We can add that later, but at least this form is correct.) Author: David Rowley, Álvaro Herrera Discussion: https://postgr.es/m/CAKJS1f8RjLeVZJ2+93pdQGuZJeBF-ifsHaFMR-q-6-Z0qxA8cA@mail.gmail.com --- src/backend/utils/adt/ruleutils.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'src/backend/utils/adt/ruleutils.c') diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index d57d5568b28..c2681ced2af 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -1448,11 +1448,10 @@ static char * pg_get_statisticsext_worker(Oid statextid, bool missing_ok) { Form_pg_statistic_ext statextrec; - Form_pg_class pgclassrec; HeapTuple statexttup; - HeapTuple pgclasstup; StringInfoData buf; int colno; + char *nsp; statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid)); @@ -1465,20 +1464,12 @@ pg_get_statisticsext_worker(Oid statextid, bool missing_ok) statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup); - pgclasstup = SearchSysCache1(RELOID, ObjectIdGetDatum(statextrec->starelid)); - - if (!HeapTupleIsValid(statexttup)) - { - ReleaseSysCache(statexttup); - elog(ERROR, "cache lookup failed for relation %u", statextrec->starelid); - } - - pgclassrec = (Form_pg_class) GETSTRUCT(pgclasstup); - initStringInfo(&buf); + nsp = get_namespace_name(statextrec->stanamespace); appendStringInfo(&buf, "CREATE STATISTICS %s ON (", - quote_identifier(NameStr(statextrec->staname))); + quote_qualified_identifier(nsp, + NameStr(statextrec->staname))); for (colno = 0; colno < statextrec->stakeys.dim1; colno++) { @@ -1494,10 +1485,9 @@ pg_get_statisticsext_worker(Oid statextid, bool missing_ok) } appendStringInfo(&buf, ") FROM %s", - quote_identifier(NameStr(pgclassrec->relname))); + generate_relation_name(statextrec->starelid, NIL)); ReleaseSysCache(statexttup); - ReleaseSysCache(pgclasstup); return buf.data; } -- cgit v1.2.3