diff options
-rw-r--r-- | src/backend/commands/analyze.c | 20 | ||||
-rw-r--r-- | src/backend/commands/vacuum.c | 48 | ||||
-rw-r--r-- | src/test/regress/expected/inherit.out | 38 |
3 files changed, 69 insertions, 37 deletions
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index eb4e91411e..1c74cce34c 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -2974,10 +2974,12 @@ coord_collect_simple_stats(Relation onerel, bool inh, int attr_cnt, int i; /* Number of data nodes from which attribute statistics are received. */ int *numnodes; + bool istemp; /* Get the relation identifier */ relname = RelationGetRelationName(onerel); nspname = get_namespace_name(RelationGetNamespace(onerel)); + istemp = (onerel->rd_rel->relpersistence == RELPERSISTENCE_TEMP); /* Make up query string */ initStringInfo(&query); @@ -3023,9 +3025,21 @@ coord_collect_simple_stats(Relation onerel, bool inh, int attr_cnt, " ON s.staop%d = o%d.oid ", i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i); - appendStringInfo(&query, "WHERE nc.nspname = '%s' " - "AND c.relname = '%s'", - nspname, relname); + + /* + * For temporary tables, the namespace may be different on each node. So we + * must not use the namespace from the coordinator. Instead, + * pg_my_temp_schema() does the right thing of finding the correct + * temporary namespace being used for the current session. We use that. + */ + if (istemp) + appendStringInfo(&query, "WHERE nc.oid = pg_my_temp_schema() " + "AND c.relname = '%s'", + relname); + else + appendStringInfo(&query, "WHERE nc.nspname = '%s' " + "AND c.relname = '%s'", + nspname, relname); /* Build up RemoteQuery */ step = makeNode(RemoteQuery); diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index edadfc4844..7a64c9b013 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1673,7 +1673,7 @@ make_relation_tle(Oid reloid, const char *relname, const char *column) * Returns number of nodes that returned correct statistics. */ static int -get_remote_relstat(char *nspname, char *relname, bool replicated, +get_remote_relstat(char *nspname, char *relname, bool istemp, bool replicated, int32 *pages, int32 *allvisiblepages, float4 *tuples, TransactionId *frozenXid) { @@ -1690,15 +1690,32 @@ get_remote_relstat(char *nspname, char *relname, bool replicated, /* Make up query string */ initStringInfo(&query); - appendStringInfo(&query, "SELECT c.relpages, " - "c.reltuples, " - "c.relallvisible, " - "c.relfrozenxid " - "FROM pg_class c JOIN pg_namespace n " - "ON c.relnamespace = n.oid " - "WHERE n.nspname = '%s' " - "AND c.relname = '%s'", - nspname, relname); + + /* + * For temporary tables, the namespace may be different on each node. So we + * must not use the namespace from the coordinator. Instead, + * pg_my_temp_schema() does the right thing of finding the correct + * temporary namespace being used for the current session. We use that. + */ + if (istemp) + appendStringInfo(&query, "SELECT c.relpages, " + "c.reltuples, " + "c.relallvisible, " + "c.relfrozenxid " + "FROM pg_class c " + "WHERE c.relnamespace = pg_my_temp_schema() " + "AND c.relname = '%s'", + relname); + else + appendStringInfo(&query, "SELECT c.relpages, " + "c.reltuples, " + "c.relallvisible, " + "c.relfrozenxid " + "FROM pg_class c JOIN pg_namespace n " + "ON c.relnamespace = n.oid " + "WHERE n.nspname = '%s' " + "AND c.relname = '%s'", + nspname, relname); /* Build up RemoteQuery */ step = makeNode(RemoteQuery); @@ -1755,12 +1772,16 @@ get_remote_relstat(char *nspname, char *relname, bool replicated, { validpages++; *pages += DatumGetInt32(value); + elog(LOG, "get_remote_relstat: relname:%s, remote relpages: %d", + relname, DatumGetInt32(value)); } value = slot_getattr(result, 2, &isnull); /* reltuples */ if (!isnull) { validtuples++; *tuples += DatumGetFloat4(value); + elog(LOG, "get_remote_relstat: relname:%s, remote reltuples: %f", + relname, DatumGetFloat4(value)); } value = slot_getattr(result, 3, &isnull); /* relallvisible */ if (!isnull) @@ -1845,10 +1866,12 @@ vacuum_rel_coordinator(Relation onerel, bool is_outer) bool hasindex; bool replicated; int rel_nodes; + bool istemp; /* Get the relation identifier */ relname = RelationGetRelationName(onerel); nspname = get_namespace_name(RelationGetNamespace(onerel)); + istemp = (onerel->rd_rel->relpersistence == RELPERSISTENCE_TEMP); elog(LOG, "Getting relation statistics for %s.%s", nspname, relname); @@ -1857,7 +1880,7 @@ vacuum_rel_coordinator(Relation onerel, bool is_outer) * Get stats from the remote nodes. Function returns the number of nodes * returning correct stats. */ - rel_nodes = get_remote_relstat(nspname, relname, replicated, + rel_nodes = get_remote_relstat(nspname, relname, istemp, replicated, &num_pages, &num_allvisible_pages, &num_tuples, &min_frozenxid); if (rel_nodes > 0) @@ -1885,7 +1908,8 @@ vacuum_rel_coordinator(Relation onerel, bool is_outer) relname = RelationGetRelationName(Irel[i]); nspname = get_namespace_name(RelationGetNamespace(Irel[i])); /* Index is replicated if parent relation is replicated */ - idx_nodes = get_remote_relstat(nspname, relname, replicated, + idx_nodes = get_remote_relstat(nspname, relname, istemp, + replicated, &idx_pages, &idx_allvisible_pages, &idx_tuples, &idx_frozenxid); if (idx_nodes > 0) diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index 03b1432896..c0e631014a 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -1520,8 +1520,8 @@ vacuum analyze patest2; analyze int4_tbl; explain (costs off, num_nodes off, nodes off) select * from patest0 join (select f1 from int4_tbl where f1 = 0 limit 1) ss on id = f1; - QUERY PLAN ----------------------------------------------------------------- + QUERY PLAN +-------------------------------------------------------------- Nested Loop -> Limit -> Remote Subquery Scan on all @@ -1531,17 +1531,13 @@ select * from patest0 join (select f1 from int4_tbl where f1 = 0 limit 1) ss on -> Materialize -> Remote Subquery Scan on all -> Append - -> Seq Scan on patest0 - Filter: (int4_tbl.f1 = id) - -> Bitmap Heap Scan on patest1 - Recheck Cond: (id = int4_tbl.f1) - -> Bitmap Index Scan on patest1i - Index Cond: (id = int4_tbl.f1) - -> Bitmap Heap Scan on patest2 - Recheck Cond: (id = int4_tbl.f1) - -> Bitmap Index Scan on patest2i - Index Cond: (id = int4_tbl.f1) -(19 rows) + -> Index Scan using patest0i on patest0 + Index Cond: (id = int4_tbl.f1) + -> Index Scan using patest1i on patest1 + Index Cond: (id = int4_tbl.f1) + -> Index Scan using patest2i on patest2 + Index Cond: (id = int4_tbl.f1) +(15 rows) select * from patest0 join (select f1 from int4_tbl where f1 = 0 limit 1) ss on id = f1; id | x | f1 @@ -1554,8 +1550,8 @@ select * from patest0 join (select f1 from int4_tbl where f1 = 0 limit 1) ss on drop index patest2i; explain (costs off, num_nodes off, nodes off) select * from patest0 join (select f1 from int4_tbl where f1 = 0 limit 1) ss on id = f1; - QUERY PLAN ----------------------------------------------------------------- + QUERY PLAN +-------------------------------------------------------------- Nested Loop -> Limit -> Remote Subquery Scan on all @@ -1565,15 +1561,13 @@ select * from patest0 join (select f1 from int4_tbl where f1 = 0 limit 1) ss on -> Materialize -> Remote Subquery Scan on all -> Append - -> Seq Scan on patest0 - Filter: (int4_tbl.f1 = id) - -> Bitmap Heap Scan on patest1 - Recheck Cond: (id = int4_tbl.f1) - -> Bitmap Index Scan on patest1i - Index Cond: (id = int4_tbl.f1) + -> Index Scan using patest0i on patest0 + Index Cond: (id = int4_tbl.f1) + -> Index Scan using patest1i on patest1 + Index Cond: (id = int4_tbl.f1) -> Seq Scan on patest2 Filter: (int4_tbl.f1 = id) -(17 rows) +(15 rows) select * from patest0 join (select f1 from int4_tbl where f1 = 0 limit 1) ss on id = f1; id | x | f1 |