Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6da004

Browse files
committed
Add index_get_partition convenience function
This new function simplifies some existing coding, as well as supportsfuture patches.Discussion:https://postgr.es/m/201901222145.t6wws6t6vrcu@alvherre.pgsqlReviewed-by: Amit Langote, Jesper Pedersen
1 parent3d0dcc5 commita6da004

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(Relationpartition,OidindexId)
155+
{
156+
List*idxlist=RelationGetIndexList(partition);
157+
ListCell*l;
158+
159+
foreach(l,idxlist)
160+
{
161+
OidpartIdx=lfirst_oid(l);
162+
HeapTupletup;
163+
Form_pg_classclassForm;
164+
boolispartition;
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+
returnpartIdx;
178+
}
179+
}
180+
181+
returnInvalidOid;
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
staticvoid
1565015650
refuseDupeIndexAttach(RelationparentIdx,RelationpartIdx,RelationpartitionTbl)
1565115651
{
15652-
Relationpg_inherits;
15653-
ScanKeyDatakey;
15654-
HeapTupletuple;
15655-
SysScanDescscan;
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_inheritsinhForm;
15666-
Oidtab;
15652+
OidexistingIdx;
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
externOidget_partition_parent(Oidrelid);
2323
externList*get_partition_ancestors(Oidrelid);
24+
externOidindex_get_partition(Relationpartition,OidindexId);
2425
externList*map_partition_varattnos(List*expr,intfromrel_varno,
2526
Relationto_rel,Relationfrom_rel,
2627
bool*found_whole_row);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp