diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-09-02 14:29:31 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-09-02 14:29:31 -0400 |
commit | 5b562644fec696977df4a82790064e8287927891 (patch) | |
tree | 3f8132a4e7e9fb9591c3e7f069035388918ae020 /src/backend/commands/tablecmds.c | |
parent | 2f72d5df6a876406cf5f2f8d7800d591dff3e2e3 (diff) | |
download | postgresql-5b562644fec696977df4a82790064e8287927891.tar.gz postgresql-5b562644fec696977df4a82790064e8287927891.zip |
Teach ANALYZE to clear pg_class.relhassubclass when appropriate.
In the past, relhassubclass always remained true if a relation had ever had
child relations, even if the last subclass was long gone. While this had
only marginal performance implications in most cases, it was annoying, and
I'm now considering some planner changes that would raise the cost of a
false positive. It was previously impractical to fix this because of race
condition concerns. However, given the recent change that made tablecmds.c
take ShareExclusiveLock on relations that are gaining a child (commit
fbcf4b92aa64d4577bcf25925b055316b978744a), we can now allow ANALYZE to
clear the flag when it's no longer relevant. There is no additional
locking cost to do so, since ANALYZE takes ShareExclusiveLock anyway.
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 4509cdab900..1e8ad2b6716 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -253,7 +253,6 @@ static void StoreCatalogInheritance(Oid relationId, List *supers); static void StoreCatalogInheritance1(Oid relationId, Oid parentOid, int16 seqNumber, Relation inhRelation); static int findAttrByName(const char *attributeName, List *schema); -static void setRelhassubclassInRelation(Oid relationId, bool relhassubclass); static void AlterIndexNamespaces(Relation classRel, Relation rel, Oid oldNspOid, Oid newNspOid); static void AlterSeqNamespaces(Relation classRel, Relation rel, @@ -1359,7 +1358,10 @@ MergeAttributes(List *schema, List *supers, char relpersistence, * add children to the same parent simultaneously, and that parent has * no pre-existing children, then both will attempt to update the * parent's relhassubclass field, leading to a "tuple concurrently - * updated" error. + * updated" error. Also, this interlocks against a concurrent ANALYZE + * on the parent table, which might otherwise be attempting to clear + * the parent's relhassubclass field, if its previous children were + * recently dropped. */ relation = heap_openrv(parent, ShareUpdateExclusiveLock); @@ -1958,7 +1960,7 @@ StoreCatalogInheritance1(Oid relationId, Oid parentOid, /* * Mark the parent as having subclasses. */ - setRelhassubclassInRelation(parentOid, true); + SetRelationHasSubclass(parentOid, true); } /* @@ -1985,11 +1987,22 @@ findAttrByName(const char *attributeName, List *schema) return 0; } + /* - * Update a relation's pg_class.relhassubclass entry to the given value + * SetRelationHasSubclass + * Set the value of the relation's relhassubclass field in pg_class. + * + * NOTE: caller must be holding an appropriate lock on the relation. + * ShareUpdateExclusiveLock is sufficient. + * + * NOTE: an important side-effect of this operation is that an SI invalidation + * message is sent out to all backends --- including me --- causing plans + * referencing the relation to be rebuilt with the new list of children. + * This must happen even if we find that no change is needed in the pg_class + * row. */ -static void -setRelhassubclassInRelation(Oid relationId, bool relhassubclass) +void +SetRelationHasSubclass(Oid relationId, bool relhassubclass) { Relation relationRelation; HeapTuple tuple; @@ -1997,9 +2010,6 @@ setRelhassubclassInRelation(Oid relationId, bool relhassubclass) /* * Fetch a modifiable copy of the tuple, modify it, update pg_class. - * - * If the tuple already has the right relhassubclass setting, we don't - * need to update it, but we still need to issue an SI inval message. */ relationRelation = heap_open(RelationRelationId, RowExclusiveLock); tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId)); |