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

Commit06e10ab

Browse files
committed
Fix problems with cached tuple descriptors disappearing while still in use
by creating a reference-count mechanism, similar to what we did a long timeago for catcache entries. The back branches have an ugly solution involvinglots of extra copies, but this way is more efficient. Reference counting isonly applied to tupdescs that are actually in caches --- there seems no needto use it for tupdescs that are generated in the executor, since they'll goaway during plan shutdown by virtue of being in the per-query memory context.Neil Conway and Tom Lane
1 parentb49ce32 commit06e10ab

36 files changed

+581
-246
lines changed

‎src/backend/access/common/tupdesc.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.116 2006/03/1600:31:54 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.117 2006/06/1618:42:21 tgl Exp $
1212
*
1313
* NOTES
1414
* some of the executor utility code such as "ExecTypeFromTL" should be
@@ -23,6 +23,7 @@
2323
#include"catalog/pg_type.h"
2424
#include"parser/parse_type.h"
2525
#include"utils/builtins.h"
26+
#include"utils/resowner.h"
2627
#include"utils/syscache.h"
2728

2829

@@ -84,6 +85,7 @@ CreateTemplateTupleDesc(int natts, bool hasoid)
8485
desc->tdtypeid=RECORDOID;
8586
desc->tdtypmod=-1;
8687
desc->tdhasoid=hasoid;
88+
desc->tdrefcount=-1;/* assume not reference-counted */
8789

8890
returndesc;
8991
}
@@ -116,6 +118,7 @@ CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
116118
desc->tdtypeid=RECORDOID;
117119
desc->tdtypmod=-1;
118120
desc->tdhasoid=hasoid;
121+
desc->tdrefcount=-1;/* assume not reference-counted */
119122

120123
returndesc;
121124
}
@@ -214,6 +217,12 @@ FreeTupleDesc(TupleDesc tupdesc)
214217
{
215218
inti;
216219

220+
/*
221+
* Possibly this should assert tdrefcount == 0, to disallow explicit
222+
* freeing of un-refcounted tupdescs?
223+
*/
224+
Assert(tupdesc->tdrefcount <=0);
225+
217226
if (tupdesc->constr)
218227
{
219228
if (tupdesc->constr->num_defval>0)
@@ -246,12 +255,48 @@ FreeTupleDesc(TupleDesc tupdesc)
246255
pfree(tupdesc);
247256
}
248257

258+
/*
259+
* Increment the reference count of a tupdesc, and log the reference in
260+
* CurrentResourceOwner.
261+
*
262+
* Do not apply this to tupdescs that are not being refcounted. (Use the
263+
* macro PinTupleDesc for tupdescs of uncertain status.)
264+
*/
265+
void
266+
IncrTupleDescRefCount(TupleDesctupdesc)
267+
{
268+
Assert(tupdesc->tdrefcount >=0);
269+
270+
ResourceOwnerEnlargeTupleDescs(CurrentResourceOwner);
271+
tupdesc->tdrefcount++;
272+
ResourceOwnerRememberTupleDesc(CurrentResourceOwner,tupdesc);
273+
}
274+
275+
/*
276+
* Decrement the reference count of a tupdesc, remove the corresponding
277+
* reference from CurrentResourceOwner, and free the tupdesc if no more
278+
* references remain.
279+
*
280+
* Do not apply this to tupdescs that are not being refcounted. (Use the
281+
* macro ReleaseTupleDesc for tupdescs of uncertain status.)
282+
*/
283+
void
284+
DecrTupleDescRefCount(TupleDesctupdesc)
285+
{
286+
Assert(tupdesc->tdrefcount>0);
287+
288+
ResourceOwnerForgetTupleDesc(CurrentResourceOwner,tupdesc);
289+
if (--tupdesc->tdrefcount==0)
290+
FreeTupleDesc(tupdesc);
291+
}
292+
249293
/*
250294
* Compare two TupleDesc structures for logical equality
251295
*
252296
* Note: we deliberately do not check the attrelid and tdtypmod fields.
253297
* This allows typcache.c to use this routine to see if a cached record type
254298
* matches a requested type, and is harmless for relcache.c's uses.
299+
* We don't compare tdrefcount, either.
255300
*/
256301
bool
257302
equalTupleDescs(TupleDesctupdesc1,TupleDesctupdesc2)

‎src/backend/access/heap/tuptoaster.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.59 2006/03/05 15:58:21momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.60 2006/06/16 18:42:21tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -892,7 +892,10 @@ toast_flatten_tuple_attribute(Datum value,
892892
* If nothing to untoast, just return the original tuple.
893893
*/
894894
if (!need_change)
895+
{
896+
ReleaseTupleDesc(tupleDesc);
895897
returnvalue;
898+
}
896899

897900
/*
898901
* Calculate the new size of the tuple. Header size should not change,
@@ -929,6 +932,7 @@ toast_flatten_tuple_attribute(Datum value,
929932
for (i=0;i<numAttrs;i++)
930933
if (toast_free[i])
931934
pfree(DatumGetPointer(toast_values[i]));
935+
ReleaseTupleDesc(tupleDesc);
932936

933937
returnPointerGetDatum(new_data);
934938
}

‎src/backend/commands/tablecmds.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.184 2006/05/10 23:18:39 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.185 2006/06/1618:42:21 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2637,6 +2637,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
26372637

26382638
MemoryContextSwitchTo(oldCxt);
26392639
heap_endscan(scan);
2640+
2641+
ExecDropSingleTupleTableSlot(oldslot);
2642+
ExecDropSingleTupleTableSlot(newslot);
26402643
}
26412644

26422645
FreeExecutorState(estate);

‎src/backend/executor/execJunk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/execJunk.c,v 1.52 2006/03/05 15:58:25 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/execJunk.c,v 1.53 2006/06/16 18:42:21 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -79,7 +79,7 @@ ExecInitJunkFilter(List *targetList, bool hasoid, TupleTableSlot *slot)
7979
* Use the given slot, or make a new slot if we weren't given one.
8080
*/
8181
if (slot)
82-
ExecSetSlotDescriptor(slot,cleanTupType, false);
82+
ExecSetSlotDescriptor(slot,cleanTupType);
8383
else
8484
slot=MakeSingleTupleTableSlot(cleanTupType);
8585

@@ -150,7 +150,7 @@ ExecInitJunkFilterConversion(List *targetList,
150150
* Use the given slot, or make a new slot if we weren't given one.
151151
*/
152152
if (slot)
153-
ExecSetSlotDescriptor(slot,cleanTupType, false);
153+
ExecSetSlotDescriptor(slot,cleanTupType);
154154
else
155155
slot=MakeSingleTupleTableSlot(cleanTupType);
156156

‎src/backend/executor/execMain.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
*
2828
* IDENTIFICATION
29-
* $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.270 2006/04/30 18:30:38 tgl Exp $
29+
* $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.271 2006/06/16 18:42:21 tgl Exp $
3030
*
3131
*-------------------------------------------------------------------------
3232
*/
@@ -1445,9 +1445,7 @@ ExecInsert(TupleTableSlot *slot,
14451445
TupleTableSlot*newslot=estate->es_trig_tuple_slot;
14461446

14471447
if (newslot->tts_tupleDescriptor!=slot->tts_tupleDescriptor)
1448-
ExecSetSlotDescriptor(newslot,
1449-
slot->tts_tupleDescriptor,
1450-
false);
1448+
ExecSetSlotDescriptor(newslot,slot->tts_tupleDescriptor);
14511449
ExecStoreTuple(newtuple,newslot,InvalidBuffer, false);
14521450
slot=newslot;
14531451
tuple=newtuple;
@@ -1654,9 +1652,7 @@ ExecUpdate(TupleTableSlot *slot,
16541652
TupleTableSlot*newslot=estate->es_trig_tuple_slot;
16551653

16561654
if (newslot->tts_tupleDescriptor!=slot->tts_tupleDescriptor)
1657-
ExecSetSlotDescriptor(newslot,
1658-
slot->tts_tupleDescriptor,
1659-
false);
1655+
ExecSetSlotDescriptor(newslot,slot->tts_tupleDescriptor);
16601656
ExecStoreTuple(newtuple,newslot,InvalidBuffer, false);
16611657
slot=newslot;
16621658
tuple=newtuple;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp