@@ -160,7 +160,7 @@ gistMakeUnionItVec(GISTSTATE *giststate, IndexTuple *itvec, int len,
160160
161161evec = (GistEntryVector * )palloc ((len + 2 )* sizeof (GISTENTRY )+ GEVHDRSZ );
162162
163- for (i = 0 ;i < giststate -> tupdesc -> natts ;i ++ )
163+ for (i = 0 ;i < giststate -> nonLeafTupdesc -> natts ;i ++ )
164164{
165165int j ;
166166
@@ -171,7 +171,8 @@ gistMakeUnionItVec(GISTSTATE *giststate, IndexTuple *itvec, int len,
171171Datum datum ;
172172bool IsNull ;
173173
174- datum = index_getattr (itvec [j ],i + 1 ,giststate -> tupdesc ,& IsNull );
174+ datum = index_getattr (itvec [j ],i + 1 ,giststate -> leafTupdesc ,
175+ & IsNull );
175176if (IsNull )
176177continue ;
177178
@@ -296,11 +297,11 @@ gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p,
296297{
297298int i ;
298299
299- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
300+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
300301{
301302Datum datum ;
302303
303- datum = index_getattr (tuple ,i + 1 ,giststate -> tupdesc ,& isnull [i ]);
304+ datum = index_getattr (tuple ,i + 1 ,giststate -> leafTupdesc ,& isnull [i ]);
304305gistdentryinit (giststate ,i ,& attdata [i ],
305306datum ,r ,p ,o ,
306307 false,isnull [i ]);
@@ -329,7 +330,7 @@ gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *gis
329330gistDeCompressAtt (giststate ,r ,addtup ,NULL ,
330331 (OffsetNumber )0 ,addentries ,addisnull );
331332
332- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
333+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
333334{
334335gistMakeUnionKey (giststate ,i ,
335336oldentries + i ,oldisnull [i ],
@@ -442,14 +443,15 @@ gistchoose(Relation r, Page p, IndexTuple it,/* it has compressed entry */
442443zero_penalty = true;
443444
444445/* Loop over index attributes. */
445- for (j = 0 ;j < r -> rd_att -> natts ;j ++ )
446+ for (j = 0 ;j < IndexRelationGetNumberOfKeyAttributes ( r ) ;j ++ )
446447{
447448Datum datum ;
448449float usize ;
449450bool IsNull ;
450451
451452/* 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 );
453455gistdentryinit (giststate ,j ,& entry ,datum ,r ,p ,i ,
454456 false,IsNull );
455457usize = gistpenalty (giststate ,j ,& entry ,IsNull ,
@@ -470,7 +472,7 @@ gistchoose(Relation r, Page p, IndexTuple it,/* it has compressed entry */
470472result = i ;
471473best_penalty [j ]= usize ;
472474
473- if (j < r -> rd_att -> natts - 1 )
475+ if (j < IndexRelationGetNumberOfKeyAttributes ( r ) - 1 )
474476best_penalty [j + 1 ]= -1 ;
475477
476478/* we have new best, so reset keep-it decision */
@@ -500,7 +502,7 @@ gistchoose(Relation r, Page p, IndexTuple it,/* it has compressed entry */
500502 * If we looped past the last column, and did not update "result",
501503 * then this tuple is exactly as good as the prior best tuple.
502504 */
503- if (j == r -> rd_att -> natts && result != i )
505+ if (j == IndexRelationGetNumberOfKeyAttributes ( r ) && result != i )
504506{
505507if (keep_current_best == -1 )
506508{
@@ -579,7 +581,7 @@ gistFormTuple(GISTSTATE *giststate, Relation r,
579581/*
580582 * Call the compress method on each attribute.
581583 */
582- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
584+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
583585{
584586if (isnull [i ])
585587compatt [i ]= (Datum )0 ;
@@ -602,7 +604,23 @@ gistFormTuple(GISTSTATE *giststate, Relation r,
602604}
603605}
604606
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 );
606624
607625/*
608626 * The offset number on tuples on internal pages is unused. For historical
@@ -644,11 +662,11 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
644662bool isnull [INDEX_MAX_KEYS ];
645663int i ;
646664
647- for (i = 0 ;i < r -> rd_att -> natts ;i ++ )
665+ for (i = 0 ;i < IndexRelationGetNumberOfKeyAttributes ( r ) ;i ++ )
648666{
649667Datum datum ;
650668
651- datum = index_getattr (tuple ,i + 1 ,giststate -> tupdesc ,& isnull [i ]);
669+ datum = index_getattr (tuple ,i + 1 ,giststate -> leafTupdesc ,& isnull [i ]);
652670
653671if (giststate -> fetchFn [i ].fn_oid != InvalidOid )
654672{
@@ -679,6 +697,15 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
679697fetchatt [i ]= (Datum )0 ;
680698}
681699}
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+ }
682709MemoryContextSwitchTo (oldcxt );
683710
684711return heap_form_tuple (giststate -> fetchTupdesc ,fetchatt ,isnull );