@@ -160,7 +160,7 @@ gistMakeUnionItVec(GISTSTATE *giststate, IndexTuple *itvec, int len,
160
160
161
161
evec = (GistEntryVector * )palloc ((len + 2 )* sizeof (GISTENTRY )+ GEVHDRSZ );
162
162
163
- for (i = 0 ;i < giststate -> tupdesc -> natts ;i ++ )
163
+ for (i = 0 ;i < giststate -> nonLeafTupdesc -> natts ;i ++ )
164
164
{
165
165
int j ;
166
166
@@ -171,7 +171,8 @@ gistMakeUnionItVec(GISTSTATE *giststate, IndexTuple *itvec, int len,
171
171
Datum datum ;
172
172
bool IsNull ;
173
173
174
- datum = index_getattr (itvec [j ],i + 1 ,giststate -> tupdesc ,& IsNull );
174
+ datum = index_getattr (itvec [j ],i + 1 ,giststate -> leafTupdesc ,
175
+ & IsNull );
175
176
if (IsNull )
176
177
continue ;
177
178
@@ -296,11 +297,11 @@ gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p,
296
297
{
297
298
int i ;
298
299
299
- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
300
+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
300
301
{
301
302
Datum datum ;
302
303
303
- datum = index_getattr (tuple ,i + 1 ,giststate -> tupdesc ,& isnull [i ]);
304
+ datum = index_getattr (tuple ,i + 1 ,giststate -> leafTupdesc ,& isnull [i ]);
304
305
gistdentryinit (giststate ,i ,& attdata [i ],
305
306
datum ,r ,p ,o ,
306
307
false,isnull [i ]);
@@ -329,7 +330,7 @@ gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *gis
329
330
gistDeCompressAtt (giststate ,r ,addtup ,NULL ,
330
331
(OffsetNumber )0 ,addentries ,addisnull );
331
332
332
- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
333
+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
333
334
{
334
335
gistMakeUnionKey (giststate ,i ,
335
336
oldentries + i ,oldisnull [i ],
@@ -442,14 +443,15 @@ gistchoose(Relation r, Page p, IndexTuple it,/* it has compressed entry */
442
443
zero_penalty = true;
443
444
444
445
/* Loop over index attributes. */
445
- for (j = 0 ;j < r -> rd_att -> natts ;j ++ )
446
+ for (j = 0 ;j < IndexRelationGetNumberOfKeyAttributes ( r ) ;j ++ )
446
447
{
447
448
Datum datum ;
448
449
float usize ;
449
450
bool IsNull ;
450
451
451
452
/* Compute penalty for this column. */
452
- datum = index_getattr (itup ,j + 1 ,giststate -> tupdesc ,& IsNull );
453
+ datum = index_getattr (itup ,j + 1 ,giststate -> leafTupdesc ,
454
+ & IsNull );
453
455
gistdentryinit (giststate ,j ,& entry ,datum ,r ,p ,i ,
454
456
false,IsNull );
455
457
usize = gistpenalty (giststate ,j ,& entry ,IsNull ,
@@ -470,7 +472,7 @@ gistchoose(Relation r, Page p, IndexTuple it,/* it has compressed entry */
470
472
result = i ;
471
473
best_penalty [j ]= usize ;
472
474
473
- if (j < r -> rd_att -> natts - 1 )
475
+ if (j < IndexRelationGetNumberOfKeyAttributes ( r ) - 1 )
474
476
best_penalty [j + 1 ]= -1 ;
475
477
476
478
/* we have new best, so reset keep-it decision */
@@ -500,7 +502,7 @@ gistchoose(Relation r, Page p, IndexTuple it,/* it has compressed entry */
500
502
* If we looped past the last column, and did not update "result",
501
503
* then this tuple is exactly as good as the prior best tuple.
502
504
*/
503
- if (j == r -> rd_att -> natts && result != i )
505
+ if (j == IndexRelationGetNumberOfKeyAttributes ( r ) && result != i )
504
506
{
505
507
if (keep_current_best == -1 )
506
508
{
@@ -579,7 +581,7 @@ gistFormTuple(GISTSTATE *giststate, Relation r,
579
581
/*
580
582
* Call the compress method on each attribute.
581
583
*/
582
- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
584
+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
583
585
{
584
586
if (isnull [i ])
585
587
compatt [i ]= (Datum )0 ;
@@ -602,7 +604,23 @@ gistFormTuple(GISTSTATE *giststate, Relation r,
602
604
}
603
605
}
604
606
605
- res = index_form_tuple (giststate -> tupdesc ,compatt ,isnull );
607
+ if (isleaf )
608
+ {
609
+ /*
610
+ * Emplace each included attribute if any.
611
+ */
612
+ for (;i < r -> rd_att -> natts ;i ++ )
613
+ {
614
+ if (isnull [i ])
615
+ compatt [i ]= (Datum )0 ;
616
+ else
617
+ compatt [i ]= attdata [i ];
618
+ }
619
+ }
620
+
621
+ res = index_form_tuple (isleaf ?giststate -> leafTupdesc :
622
+ giststate -> nonLeafTupdesc ,
623
+ compatt ,isnull );
606
624
607
625
/*
608
626
* The offset number on tuples on internal pages is unused. For historical
@@ -644,11 +662,11 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
644
662
bool isnull [INDEX_MAX_KEYS ];
645
663
int i ;
646
664
647
- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
665
+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
648
666
{
649
667
Datum datum ;
650
668
651
- datum = index_getattr (tuple ,i + 1 ,giststate -> tupdesc ,& isnull [i ]);
669
+ datum = index_getattr (tuple ,i + 1 ,giststate -> leafTupdesc ,& isnull [i ]);
652
670
653
671
if (giststate -> fetchFn [i ].fn_oid != InvalidOid )
654
672
{
@@ -679,6 +697,15 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
679
697
fetchatt [i ]= (Datum )0 ;
680
698
}
681
699
}
700
+
701
+ /*
702
+ * Get each included attribute.
703
+ */
704
+ for (;i < r -> rd_att -> natts ;i ++ )
705
+ {
706
+ fetchatt [i ]= index_getattr (tuple ,i + 1 ,giststate -> leafTupdesc ,
707
+ & isnull [i ]);
708
+ }
682
709
MemoryContextSwitchTo (oldcxt );
683
710
684
711
return heap_form_tuple (giststate -> fetchTupdesc ,fetchatt ,isnull );