Skip to content

Commit 7cd0fd6

Browse files
committed
Invalid parent's relcache after CREATE TABLE .. PARTITION OF.
Otherwise, subsequent commands in the same transaction see the wrong partition descriptor. Amit Langote. Reported by Tomas Vondra and David Fetter. Reviewed by me. Discussion: http://postgr.es/m/22dd313b-d7fd-22b5-0787-654845c8f849%402ndquadrant.com Discussion: http://postgr.es/m/20161215090916.GB20659%40fetter.org
1 parent e13029a commit 7cd0fd6

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

src/backend/catalog/heap.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -3230,9 +3230,12 @@ RemovePartitionKeyByRelId(Oid relid)
32303230
* StorePartitionBound
32313231
* Update pg_class tuple of rel to store the partition bound and set
32323232
* relispartition to true
3233+
*
3234+
* Also, invalidate the parent's relcache, so that the next rebuild will load
3235+
* the new partition's info into its partition descriptor.
32333236
*/
32343237
void
3235-
StorePartitionBound(Relation rel, Node *bound)
3238+
StorePartitionBound(Relation rel, Relation parent, Node *bound)
32363239
{
32373240
Relation classRel;
32383241
HeapTuple tuple,
@@ -3273,4 +3276,6 @@ StorePartitionBound(Relation rel, Node *bound)
32733276
CatalogUpdateIndexes(classRel, newtuple);
32743277
heap_freetuple(newtuple);
32753278
heap_close(classRel, RowExclusiveLock);
3279+
3280+
CacheInvalidateRelcache(parent);
32763281
}

src/backend/commands/tablecmds.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,11 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
777777
* it does not return on error.
778778
*/
779779
check_new_partition_bound(relname, parent, bound);
780-
heap_close(parent, NoLock);
781780

782781
/* Update the pg_class entry. */
783-
StorePartitionBound(rel, bound);
782+
StorePartitionBound(rel, parent, bound);
783+
784+
heap_close(parent, NoLock);
784785

785786
/*
786787
* The code that follows may also update the pg_class tuple to update
@@ -13141,7 +13142,7 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd)
1314113142
cmd->bound);
1314213143

1314313144
/* Update the pg_class entry. */
13144-
StorePartitionBound(attachRel, cmd->bound);
13145+
StorePartitionBound(attachRel, rel, cmd->bound);
1314513146

1314613147
/*
1314713148
* Generate partition constraint from the partition bound specification.
@@ -13352,12 +13353,6 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd)
1335213353
}
1335313354
}
1335413355

13355-
/*
13356-
* Invalidate the parent's relcache so that the new partition is now
13357-
* included its partition descriptor.
13358-
*/
13359-
CacheInvalidateRelcache(rel);
13360-
1336113356
ObjectAddressSet(address, RelationRelationId, RelationGetRelid(attachRel));
1336213357

1336313358
/* keep our lock until commit */

src/include/catalog/heap.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,6 @@ extern void StorePartitionKey(Relation rel,
143143
Oid *partopclass,
144144
Oid *partcollation);
145145
extern void RemovePartitionKeyByRelId(Oid relid);
146-
extern void StorePartitionBound(Relation rel, Node *bound);
146+
extern void StorePartitionBound(Relation rel, Relation parent, Node *bound);
147147

148148
#endif /* HEAP_H */

0 commit comments

Comments
 (0)