diff options
| author | Tom Lane <tgl@sss.pgh.pa.us> | 2019-12-25 14:43:13 -0500 |
|---|---|---|
| committer | Tom Lane <tgl@sss.pgh.pa.us> | 2019-12-25 14:43:13 -0500 |
| commit | 5b9312378e2f8fb35ef4584aea351c3319a10422 (patch) | |
| tree | afff813bde76bff39602077f1be8372ee094f52b /src/backend/utils/cache/partcache.c | |
| parent | 8ce3aa9b5914d1ac45ed3f9bc484f66b3c4850c7 (diff) | |
Load relcache entries' partitioning data on-demand, not immediately.
Formerly the rd_partkey and rd_partdesc data structures were always
populated immediately when a relcache entry was built or rebuilt.
This patch changes things so that they are populated only when they
are first requested. (Hence, callers *must* now always use
RelationGetPartitionKey or RelationGetPartitionDesc; just fetching
the pointer directly is no longer acceptable.)
This seems to have some performance benefits, but the main reason to do
it is that it eliminates a recursive-reload failure that occurs if the
partkey or partdesc expressions contain any references to the relation's
rowtype (as discovered by Amit Langote). In retrospect, since loading
these data structures might result in execution of nearly-arbitrary code
via eval_const_expressions, it was a dumb idea to require that to happen
during relcache entry rebuild.
Also, fix things so that old copies of a relcache partition descriptor
will be dropped when the cache entry's refcount goes to zero. In the
previous coding it was possible for such copies to survive for the
lifetime of the session, as I'd complained of in a previous discussion.
(This management technique still isn't perfect, but it's better than
before.) Improve the commentary explaining how that works and why
it's safe to hand out direct pointers to these relcache substructures.
In passing, improve RelationBuildPartitionDesc by using the same
memory-context-parent-swap approach used by RelationBuildPartitionKey,
thereby making it less dependent on strong assumptions about what
partition_bounds_copy does. Avoid doing get_rel_relkind in the
critical section, too.
Patch by Amit Langote and Tom Lane; Robert Haas deserves some credit
for prior work in the area, too. Although this is a pre-existing
problem, no back-patch: the patch seems too invasive to be safe to
back-patch, and the bug it fixes is a corner case that seems
relatively unlikely to cause problems in the field.
Discussion: https://postgr.es/m/CA+HiwqFUzjfj9HEsJtYWcr1SgQ_=iCAvQ=O2Sx6aQxoDu4OiHw@mail.gmail.com
Discussion: https://postgr.es/m/CA+TgmoY3bRmGB6-DUnoVy5fJoreiBJ43rwMrQRCdPXuKt4Ykaw@mail.gmail.com
Diffstat (limited to 'src/backend/utils/cache/partcache.c')
| -rw-r--r-- | src/backend/utils/cache/partcache.c | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/backend/utils/cache/partcache.c b/src/backend/utils/cache/partcache.c index 342ab4dbaa6..e2144c83aba 100644 --- a/src/backend/utils/cache/partcache.c +++ b/src/backend/utils/cache/partcache.c @@ -37,9 +37,32 @@ #include "utils/syscache.h" +static void RelationBuildPartitionKey(Relation relation); static List *generate_partition_qual(Relation rel); /* + * RelationGetPartitionKey -- get partition key, if relation is partitioned + * + * Note: partition keys are not allowed to change after the partitioned rel + * is created. RelationClearRelation knows this and preserves rd_partkey + * across relcache rebuilds, as long as the relation is open. Therefore, + * even though we hand back a direct pointer into the relcache entry, it's + * safe for callers to continue to use that pointer as long as they hold + * the relation open. + */ +PartitionKey +RelationGetPartitionKey(Relation rel) +{ + if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE) + return NULL; + + if (unlikely(rel->rd_partkey == NULL)) + RelationBuildPartitionKey(rel); + + return rel->rd_partkey; +} + +/* * RelationBuildPartitionKey * Build partition key data of relation, and attach to relcache * @@ -54,7 +77,7 @@ static List *generate_partition_qual(Relation rel); * that some of our callees allocate memory on their own which would be leaked * permanently. */ -void +static void RelationBuildPartitionKey(Relation relation) { Form_pg_partitioned_table form; @@ -74,12 +97,9 @@ RelationBuildPartitionKey(Relation relation) tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(RelationGetRelid(relation))); - /* - * The following happens when we have created our pg_class entry but not - * the pg_partitioned_table entry yet. - */ if (!HeapTupleIsValid(tuple)) - return; + elog(ERROR, "cache lookup failed for partition key of relation %u", + RelationGetRelid(relation)); partkeycxt = AllocSetContextCreate(CurTransactionContext, "partition key", |
