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

Commitc266ed3

Browse files
committed
Cleanup covering infrastructure
- Explicitly forbids opclass, collation and indoptions (like DESC/ASC etc) for including columns. Throw an error if user points that.- Truncated storage arrays for such attributes to store only key atrributes, added assertion checks.- Do not check opfamily and collation for including columns in CompareIndexInfo()Discussion:https://www.postgresql.org/message-id/5ee72852-3c4e-ee35-e2ed-c1d053d45c08@sigaev.ru
1 parent08ea7a2 commitc266ed3

File tree

8 files changed

+90
-39
lines changed

8 files changed

+90
-39
lines changed

‎src/backend/catalog/index.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ ConstructTupleDescriptor(Relation heapRelation,
297297
Oid*classObjectId)
298298
{
299299
intnumatts=indexInfo->ii_NumIndexAttrs;
300+
intnumkeyatts=indexInfo->ii_NumIndexKeyAttrs;
300301
ListCell*colnames_item=list_head(indexColNames);
301302
ListCell*indexpr_item=list_head(indexInfo->ii_Expressions);
302303
IndexAmRoutine*amroutine;
@@ -375,7 +376,8 @@ ConstructTupleDescriptor(Relation heapRelation,
375376
to->attidentity='\0';
376377
to->attislocal= true;
377378
to->attinhcount=0;
378-
to->attcollation=collationObjectId[i];
379+
to->attcollation= (i<numkeyatts) ?
380+
collationObjectId[i] :InvalidOid;
379381
}
380382
else
381383
{
@@ -411,7 +413,8 @@ ConstructTupleDescriptor(Relation heapRelation,
411413
to->attcacheoff=-1;
412414
to->atttypmod=exprTypmod(indexkey);
413415
to->attislocal= true;
414-
to->attcollation=collationObjectId[i];
416+
to->attcollation= (i<numkeyatts) ?
417+
collationObjectId[i] :InvalidOid;
415418

416419
ReleaseSysCache(tuple);
417420

@@ -608,9 +611,9 @@ UpdateIndexRelation(Oid indexoid,
608611
indkey=buildint2vector(NULL,indexInfo->ii_NumIndexAttrs);
609612
for (i=0;i<indexInfo->ii_NumIndexAttrs;i++)
610613
indkey->values[i]=indexInfo->ii_IndexAttrNumbers[i];
611-
indcollation=buildoidvector(collationOids,indexInfo->ii_NumIndexAttrs);
614+
indcollation=buildoidvector(collationOids,indexInfo->ii_NumIndexKeyAttrs);
612615
indclass=buildoidvector(classOids,indexInfo->ii_NumIndexKeyAttrs);
613-
indoption=buildint2vector(coloptions,indexInfo->ii_NumIndexAttrs);
616+
indoption=buildint2vector(coloptions,indexInfo->ii_NumIndexKeyAttrs);
614617

615618
/*
616619
* Convert the index expressions (if any) to a text datum
@@ -1081,7 +1084,7 @@ index_create(Relation heapRelation,
10811084

10821085
/* Store dependency on collations */
10831086
/* The default collation is pinned, so don't bother recording it */
1084-
for (i=0;i<indexInfo->ii_NumIndexAttrs;i++)
1087+
for (i=0;i<indexInfo->ii_NumIndexKeyAttrs;i++)
10851088
{
10861089
if (OidIsValid(collationObjectId[i])&&
10871090
collationObjectId[i]!=DEFAULT_COLLATION_OID)
@@ -1832,6 +1835,11 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
18321835
if (info1->ii_NumIndexAttrs!=info2->ii_NumIndexAttrs)
18331836
return false;
18341837

1838+
/* and same number of key attributes */
1839+
if (info1->ii_NumIndexKeyAttrs!=info2->ii_NumIndexKeyAttrs)
1840+
return false;
1841+
1842+
18351843
/*
18361844
* and columns match through the attribute map (actual attribute numbers
18371845
* might differ!) Note that this implies that index columns that are
@@ -1849,6 +1857,10 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
18491857
info1->ii_IndexAttrNumbers[i]))
18501858
return false;
18511859

1860+
/* collation and opfamily is not valid for including columns */
1861+
if (i >=info1->ii_NumIndexKeyAttrs)
1862+
continue;
1863+
18521864
if (collations1[i]!=collations2[i])
18531865
return false;
18541866
if (opfamilies1[i]!=opfamilies2[i])

‎src/backend/commands/indexcmds.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,8 @@ CheckPredicate(Expr *predicate)
13591359

13601360
/*
13611361
* Compute per-index-column information, including indexed column numbers
1362-
* or index expressions, opclasses, and indoptions.
1362+
* or index expressions, opclasses, and indoptions. Note, all output vectors
1363+
* should be allocated for all columns, including "including" ones.
13631364
*/
13641365
staticvoid
13651366
ComputeIndexAttrs(IndexInfo*indexInfo,
@@ -1490,6 +1491,36 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
14901491

14911492
typeOidP[attn]=atttype;
14921493

1494+
/*
1495+
* Included columns have no collation, no opclass and no ordering options.
1496+
*/
1497+
if (attn >=nkeycols)
1498+
{
1499+
if (attribute->collation)
1500+
ereport(ERROR,
1501+
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1502+
errmsg("including column does not support a collation")));
1503+
if (attribute->opclass)
1504+
ereport(ERROR,
1505+
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1506+
errmsg("including column does not support an operator class")));
1507+
if (attribute->ordering!=SORTBY_DEFAULT)
1508+
ereport(ERROR,
1509+
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1510+
errmsg("including column does not support ASC/DESC options")));
1511+
if (attribute->nulls_ordering!=SORTBY_NULLS_DEFAULT)
1512+
ereport(ERROR,
1513+
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1514+
errmsg("including column does not support NULLS FIRST/LAST options")));
1515+
1516+
classOidP[attn]=InvalidOid;
1517+
colOptionP[attn]=0;
1518+
collationOidP[attn]=InvalidOid;
1519+
attn++;
1520+
1521+
continue;
1522+
}
1523+
14931524
/*
14941525
* Apply collation override if any
14951526
*/
@@ -1521,17 +1552,6 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
15211552

15221553
collationOidP[attn]=attcollation;
15231554

1524-
/*
1525-
* Included columns have no opclass and no ordering options.
1526-
*/
1527-
if (attn >=nkeycols)
1528-
{
1529-
classOidP[attn]=InvalidOid;
1530-
colOptionP[attn]=0;
1531-
attn++;
1532-
continue;
1533-
}
1534-
15351555
/*
15361556
* Identify the opclass to use.
15371557
*/

‎src/backend/optimizer/path/indxpath.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,8 +2329,8 @@ match_clause_to_indexcol(IndexOptInfo *index,
23292329
{
23302330
Expr*clause=rinfo->clause;
23312331
Indexindex_relid=index->rel->relid;
2332-
Oidopfamily=index->opfamily[indexcol];
2333-
Oididxcollation=index->indexcollations[indexcol];
2332+
Oidopfamily;
2333+
Oididxcollation;
23342334
Node*leftop,
23352335
*rightop;
23362336
Relidsleft_relids;
@@ -2339,6 +2339,11 @@ match_clause_to_indexcol(IndexOptInfo *index,
23392339
Oidexpr_coll;
23402340
boolplain_op;
23412341

2342+
Assert(indexcol<index->nkeycolumns);
2343+
2344+
opfamily=index->opfamily[indexcol];
2345+
idxcollation=index->indexcollations[indexcol];
2346+
23422347
/* First check for boolean-index cases. */
23432348
if (IsBooleanOpfamily(opfamily))
23442349
{
@@ -2678,15 +2683,19 @@ match_clause_to_ordering_op(IndexOptInfo *index,
26782683
Expr*clause,
26792684
Oidpk_opfamily)
26802685
{
2681-
Oidopfamily=index->opfamily[indexcol];
2682-
Oididxcollation=index->indexcollations[indexcol];
2686+
Oidopfamily;
2687+
Oididxcollation;
26832688
Node*leftop,
26842689
*rightop;
26852690
Oidexpr_op;
26862691
Oidexpr_coll;
26872692
Oidsortfamily;
26882693
boolcommuted;
26892694

2695+
Assert(indexcol<index->nkeycolumns);
2696+
2697+
opfamily=index->opfamily[indexcol];
2698+
idxcollation=index->indexcollations[indexcol];
26902699
/*
26912700
* Clause must be a binary opclause.
26922701
*/
@@ -2921,8 +2930,13 @@ ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
29212930
{
29222931
IndexOptInfo*index= ((ec_member_matches_arg*)arg)->index;
29232932
intindexcol= ((ec_member_matches_arg*)arg)->indexcol;
2924-
OidcurFamily=index->opfamily[indexcol];
2925-
OidcurCollation=index->indexcollations[indexcol];
2933+
OidcurFamily;
2934+
OidcurCollation;
2935+
2936+
Assert(indexcol<index->nkeycolumns);
2937+
2938+
curFamily=index->opfamily[indexcol];
2939+
curCollation=index->indexcollations[indexcol];
29262940

29272941
/*
29282942
* If it's a btree index, we can reject it if its opfamily isn't
@@ -3548,8 +3562,13 @@ expand_indexqual_conditions(IndexOptInfo *index,
35483562
RestrictInfo*rinfo= (RestrictInfo*)lfirst(lcc);
35493563
intindexcol=lfirst_int(lci);
35503564
Expr*clause=rinfo->clause;
3551-
OidcurFamily=index->opfamily[indexcol];
3552-
OidcurCollation=index->indexcollations[indexcol];
3565+
OidcurFamily;
3566+
OidcurCollation;
3567+
3568+
Assert(indexcol<index->nkeycolumns);
3569+
3570+
curFamily=index->opfamily[indexcol];
3571+
curCollation=index->indexcollations[indexcol];
35533572

35543573
/* First check for boolean cases */
35553574
if (IsBooleanOpfamily(curFamily))
@@ -3918,14 +3937,15 @@ adjust_rowcompare_for_index(RowCompareExpr *clause,
39183937
/*
39193938
* The Var side can match any column of the index.
39203939
*/
3921-
for (i=0;i<index->ncolumns;i++)
3940+
for (i=0;i<index->nkeycolumns;i++)
39223941
{
39233942
if (match_index_to_operand(varop,i,index)&&
39243943
get_op_opfamily_strategy(expr_op,
39253944
index->opfamily[i])==op_strategy&&
39263945
IndexCollMatchesExprColl(index->indexcollations[i],
39273946
lfirst_oid(collids_cell)))
3928-
break;
3947+
3948+
break;
39293949
}
39303950
if (i >=index->ncolumns)
39313951
break;/* no match found */

‎src/backend/optimizer/util/plancat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,22 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
242242
info->nkeycolumns=nkeycolumns=index->indnkeyatts;
243243

244244
info->indexkeys= (int*)palloc(sizeof(int)*ncolumns);
245-
info->indexcollations= (Oid*)palloc(sizeof(Oid)*ncolumns);
245+
info->indexcollations= (Oid*)palloc(sizeof(Oid)*nkeycolumns);
246246
info->opfamily= (Oid*)palloc(sizeof(Oid)*nkeycolumns);
247247
info->opcintype= (Oid*)palloc(sizeof(Oid)*nkeycolumns);
248248
info->canreturn= (bool*)palloc(sizeof(bool)*ncolumns);
249249

250250
for (i=0;i<ncolumns;i++)
251251
{
252252
info->indexkeys[i]=index->indkey.values[i];
253-
info->indexcollations[i]=indexRelation->rd_indcollation[i];
254253
info->canreturn[i]=index_can_return(indexRelation,i+1);
255254
}
256255

257256
for (i=0;i<nkeycolumns;i++)
258257
{
259258
info->opfamily[i]=indexRelation->rd_opfamily[i];
260259
info->opcintype[i]=indexRelation->rd_opcintype[i];
260+
info->indexcollations[i]=indexRelation->rd_indcollation[i];
261261
}
262262

263263
info->relam=indexRelation->rd_rel->relam;

‎src/backend/parser/parse_utilcmd.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,9 +1588,6 @@ generateClonedIndexStmt(RangeVar *heapRel, Oid heapRelid, Relation source_idx,
15881588
/* Copy the original index column name */
15891589
iparam->indexcolname=pstrdup(NameStr(attr->attname));
15901590

1591-
/* Add the collation name, if non-default */
1592-
iparam->collation=get_collation(indcollation->values[keyno],keycoltype);
1593-
15941591
index->indexIncludingParams=lappend(index->indexIncludingParams,iparam);
15951592
}
15961593
/* Copy reloptions if any */

‎src/backend/utils/adt/ruleutils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,15 +1356,15 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
13561356
{
13571357
Oidindcoll;
13581358

1359+
if (keyno >=idxrec->indnkeyatts)
1360+
continue;
1361+
13591362
/* Add collation, if not default for column */
13601363
indcoll=indcollation->values[keyno];
13611364
if (OidIsValid(indcoll)&&indcoll!=keycolcollation)
13621365
appendStringInfo(&buf," COLLATE %s",
13631366
generate_collation_name((indcoll)));
13641367

1365-
if (keyno >=idxrec->indnkeyatts)
1366-
continue;
1367-
13681368
/* Add the operator class name, if not default */
13691369
get_opclass_name(indclass->values[keyno],keycoltype,&buf);
13701370

‎src/backend/utils/adt/selfuncs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7437,6 +7437,8 @@ gincost_pattern(IndexOptInfo *index, int indexcol,
74377437
int32searchMode=GIN_SEARCH_MODE_DEFAULT;
74387438
int32i;
74397439

7440+
Assert(indexcol<index->nkeycolumns);
7441+
74407442
/*
74417443
* Get the operator's strategy number and declared input data types within
74427444
* the index opfamily. (We don't need the latter, but we use

‎src/backend/utils/cache/relcache.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,10 +1637,10 @@ RelationInitIndexAccessInfo(Relation relation)
16371637
}
16381638

16391639
relation->rd_indcollation= (Oid*)
1640-
MemoryContextAllocZero(indexcxt,indnatts*sizeof(Oid));
1640+
MemoryContextAllocZero(indexcxt,indnkeyatts*sizeof(Oid));
16411641

16421642
relation->rd_indoption= (int16*)
1643-
MemoryContextAllocZero(indexcxt,indnatts*sizeof(int16));
1643+
MemoryContextAllocZero(indexcxt,indnkeyatts*sizeof(int16));
16441644

16451645
/*
16461646
* indcollation cannot be referenced directly through the C struct,
@@ -1653,7 +1653,7 @@ RelationInitIndexAccessInfo(Relation relation)
16531653
&isnull);
16541654
Assert(!isnull);
16551655
indcoll= (oidvector*)DatumGetPointer(indcollDatum);
1656-
memcpy(relation->rd_indcollation,indcoll->values,indnatts*sizeof(Oid));
1656+
memcpy(relation->rd_indcollation,indcoll->values,indnkeyatts*sizeof(Oid));
16571657

16581658
/*
16591659
* indclass cannot be referenced directly through the C struct, because it
@@ -1685,7 +1685,7 @@ RelationInitIndexAccessInfo(Relation relation)
16851685
&isnull);
16861686
Assert(!isnull);
16871687
indoption= (int2vector*)DatumGetPointer(indoptionDatum);
1688-
memcpy(relation->rd_indoption,indoption->values,indnatts*sizeof(int16));
1688+
memcpy(relation->rd_indoption,indoption->values,indnkeyatts*sizeof(int16));
16891689

16901690
/*
16911691
* expressions, predicate, exclusion caches will be filled later

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp