Skip to content

Commit a6da004

Browse files
committed
Add index_get_partition convenience function
This new function simplifies some existing coding, as well as supports future patches. Discussion: https://postgr.es/m/201901222145.t6wws6t6vrcu@alvherre.pgsql Reviewed-by: Amit Langote, Jesper Pedersen
1 parent 3d0dcc5 commit a6da004

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

src/backend/catalog/partition.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,42 @@ get_partition_ancestors_worker(Relation inhRel, Oid relid, List **ancestors)
145145
get_partition_ancestors_worker(inhRel, parentOid, ancestors);
146146
}
147147

148+
/*
149+
* index_get_partition
150+
* Return the OID of index of the given partition that is a child
151+
* of the given index, or InvalidOid if there isn't one.
152+
*/
153+
Oid
154+
index_get_partition(Relation partition, Oid indexId)
155+
{
156+
List *idxlist = RelationGetIndexList(partition);
157+
ListCell *l;
158+
159+
foreach(l, idxlist)
160+
{
161+
Oid partIdx = lfirst_oid(l);
162+
HeapTuple tup;
163+
Form_pg_class classForm;
164+
bool ispartition;
165+
166+
tup = SearchSysCache1(RELOID, ObjectIdGetDatum(partIdx));
167+
if (!tup)
168+
elog(ERROR, "cache lookup failed for relation %u", partIdx);
169+
classForm = (Form_pg_class) GETSTRUCT(tup);
170+
ispartition = classForm->relispartition;
171+
ReleaseSysCache(tup);
172+
if (!ispartition)
173+
continue;
174+
if (get_partition_parent(lfirst_oid(l)) == indexId)
175+
{
176+
list_free(idxlist);
177+
return partIdx;
178+
}
179+
}
180+
181+
return InvalidOid;
182+
}
183+
148184
/*
149185
* map_partition_varattnos - maps varattno of any Vars in expr from the
150186
* attno's of 'from_rel' to the attno's of 'to_rel' partition, each of which

src/backend/commands/tablecmds.c

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15649,36 +15649,18 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name)
1564915649
static void
1565015650
refuseDupeIndexAttach(Relation parentIdx, Relation partIdx, Relation partitionTbl)
1565115651
{
15652-
Relation pg_inherits;
15653-
ScanKeyData key;
15654-
HeapTuple tuple;
15655-
SysScanDesc scan;
15656-
15657-
pg_inherits = table_open(InheritsRelationId, AccessShareLock);
15658-
ScanKeyInit(&key, Anum_pg_inherits_inhparent,
15659-
BTEqualStrategyNumber, F_OIDEQ,
15660-
ObjectIdGetDatum(RelationGetRelid(parentIdx)));
15661-
scan = systable_beginscan(pg_inherits, InheritsParentIndexId, true,
15662-
NULL, 1, &key);
15663-
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
15664-
{
15665-
Form_pg_inherits inhForm;
15666-
Oid tab;
15652+
Oid existingIdx;
1566715653

15668-
inhForm = (Form_pg_inherits) GETSTRUCT(tuple);
15669-
tab = IndexGetRelation(inhForm->inhrelid, false);
15670-
if (tab == RelationGetRelid(partitionTbl))
15671-
ereport(ERROR,
15672-
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
15673-
errmsg("cannot attach index \"%s\" as a partition of index \"%s\"",
15674-
RelationGetRelationName(partIdx),
15675-
RelationGetRelationName(parentIdx)),
15676-
errdetail("Another index is already attached for partition \"%s\".",
15677-
RelationGetRelationName(partitionTbl))));
15678-
}
15679-
15680-
systable_endscan(scan);
15681-
table_close(pg_inherits, AccessShareLock);
15654+
existingIdx = index_get_partition(partitionTbl,
15655+
RelationGetRelid(parentIdx));
15656+
if (OidIsValid(existingIdx))
15657+
ereport(ERROR,
15658+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
15659+
errmsg("cannot attach index \"%s\" as a partition of index \"%s\"",
15660+
RelationGetRelationName(partIdx),
15661+
RelationGetRelationName(parentIdx)),
15662+
errdetail("Another index is already attached for partition \"%s\".",
15663+
RelationGetRelationName(partitionTbl))));
1568215664
}
1568315665

1568415666
/*

src/include/catalog/partition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
extern Oid get_partition_parent(Oid relid);
2323
extern List *get_partition_ancestors(Oid relid);
24+
extern Oid index_get_partition(Relation partition, Oid indexId);
2425
extern List *map_partition_varattnos(List *expr, int fromrel_varno,
2526
Relation to_rel, Relation from_rel,
2627
bool *found_whole_row);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy