| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* resowner.c |
| 4 |
* POSTGRES resource owner management code. |
| 5 |
* |
| 6 |
* Query-lifespan resources are tracked by associating them with |
| 7 |
* ResourceOwner objects. This provides a simple mechanism for ensuring |
| 8 |
* that such resources are freed at the right time. |
| 9 |
* See utils/resowner/README for more info on how to use it. |
| 10 |
* |
| 11 |
* The implementation consists of a small fixed-size array and a hash table. |
| 12 |
* New entries are inserted to the fixed-size array, and when the array |
| 13 |
* fills up, all the entries are moved to the hash table. This way, the |
| 14 |
* array always contains a few most recently remembered references. To find |
| 15 |
* a particular reference, you need to search both the array and the hash |
| 16 |
* table. |
| 17 |
* |
| 18 |
* The most frequent usage is that a resource is remembered, and forgotten |
| 19 |
* shortly thereafter. For example, pin a buffer, read one tuple from it, |
| 20 |
* release the pin. Linearly scanning the small array handles that case |
| 21 |
* efficiently. However, some resources are held for a longer time, and |
| 22 |
* sometimes a lot of resources need to be held simultaneously. The hash |
| 23 |
* table handles those cases. |
| 24 |
* |
| 25 |
* When it's time to release the resources, we sort them according to the |
| 26 |
* release-priority of each resource, and release them in that order. |
| 27 |
* |
| 28 |
* Local lock references are special, they are not stored in the array or |
| 29 |
* the hash table. Instead, each resource owner has a separate small cache |
| 30 |
* of locks it owns. The lock manager has the same information in its local |
| 31 |
* lock hash table, and we fall back on that if the cache overflows, but |
| 32 |
* traversing the hash table is slower when there are a lot of locks |
| 33 |
* belonging to other resource owners. This is to speed up bulk releasing |
| 34 |
* or reassigning locks from a resource owner to its parent. |
| 35 |
* |
| 36 |
* |
| 37 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 38 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 39 |
* |
| 40 |
* |
| 41 |
* IDENTIFICATION |
| 42 |
* src/backend/utils/resowner/resowner.c |
| 43 |
* |
| 44 |
*------------------------------------------------------------------------- |
| 45 |
*/ |
| 46 |
#include "postgres.h" |
| 47 |
|
| 48 |
#include "common/hashfn.h" |
| 49 |
#include "common/int.h" |
| 50 |
#include "lib/ilist.h" |
| 51 |
#include "storage/aio.h" |
| 52 |
#include "storage/ipc.h" |
| 53 |
#include "storage/predicate.h" |
| 54 |
#include "storage/proc.h" |
| 55 |
#include "utils/memutils.h" |
| 56 |
#include "utils/resowner.h" |
| 57 |
|
| 58 |
/* |
| 59 |
* ResourceElem represents a reference associated with a resource owner. |
| 60 |
* |
| 61 |
* All objects managed by this code are required to fit into a Datum, |
| 62 |
* which is fine since they are generally pointers or integers. |
| 63 |
*/ |
| 64 |
typedef struct ResourceElem |
| 65 |
{ |
| 66 |
Datum item; |
| 67 |
const ResourceOwnerDesc *kind; /* NULL indicates a free hash table slot */ |
| 68 |
} ResourceElem; |
| 69 |
|
| 70 |
/* |
| 71 |
* Size of the fixed-size array to hold most-recently remembered resources. |
| 72 |
*/ |
| 73 |
#define RESOWNER_ARRAY_SIZE 32 |
| 74 |
|
| 75 |
/* |
| 76 |
* Initially allocated size of a ResourceOwner's hash table. Must be power of |
| 77 |
* two because we use (capacity - 1) as mask for hashing. |
| 78 |
*/ |
| 79 |
#define RESOWNER_HASH_INIT_SIZE 64 |
| 80 |
|
| 81 |
/* |
| 82 |
* How many items may be stored in a hash table of given capacity. When this |
| 83 |
* number is reached, we must resize. |
| 84 |
* |
| 85 |
* The hash table must always have enough free space that we can copy the |
| 86 |
* entries from the array to it, in ResourceOwnerSort. We also insist that |
| 87 |
* the initial size is large enough that we don't hit the max size immediately |
| 88 |
* when it's created. Aside from those limitations, 0.75 is a reasonable fill |
| 89 |
* factor. |
| 90 |
*/ |
| 91 |
#define RESOWNER_HASH_MAX_ITEMS(capacity) \ |
| 92 |
Min(capacity - RESOWNER_ARRAY_SIZE, (capacity)/4 * 3) |
| 93 |
|
| 94 |
StaticAssertDecl(RESOWNER_HASH_MAX_ITEMS(RESOWNER_HASH_INIT_SIZE) >= RESOWNER_ARRAY_SIZE, |
| 95 |
"initial hash size too small compared to array size"); |
| 96 |
|
| 97 |
/* |
| 98 |
* MAX_RESOWNER_LOCKS is the size of the per-resource owner locks cache. It's |
| 99 |
* chosen based on some testing with pg_dump with a large schema. When the |
| 100 |
* tests were done (on 9.2), resource owners in a pg_dump run contained up |
| 101 |
* to 9 locks, regardless of the schema size, except for the top resource |
| 102 |
* owner which contained much more (overflowing the cache). 15 seems like a |
| 103 |
* nice round number that's somewhat higher than what pg_dump needs. Note that |
| 104 |
* making this number larger is not free - the bigger the cache, the slower |
| 105 |
* it is to release locks (in retail), when a resource owner holds many locks. |
| 106 |
*/ |
| 107 |
#define MAX_RESOWNER_LOCKS 15 |
| 108 |
|
| 109 |
/* |
| 110 |
* ResourceOwner objects look like this |
| 111 |
*/ |
| 112 |
struct ResourceOwnerData |
| 113 |
{ |
| 114 |
ResourceOwner parent; /* NULL if no parent (toplevel owner) */ |
| 115 |
ResourceOwner firstchild; /* head of linked list of children */ |
| 116 |
ResourceOwner nextchild; /* next child of same parent */ |
| 117 |
const char *name; /* name (just for debugging) */ |
| 118 |
|
| 119 |
/* |
| 120 |
* When ResourceOwnerRelease is called, we sort the 'hash' and 'arr' by |
| 121 |
* the release priority. After that, no new resources can be remembered |
| 122 |
* or forgotten in retail. We have separate flags because |
| 123 |
* ResourceOwnerReleaseAllOfKind() temporarily sets 'releasing' without |
| 124 |
* sorting the arrays. |
| 125 |
*/ |
| 126 |
bool releasing; |
| 127 |
bool sorted; /* are 'hash' and 'arr' sorted by priority? */ |
| 128 |
|
| 129 |
/* |
| 130 |
* Number of items in the locks cache, array, and hash table respectively. |
| 131 |
* (These are packed together to avoid padding in the struct.) |
| 132 |
*/ |
| 133 |
uint8 nlocks; /* number of owned locks */ |
| 134 |
uint8 narr; /* how many items are stored in the array */ |
| 135 |
uint32 nhash; /* how many items are stored in the hash */ |
| 136 |
|
| 137 |
/* |
| 138 |
* The fixed-size array for recent resources. |
| 139 |
* |
| 140 |
* If 'sorted' is set, the contents are sorted by release priority. |
| 141 |
*/ |
| 142 |
ResourceElem arr[RESOWNER_ARRAY_SIZE]; |
| 143 |
|
| 144 |
/* |
| 145 |
* The hash table. Uses open-addressing. 'nhash' is the number of items |
| 146 |
* present; if it would exceed 'grow_at', we enlarge it and re-hash. |
| 147 |
* 'grow_at' should be rather less than 'capacity' so that we don't waste |
| 148 |
* too much time searching for empty slots. |
| 149 |
* |
| 150 |
* If 'sorted' is set, the contents are no longer hashed, but sorted by |
| 151 |
* release priority. The first 'nhash' elements are occupied, the rest |
| 152 |
* are empty. |
| 153 |
*/ |
| 154 |
ResourceElem *hash; |
| 155 |
uint32 capacity; /* allocated length of hash[] */ |
| 156 |
uint32 grow_at; /* grow hash when reach this */ |
| 157 |
|
| 158 |
/* The local locks cache. */ |
| 159 |
LOCALLOCK *locks[MAX_RESOWNER_LOCKS]; /* list of owned locks */ |
| 160 |
|
| 161 |
/* |
| 162 |
* AIO handles need be registered in critical sections and therefore |
| 163 |
* cannot use the normal ResourceElem mechanism. |
| 164 |
*/ |
| 165 |
dlist_head aio_handles; |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
/***************************************************************************** |
| 170 |
* GLOBAL MEMORY * |
| 171 |
*****************************************************************************/ |
| 172 |
|
| 173 |
ResourceOwner CurrentResourceOwner = NULL; |
| 174 |
ResourceOwner CurTransactionResourceOwner = NULL; |
| 175 |
ResourceOwner TopTransactionResourceOwner = NULL; |
| 176 |
ResourceOwner AuxProcessResourceOwner = NULL; |
| 177 |
|
| 178 |
/* #define RESOWNER_STATS */ |
| 179 |
|
| 180 |
#ifdef RESOWNER_STATS |
| 181 |
static int narray_lookups = 0; |
| 182 |
static int nhash_lookups = 0; |
| 183 |
#endif |
| 184 |
|
| 185 |
/* |
| 186 |
* List of add-on callbacks for resource releasing |
| 187 |
*/ |
| 188 |
typedef struct ResourceReleaseCallbackItem |
| 189 |
{ |
| 190 |
struct ResourceReleaseCallbackItem *next; |
| 191 |
ResourceReleaseCallback callback; |
| 192 |
void *arg; |
| 193 |
} ResourceReleaseCallbackItem; |
| 194 |
|
| 195 |
static ResourceReleaseCallbackItem *ResourceRelease_callbacks = NULL; |
| 196 |
|
| 197 |
|
| 198 |
/* Internal routines */ |
| 199 |
static inline uint32 hash_resource_elem(Datum value, const ResourceOwnerDesc *kind); |
| 200 |
static void ResourceOwnerAddToHash(ResourceOwner owner, Datum value, |
| 201 |
const ResourceOwnerDesc *kind); |
| 202 |
static int resource_priority_cmp(const void *a, const void *b); |
| 203 |
static void ResourceOwnerSort(ResourceOwner owner); |
| 204 |
static void ResourceOwnerReleaseAll(ResourceOwner owner, |
| 205 |
ResourceReleasePhase phase, |
| 206 |
bool printLeakWarnings); |
| 207 |
static void ResourceOwnerReleaseInternal(ResourceOwner owner, |
| 208 |
ResourceReleasePhase phase, |
| 209 |
bool isCommit, |
| 210 |
bool isTopLevel); |
| 211 |
static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); |
| 212 |
|
| 213 |
|
| 214 |
/***************************************************************************** |
| 215 |
* INTERNAL ROUTINES * |
| 216 |
*****************************************************************************/ |
| 217 |
|
| 218 |
/* |
| 219 |
* Hash function for value+kind combination. |
| 220 |
*/ |
| 221 |
static inline uint32 |
| 222 |
hash_resource_elem(Datum value, const ResourceOwnerDesc *kind) |
| 223 |
{ |
| 224 |
/* |
| 225 |
* Most resource kinds store a pointer in 'value', and pointers are unique |
| 226 |
* all on their own. But some resources store plain integers (Files and |
| 227 |
* Buffers as of this writing), so we want to incorporate the 'kind' in |
| 228 |
* the hash too, otherwise those resources will collide a lot. But |
| 229 |
* because there are only a few resource kinds like that - and only a few |
| 230 |
* resource kinds to begin with - we don't need to work too hard to mix |
| 231 |
* 'kind' into the hash. Just add it with hash_combine(), it perturbs the |
| 232 |
* result enough for our purposes. |
| 233 |
*/ |
| 234 |
#if SIZEOF_DATUM == 8 |
| 235 |
return hash_combine64(murmurhash64((uint64) value), (uint64) kind); |
| 236 |
#else |
| 237 |
return hash_combine(murmurhash32((uint32) value), (uint32) kind); |
| 238 |
#endif |
| 239 |
} |
| 240 |
|
| 241 |
/* |
| 242 |
* Adds 'value' of given 'kind' to the ResourceOwner's hash table |
| 243 |
*/ |
| 244 |
static void |
| 245 |
ResourceOwnerAddToHash(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind) |
| 246 |
{ |
| 247 |
uint32 mask = owner->capacity - 1; |
| 248 |
uint32 idx; |
| 249 |
|
| 250 |
Assert(kind != NULL); |
| 251 |
|
| 252 |
/* Insert into first free slot at or after hash location. */ |
| 253 |
idx = hash_resource_elem(value, kind) & mask; |
| 254 |
for (;;) |
| 255 |
{ |
| 256 |
if (owner->hash[idx].kind == NULL) |
| 257 |
break; /* found a free slot */ |
| 258 |
idx = (idx + 1) & mask; |
| 259 |
} |
| 260 |
owner->hash[idx].item = value; |
| 261 |
owner->hash[idx].kind = kind; |
| 262 |
owner->nhash++; |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* Comparison function to sort by release phase and priority |
| 267 |
*/ |
| 268 |
static int |
| 269 |
resource_priority_cmp(const void *a, const void *b) |
| 270 |
{ |
| 271 |
const ResourceElem *ra = (const ResourceElem *) a; |
| 272 |
const ResourceElem *rb = (const ResourceElem *) b; |
| 273 |
|
| 274 |
/* Note: reverse order */ |
| 275 |
if (ra->kind->release_phase == rb->kind->release_phase) |
| 276 |
return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority); |
| 277 |
else if (ra->kind->release_phase > rb->kind->release_phase) |
| 278 |
return -1; |
| 279 |
else |
| 280 |
return 1; |
| 281 |
} |
| 282 |
|
| 283 |
/* |
| 284 |
* Sort resources in reverse release priority. |
| 285 |
* |
| 286 |
* If the hash table is in use, all the elements from the fixed-size array are |
| 287 |
* moved to the hash table, and then the hash table is sorted. If there is no |
| 288 |
* hash table, then the fixed-size array is sorted directly. In either case, |
| 289 |
* the result is one sorted array that contains all the resources. |
| 290 |
*/ |
| 291 |
static void |
| 292 |
ResourceOwnerSort(ResourceOwner owner) |
| 293 |
{ |
| 294 |
ResourceElem *items; |
| 295 |
uint32 nitems; |
| 296 |
|
| 297 |
if (owner->nhash == 0) |
| 298 |
{ |
| 299 |
items = owner->arr; |
| 300 |
nitems = owner->narr; |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
/* |
| 305 |
* Compact the hash table, so that all the elements are in the |
| 306 |
* beginning of the 'hash' array, with no empty elements. |
| 307 |
*/ |
| 308 |
uint32 dst = 0; |
| 309 |
|
| 310 |
for (int idx = 0; idx < owner->capacity; idx++) |
| 311 |
{ |
| 312 |
if (owner->hash[idx].kind != NULL) |
| 313 |
{ |
| 314 |
if (dst != idx) |
| 315 |
owner->hash[dst] = owner->hash[idx]; |
| 316 |
dst++; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/* |
| 321 |
* Move all entries from the fixed-size array to 'hash'. |
| 322 |
* |
| 323 |
* RESOWNER_HASH_MAX_ITEMS is defined so that there is always enough |
| 324 |
* free space to move all the elements from the fixed-size array to |
| 325 |
* the hash. |
| 326 |
*/ |
| 327 |
Assert(dst + owner->narr <= owner->capacity); |
| 328 |
for (int idx = 0; idx < owner->narr; idx++) |
| 329 |
{ |
| 330 |
owner->hash[dst] = owner->arr[idx]; |
| 331 |
dst++; |
| 332 |
} |
| 333 |
Assert(dst == owner->nhash + owner->narr); |
| 334 |
owner->narr = 0; |
| 335 |
owner->nhash = dst; |
| 336 |
|
| 337 |
items = owner->hash; |
| 338 |
nitems = owner->nhash; |
| 339 |
} |
| 340 |
|
| 341 |
qsort(items, nitems, sizeof(ResourceElem), resource_priority_cmp); |
| 342 |
} |
| 343 |
|
| 344 |
/* |
| 345 |
* Call the ReleaseResource callback on entries with given 'phase'. |
| 346 |
*/ |
| 347 |
static void |
| 348 |
ResourceOwnerReleaseAll(ResourceOwner owner, ResourceReleasePhase phase, |
| 349 |
bool printLeakWarnings) |
| 350 |
{ |
| 351 |
ResourceElem *items; |
| 352 |
uint32 nitems; |
| 353 |
bool using_arr; |
| 354 |
|
| 355 |
/* |
| 356 |
* ResourceOwnerSort must've been called already. All the resources are |
| 357 |
* either in the array or the hash. |
| 358 |
*/ |
| 359 |
Assert(owner->releasing); |
| 360 |
Assert(owner->sorted); |
| 361 |
if (owner->nhash == 0) |
| 362 |
{ |
| 363 |
items = owner->arr; |
| 364 |
nitems = owner->narr; |
| 365 |
using_arr = true; |
| 366 |
} |
| 367 |
else |
| 368 |
{ |
| 369 |
Assert(owner->narr == 0); |
| 370 |
items = owner->hash; |
| 371 |
nitems = owner->nhash; |
| 372 |
using_arr = false; |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
* The resources are sorted in reverse priority order. Release them |
| 377 |
* starting from the end, until we hit the end of the phase that we are |
| 378 |
* releasing now. We will continue from there when called again for the |
| 379 |
* next phase. |
| 380 |
*/ |
| 381 |
while (nitems > 0) |
| 382 |
{ |
| 383 |
uint32 idx = nitems - 1; |
| 384 |
Datum value = items[idx].item; |
| 385 |
const ResourceOwnerDesc *kind = items[idx].kind; |
| 386 |
|
| 387 |
if (kind->release_phase > phase) |
| 388 |
break; |
| 389 |
Assert(kind->release_phase == phase); |
| 390 |
|
| 391 |
if (printLeakWarnings) |
| 392 |
{ |
| 393 |
char *res_str; |
| 394 |
|
| 395 |
res_str = kind->DebugPrint ? |
| 396 |
kind->DebugPrint(value) |
| 397 |
: psprintf("%s %p", kind->name, DatumGetPointer(value)); |
| 398 |
elog(WARNING, "resource was not closed: %s", res_str); |
| 399 |
pfree(res_str); |
| 400 |
} |
| 401 |
|
| 402 |
/* |
| 403 |
* Update stored count to forget the item before calling its |
| 404 |
* ReleaseResource method. This avoids double-free crashes in case an |
| 405 |
* error gets thrown within ReleaseResource. |
| 406 |
*/ |
| 407 |
nitems--; |
| 408 |
if (using_arr) |
| 409 |
owner->narr = nitems; |
| 410 |
else |
| 411 |
owner->nhash = nitems; |
| 412 |
|
| 413 |
kind->ReleaseResource(value); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/***************************************************************************** |
| 419 |
* EXPORTED ROUTINES * |
| 420 |
*****************************************************************************/ |
| 421 |
|
| 422 |
|
| 423 |
/* |
| 424 |
* ResourceOwnerCreate |
| 425 |
* Create an empty ResourceOwner. |
| 426 |
* |
| 427 |
* All ResourceOwner objects are kept in TopMemoryContext, since they should |
| 428 |
* only be freed explicitly. |
| 429 |
*/ |
| 430 |
ResourceOwner |
| 431 |
ResourceOwnerCreate(ResourceOwner parent, const char *name) |
| 432 |
{ |
| 433 |
ResourceOwner owner; |
| 434 |
|
| 435 |
owner = (ResourceOwner) MemoryContextAllocZero(TopMemoryContext, |
| 436 |
sizeof(struct ResourceOwnerData)); |
| 437 |
owner->name = name; |
| 438 |
|
| 439 |
if (parent) |
| 440 |
{ |
| 441 |
owner->parent = parent; |
| 442 |
owner->nextchild = parent->firstchild; |
| 443 |
parent->firstchild = owner; |
| 444 |
} |
| 445 |
|
| 446 |
dlist_init(&owner->aio_handles); |
| 447 |
|
| 448 |
return owner; |
| 449 |
} |
| 450 |
|
| 451 |
/* |
| 452 |
* Make sure there is room for at least one more resource in an array. |
| 453 |
* |
| 454 |
* This is separate from actually inserting a resource because if we run out |
| 455 |
* of memory, it's critical to do so *before* acquiring the resource. |
| 456 |
* |
| 457 |
* NB: Make sure there are no unrelated ResourceOwnerRemember() calls between |
| 458 |
* your ResourceOwnerEnlarge() call and the ResourceOwnerRemember() call that |
| 459 |
* you reserved the space for! |
| 460 |
*/ |
| 461 |
void |
| 462 |
ResourceOwnerEnlarge(ResourceOwner owner) |
| 463 |
{ |
| 464 |
/* |
| 465 |
* Mustn't try to remember more resources after we have already started |
| 466 |
* releasing |
| 467 |
*/ |
| 468 |
if (owner->releasing) |
| 469 |
elog(ERROR, "ResourceOwnerEnlarge called after release started"); |
| 470 |
|
| 471 |
if (owner->narr < RESOWNER_ARRAY_SIZE) |
| 472 |
return; /* no work needed */ |
| 473 |
|
| 474 |
/* |
| 475 |
* Is there space in the hash? If not, enlarge it. |
| 476 |
*/ |
| 477 |
if (owner->narr + owner->nhash >= owner->grow_at) |
| 478 |
{ |
| 479 |
uint32 i, |
| 480 |
oldcap, |
| 481 |
newcap; |
| 482 |
ResourceElem *oldhash; |
| 483 |
ResourceElem *newhash; |
| 484 |
|
| 485 |
oldhash = owner->hash; |
| 486 |
oldcap = owner->capacity; |
| 487 |
|
| 488 |
/* Double the capacity (it must stay a power of 2!) */ |
| 489 |
newcap = (oldcap > 0) ? oldcap * 2 : RESOWNER_HASH_INIT_SIZE; |
| 490 |
newhash = (ResourceElem *) MemoryContextAllocZero(TopMemoryContext, |
| 491 |
newcap * sizeof(ResourceElem)); |
| 492 |
|
| 493 |
/* |
| 494 |
* We assume we can't fail below this point, so OK to scribble on the |
| 495 |
* owner |
| 496 |
*/ |
| 497 |
owner->hash = newhash; |
| 498 |
owner->capacity = newcap; |
| 499 |
owner->grow_at = RESOWNER_HASH_MAX_ITEMS(newcap); |
| 500 |
owner->nhash = 0; |
| 501 |
|
| 502 |
if (oldhash != NULL) |
| 503 |
{ |
| 504 |
/* |
| 505 |
* Transfer any pre-existing entries into the new hash table; they |
| 506 |
* don't necessarily go where they were before, so this simple |
| 507 |
* logic is the best way. |
| 508 |
*/ |
| 509 |
for (i = 0; i < oldcap; i++) |
| 510 |
{ |
| 511 |
if (oldhash[i].kind != NULL) |
| 512 |
ResourceOwnerAddToHash(owner, oldhash[i].item, oldhash[i].kind); |
| 513 |
} |
| 514 |
|
| 515 |
/* And release old hash table. */ |
| 516 |
pfree(oldhash); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
/* Move items from the array to the hash */ |
| 521 |
for (int i = 0; i < owner->narr; i++) |
| 522 |
ResourceOwnerAddToHash(owner, owner->arr[i].item, owner->arr[i].kind); |
| 523 |
owner->narr = 0; |
| 524 |
|
| 525 |
Assert(owner->nhash <= owner->grow_at); |
| 526 |
} |
| 527 |
|
| 528 |
/* |
| 529 |
* Remember that an object is owned by a ResourceOwner |
| 530 |
* |
| 531 |
* Caller must have previously done ResourceOwnerEnlarge() |
| 532 |
*/ |
| 533 |
void |
| 534 |
ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind) |
| 535 |
{ |
| 536 |
uint32 idx; |
| 537 |
|
| 538 |
/* sanity check the ResourceOwnerDesc */ |
| 539 |
Assert(kind->release_phase != 0); |
| 540 |
Assert(kind->release_priority != 0); |
| 541 |
|
| 542 |
/* |
| 543 |
* Mustn't try to remember more resources after we have already started |
| 544 |
* releasing. We already checked this in ResourceOwnerEnlarge. |
| 545 |
*/ |
| 546 |
Assert(!owner->releasing); |
| 547 |
Assert(!owner->sorted); |
| 548 |
|
| 549 |
if (owner->narr >= RESOWNER_ARRAY_SIZE) |
| 550 |
{ |
| 551 |
/* forgot to call ResourceOwnerEnlarge? */ |
| 552 |
elog(ERROR, "ResourceOwnerRemember called but array was full"); |
| 553 |
} |
| 554 |
|
| 555 |
/* Append to the array. */ |
| 556 |
idx = owner->narr; |
| 557 |
owner->arr[idx].item = value; |
| 558 |
owner->arr[idx].kind = kind; |
| 559 |
owner->narr++; |
| 560 |
} |
| 561 |
|
| 562 |
/* |
| 563 |
* Forget that an object is owned by a ResourceOwner |
| 564 |
* |
| 565 |
* Note: If same resource ID is associated with the ResourceOwner more than |
| 566 |
* once, one instance is removed. |
| 567 |
* |
| 568 |
* Note: Forgetting a resource does not guarantee that there is room to |
| 569 |
* remember a new resource. One exception is when you forget the most |
| 570 |
* recently remembered resource; that does make room for a new remember call. |
| 571 |
* Some code callers rely on that exception. |
| 572 |
*/ |
| 573 |
void |
| 574 |
ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind) |
| 575 |
{ |
| 576 |
/* |
| 577 |
* Mustn't call this after we have already started releasing resources. |
| 578 |
* (Release callback functions are not allowed to release additional |
| 579 |
* resources.) |
| 580 |
*/ |
| 581 |
if (owner->releasing) |
| 582 |
elog(ERROR, "ResourceOwnerForget called for %s after release started", kind->name); |
| 583 |
Assert(!owner->sorted); |
| 584 |
|
| 585 |
/* Search through all items in the array first. */ |
| 586 |
for (int i = owner->narr - 1; i >= 0; i--) |
| 587 |
{ |
| 588 |
if (owner->arr[i].item == value && |
| 589 |
owner->arr[i].kind == kind) |
| 590 |
{ |
| 591 |
owner->arr[i] = owner->arr[owner->narr - 1]; |
| 592 |
owner->narr--; |
| 593 |
|
| 594 |
#ifdef RESOWNER_STATS |
| 595 |
narray_lookups++; |
| 596 |
#endif |
| 597 |
return; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
/* Search hash */ |
| 602 |
if (owner->nhash > 0) |
| 603 |
{ |
| 604 |
uint32 mask = owner->capacity - 1; |
| 605 |
uint32 idx; |
| 606 |
|
| 607 |
idx = hash_resource_elem(value, kind) & mask; |
| 608 |
for (uint32 i = 0; i < owner->capacity; i++) |
| 609 |
{ |
| 610 |
if (owner->hash[idx].item == value && |
| 611 |
owner->hash[idx].kind == kind) |
| 612 |
{ |
| 613 |
owner->hash[idx].item = (Datum) 0; |
| 614 |
owner->hash[idx].kind = NULL; |
| 615 |
owner->nhash--; |
| 616 |
|
| 617 |
#ifdef RESOWNER_STATS |
| 618 |
nhash_lookups++; |
| 619 |
#endif |
| 620 |
return; |
| 621 |
} |
| 622 |
idx = (idx + 1) & mask; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
/* |
| 627 |
* Use %p to print the reference, since most objects tracked by a resource |
| 628 |
* owner are pointers. It's a bit misleading if it's not a pointer, but |
| 629 |
* this is a programmer error, anyway. |
| 630 |
*/ |
| 631 |
elog(ERROR, "%s %p is not owned by resource owner %s", |
| 632 |
kind->name, DatumGetPointer(value), owner->name); |
| 633 |
} |
| 634 |
|
| 635 |
/* |
| 636 |
* ResourceOwnerRelease |
| 637 |
* Release all resources owned by a ResourceOwner and its descendants, |
| 638 |
* but don't delete the owner objects themselves. |
| 639 |
* |
| 640 |
* Note that this executes just one phase of release, and so typically |
| 641 |
* must be called three times. We do it this way because (a) we want to |
| 642 |
* do all the recursion separately for each phase, thereby preserving |
| 643 |
* the needed order of operations; and (b) xact.c may have other operations |
| 644 |
* to do between the phases. |
| 645 |
* |
| 646 |
* phase: release phase to execute |
| 647 |
* isCommit: true for successful completion of a query or transaction, |
| 648 |
* false for unsuccessful |
| 649 |
* isTopLevel: true if completing a main transaction, else false |
| 650 |
* |
| 651 |
* isCommit is passed because some modules may expect that their resources |
| 652 |
* were all released already if the transaction or portal finished normally. |
| 653 |
* If so it is reasonable to give a warning (NOT an error) should any |
| 654 |
* unreleased resources be present. When isCommit is false, such warnings |
| 655 |
* are generally inappropriate. |
| 656 |
* |
| 657 |
* isTopLevel is passed when we are releasing TopTransactionResourceOwner |
| 658 |
* at completion of a main transaction. This generally means that *all* |
| 659 |
* resources will be released, and so we can optimize things a bit. |
| 660 |
* |
| 661 |
* NOTE: After starting the release process, by calling this function, no new |
| 662 |
* resources can be remembered in the resource owner. You also cannot call |
| 663 |
* ResourceOwnerForget on any previously remembered resources to release |
| 664 |
* resources "in retail" after that, you must let the bulk release take care |
| 665 |
* of them. |
| 666 |
*/ |
| 667 |
void |
| 668 |
ResourceOwnerRelease(ResourceOwner owner, |
| 669 |
ResourceReleasePhase phase, |
| 670 |
bool isCommit, |
| 671 |
bool isTopLevel) |
| 672 |
{ |
| 673 |
/* There's not currently any setup needed before recursing */ |
| 674 |
ResourceOwnerReleaseInternal(owner, phase, isCommit, isTopLevel); |
| 675 |
|
| 676 |
#ifdef RESOWNER_STATS |
| 677 |
if (isTopLevel) |
| 678 |
{ |
| 679 |
elog(LOG, "RESOWNER STATS: lookups: array %d, hash %d", |
| 680 |
narray_lookups, nhash_lookups); |
| 681 |
narray_lookups = 0; |
| 682 |
nhash_lookups = 0; |
| 683 |
} |
| 684 |
#endif |
| 685 |
} |
| 686 |
|
| 687 |
static void |
| 688 |
ResourceOwnerReleaseInternal(ResourceOwner owner, |
| 689 |
ResourceReleasePhase phase, |
| 690 |
bool isCommit, |
| 691 |
bool isTopLevel) |
| 692 |
{ |
| 693 |
ResourceOwner child; |
| 694 |
ResourceOwner save; |
| 695 |
ResourceReleaseCallbackItem *item; |
| 696 |
ResourceReleaseCallbackItem *next; |
| 697 |
|
| 698 |
/* Recurse to handle descendants */ |
| 699 |
for (child = owner->firstchild; child != NULL; child = child->nextchild) |
| 700 |
ResourceOwnerReleaseInternal(child, phase, isCommit, isTopLevel); |
| 701 |
|
| 702 |
/* |
| 703 |
* To release the resources in the right order, sort them by phase and |
| 704 |
* priority. |
| 705 |
* |
| 706 |
* The ReleaseResource callback functions are not allowed to remember or |
| 707 |
* forget any other resources after this. Otherwise we lose track of where |
| 708 |
* we are in processing the hash/array. |
| 709 |
*/ |
| 710 |
if (!owner->releasing) |
| 711 |
{ |
| 712 |
Assert(phase == RESOURCE_RELEASE_BEFORE_LOCKS); |
| 713 |
Assert(!owner->sorted); |
| 714 |
owner->releasing = true; |
| 715 |
} |
| 716 |
else |
| 717 |
{ |
| 718 |
/* |
| 719 |
* Phase is normally > RESOURCE_RELEASE_BEFORE_LOCKS, if this is not |
| 720 |
* the first call to ResourceOwnerRelease. But if an error happens |
| 721 |
* between the release phases, we might get called again for the same |
| 722 |
* ResourceOwner from AbortTransaction. |
| 723 |
*/ |
| 724 |
} |
| 725 |
if (!owner->sorted) |
| 726 |
{ |
| 727 |
ResourceOwnerSort(owner); |
| 728 |
owner->sorted = true; |
| 729 |
} |
| 730 |
|
| 731 |
/* |
| 732 |
* Make CurrentResourceOwner point to me, so that the release callback |
| 733 |
* functions know which resource owner is been released. |
| 734 |
*/ |
| 735 |
save = CurrentResourceOwner; |
| 736 |
CurrentResourceOwner = owner; |
| 737 |
|
| 738 |
if (phase == RESOURCE_RELEASE_BEFORE_LOCKS) |
| 739 |
{ |
| 740 |
/* |
| 741 |
* Release all resources that need to be released before the locks. |
| 742 |
* |
| 743 |
* During a commit, there shouldn't be any remaining resources --- |
| 744 |
* that would indicate failure to clean up the executor correctly --- |
| 745 |
* so issue warnings. In the abort case, just clean up quietly. |
| 746 |
*/ |
| 747 |
ResourceOwnerReleaseAll(owner, phase, isCommit); |
| 748 |
|
| 749 |
while (!dlist_is_empty(&owner->aio_handles)) |
| 750 |
{ |
| 751 |
dlist_node *node = dlist_head_node(&owner->aio_handles); |
| 752 |
|
| 753 |
pgaio_io_release_resowner(node, !isCommit); |
| 754 |
} |
| 755 |
} |
| 756 |
else if (phase == RESOURCE_RELEASE_LOCKS) |
| 757 |
{ |
| 758 |
if (isTopLevel) |
| 759 |
{ |
| 760 |
/* |
| 761 |
* For a top-level xact we are going to release all locks (or at |
| 762 |
* least all non-session locks), so just do a single lmgr call at |
| 763 |
* the top of the recursion. |
| 764 |
*/ |
| 765 |
if (owner == TopTransactionResourceOwner) |
| 766 |
{ |
| 767 |
ProcReleaseLocks(isCommit); |
| 768 |
ReleasePredicateLocks(isCommit, false); |
| 769 |
} |
| 770 |
} |
| 771 |
else |
| 772 |
{ |
| 773 |
/* |
| 774 |
* Release locks retail. Note that if we are committing a |
| 775 |
* subtransaction, we do NOT release its locks yet, but transfer |
| 776 |
* them to the parent. |
| 777 |
*/ |
| 778 |
LOCALLOCK **locks; |
| 779 |
int nlocks; |
| 780 |
|
| 781 |
Assert(owner->parent != NULL); |
| 782 |
|
| 783 |
/* |
| 784 |
* Pass the list of locks owned by this resource owner to the lock |
| 785 |
* manager, unless it has overflowed. |
| 786 |
*/ |
| 787 |
if (owner->nlocks > MAX_RESOWNER_LOCKS) |
| 788 |
{ |
| 789 |
locks = NULL; |
| 790 |
nlocks = 0; |
| 791 |
} |
| 792 |
else |
| 793 |
{ |
| 794 |
locks = owner->locks; |
| 795 |
nlocks = owner->nlocks; |
| 796 |
} |
| 797 |
|
| 798 |
if (isCommit) |
| 799 |
LockReassignCurrentOwner(locks, nlocks); |
| 800 |
else |
| 801 |
LockReleaseCurrentOwner(locks, nlocks); |
| 802 |
} |
| 803 |
} |
| 804 |
else if (phase == RESOURCE_RELEASE_AFTER_LOCKS) |
| 805 |
{ |
| 806 |
/* |
| 807 |
* Release all resources that need to be released after the locks. |
| 808 |
*/ |
| 809 |
ResourceOwnerReleaseAll(owner, phase, isCommit); |
| 810 |
} |
| 811 |
|
| 812 |
/* Let add-on modules get a chance too */ |
| 813 |
for (item = ResourceRelease_callbacks; item; item = next) |
| 814 |
{ |
| 815 |
/* allow callbacks to unregister themselves when called */ |
| 816 |
next = item->next; |
| 817 |
item->callback(phase, isCommit, isTopLevel, item->arg); |
| 818 |
} |
| 819 |
|
| 820 |
CurrentResourceOwner = save; |
| 821 |
} |
| 822 |
|
| 823 |
/* |
| 824 |
* ResourceOwnerReleaseAllOfKind |
| 825 |
* Release all resources of a certain type held by this owner. |
| 826 |
*/ |
| 827 |
void |
| 828 |
ResourceOwnerReleaseAllOfKind(ResourceOwner owner, const ResourceOwnerDesc *kind) |
| 829 |
{ |
| 830 |
/* Mustn't call this after we have already started releasing resources. */ |
| 831 |
if (owner->releasing) |
| 832 |
elog(ERROR, "ResourceOwnerForget called for %s after release started", kind->name); |
| 833 |
Assert(!owner->sorted); |
| 834 |
|
| 835 |
/* |
| 836 |
* Temporarily set 'releasing', to prevent calls to ResourceOwnerRemember |
| 837 |
* while we're scanning the owner. Enlarging the hash would cause us to |
| 838 |
* lose track of the point we're scanning. |
| 839 |
*/ |
| 840 |
owner->releasing = true; |
| 841 |
|
| 842 |
/* Array first */ |
| 843 |
for (int i = 0; i < owner->narr; i++) |
| 844 |
{ |
| 845 |
if (owner->arr[i].kind == kind) |
| 846 |
{ |
| 847 |
Datum value = owner->arr[i].item; |
| 848 |
|
| 849 |
owner->arr[i] = owner->arr[owner->narr - 1]; |
| 850 |
owner->narr--; |
| 851 |
i--; |
| 852 |
|
| 853 |
kind->ReleaseResource(value); |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
/* Then hash */ |
| 858 |
for (int i = 0; i < owner->capacity; i++) |
| 859 |
{ |
| 860 |
if (owner->hash[i].kind == kind) |
| 861 |
{ |
| 862 |
Datum value = owner->hash[i].item; |
| 863 |
|
| 864 |
owner->hash[i].item = (Datum) 0; |
| 865 |
owner->hash[i].kind = NULL; |
| 866 |
owner->nhash--; |
| 867 |
|
| 868 |
kind->ReleaseResource(value); |
| 869 |
} |
| 870 |
} |
| 871 |
owner->releasing = false; |
| 872 |
} |
| 873 |
|
| 874 |
/* |
| 875 |
* ResourceOwnerDelete |
| 876 |
* Delete an owner object and its descendants. |
| 877 |
* |
| 878 |
* The caller must have already released all resources in the object tree. |
| 879 |
*/ |
| 880 |
void |
| 881 |
ResourceOwnerDelete(ResourceOwner owner) |
| 882 |
{ |
| 883 |
/* We had better not be deleting CurrentResourceOwner ... */ |
| 884 |
Assert(owner != CurrentResourceOwner); |
| 885 |
|
| 886 |
/* And it better not own any resources, either */ |
| 887 |
Assert(owner->narr == 0); |
| 888 |
Assert(owner->nhash == 0); |
| 889 |
Assert(owner->nlocks == 0 || owner->nlocks == MAX_RESOWNER_LOCKS + 1); |
| 890 |
|
| 891 |
/* |
| 892 |
* Delete children. The recursive call will delink the child from me, so |
| 893 |
* just iterate as long as there is a child. |
| 894 |
*/ |
| 895 |
while (owner->firstchild != NULL) |
| 896 |
ResourceOwnerDelete(owner->firstchild); |
| 897 |
|
| 898 |
/* |
| 899 |
* We delink the owner from its parent before deleting it, so that if |
| 900 |
* there's an error we won't have deleted/busted owners still attached to |
| 901 |
* the owner tree. Better a leak than a crash. |
| 902 |
*/ |
| 903 |
ResourceOwnerNewParent(owner, NULL); |
| 904 |
|
| 905 |
/* And free the object. */ |
| 906 |
if (owner->hash) |
| 907 |
pfree(owner->hash); |
| 908 |
pfree(owner); |
| 909 |
} |
| 910 |
|
| 911 |
/* |
| 912 |
* Fetch parent of a ResourceOwner (returns NULL if top-level owner) |
| 913 |
*/ |
| 914 |
ResourceOwner |
| 915 |
ResourceOwnerGetParent(ResourceOwner owner) |
| 916 |
{ |
| 917 |
return owner->parent; |
| 918 |
} |
| 919 |
|
| 920 |
/* |
| 921 |
* Reassign a ResourceOwner to have a new parent |
| 922 |
*/ |
| 923 |
void |
| 924 |
ResourceOwnerNewParent(ResourceOwner owner, |
| 925 |
ResourceOwner newparent) |
| 926 |
{ |
| 927 |
ResourceOwner oldparent = owner->parent; |
| 928 |
|
| 929 |
if (oldparent) |
| 930 |
{ |
| 931 |
if (owner == oldparent->firstchild) |
| 932 |
oldparent->firstchild = owner->nextchild; |
| 933 |
else |
| 934 |
{ |
| 935 |
ResourceOwner child; |
| 936 |
|
| 937 |
for (child = oldparent->firstchild; child; child = child->nextchild) |
| 938 |
{ |
| 939 |
if (owner == child->nextchild) |
| 940 |
{ |
| 941 |
child->nextchild = owner->nextchild; |
| 942 |
break; |
| 943 |
} |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
if (newparent) |
| 949 |
{ |
| 950 |
Assert(owner != newparent); |
| 951 |
owner->parent = newparent; |
| 952 |
owner->nextchild = newparent->firstchild; |
| 953 |
newparent->firstchild = owner; |
| 954 |
} |
| 955 |
else |
| 956 |
{ |
| 957 |
owner->parent = NULL; |
| 958 |
owner->nextchild = NULL; |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
/* |
| 963 |
* Register or deregister callback functions for resource cleanup |
| 964 |
* |
| 965 |
* These functions can be used by dynamically loaded modules. These used |
| 966 |
* to be the only way for an extension to register custom resource types |
| 967 |
* with a resource owner, but nowadays it is easier to define a new |
| 968 |
* ResourceOwnerDesc with custom callbacks. |
| 969 |
*/ |
| 970 |
void |
| 971 |
RegisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg) |
| 972 |
{ |
| 973 |
ResourceReleaseCallbackItem *item; |
| 974 |
|
| 975 |
item = (ResourceReleaseCallbackItem *) |
| 976 |
MemoryContextAlloc(TopMemoryContext, |
| 977 |
sizeof(ResourceReleaseCallbackItem)); |
| 978 |
item->callback = callback; |
| 979 |
item->arg = arg; |
| 980 |
item->next = ResourceRelease_callbacks; |
| 981 |
ResourceRelease_callbacks = item; |
| 982 |
} |
| 983 |
|
| 984 |
void |
| 985 |
UnregisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg) |
| 986 |
{ |
| 987 |
ResourceReleaseCallbackItem *item; |
| 988 |
ResourceReleaseCallbackItem *prev; |
| 989 |
|
| 990 |
prev = NULL; |
| 991 |
for (item = ResourceRelease_callbacks; item; prev = item, item = item->next) |
| 992 |
{ |
| 993 |
if (item->callback == callback && item->arg == arg) |
| 994 |
{ |
| 995 |
if (prev) |
| 996 |
prev->next = item->next; |
| 997 |
else |
| 998 |
ResourceRelease_callbacks = item->next; |
| 999 |
pfree(item); |
| 1000 |
break; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
/* |
| 1006 |
* Establish an AuxProcessResourceOwner for the current process. |
| 1007 |
*/ |
| 1008 |
void |
| 1009 |
CreateAuxProcessResourceOwner(void) |
| 1010 |
{ |
| 1011 |
Assert(AuxProcessResourceOwner == NULL); |
| 1012 |
Assert(CurrentResourceOwner == NULL); |
| 1013 |
AuxProcessResourceOwner = ResourceOwnerCreate(NULL, "AuxiliaryProcess"); |
| 1014 |
CurrentResourceOwner = AuxProcessResourceOwner; |
| 1015 |
|
| 1016 |
/* |
| 1017 |
* Register a shmem-exit callback for cleanup of aux-process resource |
| 1018 |
* owner. (This needs to run after, e.g., ShutdownXLOG.) |
| 1019 |
*/ |
| 1020 |
on_shmem_exit(ReleaseAuxProcessResourcesCallback, 0); |
| 1021 |
} |
| 1022 |
|
| 1023 |
/* |
| 1024 |
* Convenience routine to release all resources tracked in |
| 1025 |
* AuxProcessResourceOwner (but that resowner is not destroyed here). |
| 1026 |
* Warn about leaked resources if isCommit is true. |
| 1027 |
*/ |
| 1028 |
void |
| 1029 |
ReleaseAuxProcessResources(bool isCommit) |
| 1030 |
{ |
| 1031 |
/* |
| 1032 |
* At this writing, the only thing that could actually get released is |
| 1033 |
* buffer pins; but we may as well do the full release protocol. |
| 1034 |
*/ |
| 1035 |
ResourceOwnerRelease(AuxProcessResourceOwner, |
| 1036 |
RESOURCE_RELEASE_BEFORE_LOCKS, |
| 1037 |
isCommit, true); |
| 1038 |
ResourceOwnerRelease(AuxProcessResourceOwner, |
| 1039 |
RESOURCE_RELEASE_LOCKS, |
| 1040 |
isCommit, true); |
| 1041 |
ResourceOwnerRelease(AuxProcessResourceOwner, |
| 1042 |
RESOURCE_RELEASE_AFTER_LOCKS, |
| 1043 |
isCommit, true); |
| 1044 |
/* allow it to be reused */ |
| 1045 |
AuxProcessResourceOwner->releasing = false; |
| 1046 |
AuxProcessResourceOwner->sorted = false; |
| 1047 |
} |
| 1048 |
|
| 1049 |
/* |
| 1050 |
* Shmem-exit callback for the same. |
| 1051 |
* Warn about leaked resources if process exit code is zero (ie normal). |
| 1052 |
*/ |
| 1053 |
static void |
| 1054 |
ReleaseAuxProcessResourcesCallback(int code, Datum arg) |
| 1055 |
{ |
| 1056 |
bool isCommit = (code == 0); |
| 1057 |
|
| 1058 |
ReleaseAuxProcessResources(isCommit); |
| 1059 |
} |
| 1060 |
|
| 1061 |
/* |
| 1062 |
* Remember that a Local Lock is owned by a ResourceOwner |
| 1063 |
* |
| 1064 |
* This is different from the generic ResourceOwnerRemember in that the list of |
| 1065 |
* locks is only a lossy cache. It can hold up to MAX_RESOWNER_LOCKS entries, |
| 1066 |
* and when it overflows, we stop tracking locks. The point of only remembering |
| 1067 |
* only up to MAX_RESOWNER_LOCKS entries is that if a lot of locks are held, |
| 1068 |
* ResourceOwnerForgetLock doesn't need to scan through a large array to find |
| 1069 |
* the entry. |
| 1070 |
*/ |
| 1071 |
void |
| 1072 |
ResourceOwnerRememberLock(ResourceOwner owner, LOCALLOCK *locallock) |
| 1073 |
{ |
| 1074 |
Assert(locallock != NULL); |
| 1075 |
|
| 1076 |
if (owner->nlocks > MAX_RESOWNER_LOCKS) |
| 1077 |
return; /* we have already overflowed */ |
| 1078 |
|
| 1079 |
if (owner->nlocks < MAX_RESOWNER_LOCKS) |
| 1080 |
owner->locks[owner->nlocks] = locallock; |
| 1081 |
else |
| 1082 |
{ |
| 1083 |
/* overflowed */ |
| 1084 |
} |
| 1085 |
owner->nlocks++; |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* |
| 1089 |
* Forget that a Local Lock is owned by a ResourceOwner |
| 1090 |
*/ |
| 1091 |
void |
| 1092 |
ResourceOwnerForgetLock(ResourceOwner owner, LOCALLOCK *locallock) |
| 1093 |
{ |
| 1094 |
int i; |
| 1095 |
|
| 1096 |
if (owner->nlocks > MAX_RESOWNER_LOCKS) |
| 1097 |
return; /* we have overflowed */ |
| 1098 |
|
| 1099 |
Assert(owner->nlocks > 0); |
| 1100 |
for (i = owner->nlocks - 1; i >= 0; i--) |
| 1101 |
{ |
| 1102 |
if (locallock == owner->locks[i]) |
| 1103 |
{ |
| 1104 |
owner->locks[i] = owner->locks[owner->nlocks - 1]; |
| 1105 |
owner->nlocks--; |
| 1106 |
return; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
elog(ERROR, "lock reference %p is not owned by resource owner %s", |
| 1110 |
locallock, owner->name); |
| 1111 |
} |
| 1112 |
|
| 1113 |
void |
| 1114 |
ResourceOwnerRememberAioHandle(ResourceOwner owner, struct dlist_node *ioh_node) |
| 1115 |
{ |
| 1116 |
dlist_push_tail(&owner->aio_handles, ioh_node); |
| 1117 |
} |
| 1118 |
|
| 1119 |
void |
| 1120 |
ResourceOwnerForgetAioHandle(ResourceOwner owner, struct dlist_node *ioh_node) |
| 1121 |
{ |
| 1122 |
dlist_delete_from(&owner->aio_handles, ioh_node); |
| 1123 |
} |