@@ -1023,9 +1023,6 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
1023
1023
forkNum , strategy , flags );
1024
1024
}
1025
1025
1026
- /* Make sure we will have room to remember the buffer pin */
1027
- ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
1028
-
1029
1026
TRACE_POSTGRESQL_BUFFER_READ_START (forkNum , blockNum ,
1030
1027
smgr -> smgr_rlocator .locator .spcOid ,
1031
1028
smgr -> smgr_rlocator .locator .dbOid ,
@@ -1230,6 +1227,10 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
1230
1227
BufferDesc * victim_buf_hdr ;
1231
1228
uint32 victim_buf_state ;
1232
1229
1230
+ /* Make sure we will have room to remember the buffer pin */
1231
+ ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
1232
+ ReservePrivateRefCountEntry ();
1233
+
1233
1234
/* create a tag so we can lookup the buffer */
1234
1235
InitBufferTag (& newTag , & smgr -> smgr_rlocator .locator , forkNum , blockNum );
1235
1236
@@ -1591,7 +1592,7 @@ GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context)
1591
1592
1592
1593
/*
1593
1594
* Ensure, while the spinlock's not yet held, that there's a free refcount
1594
- * entry.
1595
+ * entry, and a resource owner slot for the pin .
1595
1596
*/
1596
1597
ReservePrivateRefCountEntry ();
1597
1598
ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
@@ -1859,9 +1860,6 @@ ExtendBufferedRelShared(BufferManagerRelation bmr,
1859
1860
MemSet ((char * ) buf_block , 0 , BLCKSZ );
1860
1861
}
1861
1862
1862
- /* in case we need to pin an existing buffer below */
1863
- ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
1864
-
1865
1863
/*
1866
1864
* Lock relation against concurrent extensions, unless requested not to.
1867
1865
*
@@ -1947,6 +1945,10 @@ ExtendBufferedRelShared(BufferManagerRelation bmr,
1947
1945
LWLock * partition_lock ;
1948
1946
int existing_id ;
1949
1947
1948
+ /* in case we need to pin an existing buffer below */
1949
+ ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
1950
+ ReservePrivateRefCountEntry ();
1951
+
1950
1952
InitBufferTag (& tag , & bmr .smgr -> smgr_rlocator .locator , fork , first_block + i );
1951
1953
hash = BufTableHashCode (& tag );
1952
1954
partition_lock = BufMappingPartitionLock (hash );
@@ -2281,7 +2283,8 @@ ReleaseAndReadBuffer(Buffer buffer,
2281
2283
* taking the buffer header lock; instead update the state variable in loop of
2282
2284
* CAS operations. Hopefully it's just a single CAS.
2283
2285
*
2284
- * Note that ResourceOwnerEnlargeBuffers must have been done already.
2286
+ * Note that ResourceOwnerEnlargeBuffers and ReservePrivateRefCountEntry()
2287
+ * must have been done already.
2285
2288
*
2286
2289
* Returns true if buffer is BM_VALID, else false. This provision allows
2287
2290
* some callers to avoid an extra spinlock cycle.
@@ -2294,6 +2297,7 @@ PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy)
2294
2297
PrivateRefCountEntry * ref ;
2295
2298
2296
2299
Assert (!BufferIsLocal (b ));
2300
+ Assert (ReservedRefCountEntry != NULL );
2297
2301
2298
2302
ref = GetPrivateRefCountEntry (b , true);
2299
2303
@@ -2302,7 +2306,6 @@ PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy)
2302
2306
uint32 buf_state ;
2303
2307
uint32 old_buf_state ;
2304
2308
2305
- ReservePrivateRefCountEntry ();
2306
2309
ref = NewPrivateRefCountEntry (b );
2307
2310
2308
2311
old_buf_state = pg_atomic_read_u32 (& buf -> state );
@@ -2375,7 +2378,8 @@ PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy)
2375
2378
* The spinlock is released before return.
2376
2379
*
2377
2380
* As this function is called with the spinlock held, the caller has to
2378
- * previously call ReservePrivateRefCountEntry().
2381
+ * previously call ReservePrivateRefCountEntry() and
2382
+ * ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
2379
2383
*
2380
2384
* Currently, no callers of this function want to modify the buffer's
2381
2385
* usage_count at all, so there's no need for a strategy parameter.
@@ -2550,9 +2554,6 @@ BufferSync(int flags)
2550
2554
int mask = BM_DIRTY ;
2551
2555
WritebackContext wb_context ;
2552
2556
2553
- /* Make sure we can handle the pin inside SyncOneBuffer */
2554
- ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
2555
-
2556
2557
/*
2557
2558
* Unless this is a shutdown checkpoint or we have been explicitly told,
2558
2559
* we write only permanent, dirty buffers. But at shutdown or end of
@@ -3029,9 +3030,6 @@ BgBufferSync(WritebackContext *wb_context)
3029
3030
* requirements, or hit the bgwriter_lru_maxpages limit.
3030
3031
*/
3031
3032
3032
- /* Make sure we can handle the pin inside SyncOneBuffer */
3033
- ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
3034
-
3035
3033
num_to_scan = bufs_to_lap ;
3036
3034
num_written = 0 ;
3037
3035
reusable_buffers = reusable_buffers_est ;
@@ -3113,8 +3111,6 @@ BgBufferSync(WritebackContext *wb_context)
3113
3111
*
3114
3112
* (BUF_WRITTEN could be set in error if FlushBuffer finds the buffer clean
3115
3113
* after locking it, but we don't care all that much.)
3116
- *
3117
- * Note: caller must have done ResourceOwnerEnlargeBuffers.
3118
3114
*/
3119
3115
static int
3120
3116
SyncOneBuffer (int buf_id , bool skip_recently_used , WritebackContext * wb_context )
@@ -3124,7 +3120,9 @@ SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context)
3124
3120
uint32 buf_state ;
3125
3121
BufferTag tag ;
3126
3122
3123
+ /* Make sure we can handle the pin */
3127
3124
ReservePrivateRefCountEntry ();
3125
+ ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
3128
3126
3129
3127
/*
3130
3128
* Check whether buffer needs writing.
@@ -4169,9 +4167,6 @@ FlushRelationBuffers(Relation rel)
4169
4167
return ;
4170
4168
}
4171
4169
4172
- /* Make sure we can handle the pin inside the loop */
4173
- ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
4174
-
4175
4170
for (i = 0 ; i < NBuffers ; i ++ )
4176
4171
{
4177
4172
uint32 buf_state ;
@@ -4185,7 +4180,9 @@ FlushRelationBuffers(Relation rel)
4185
4180
if (!BufTagMatchesRelFileLocator (& bufHdr -> tag , & rel -> rd_locator ))
4186
4181
continue ;
4187
4182
4183
+ /* Make sure we can handle the pin */
4188
4184
ReservePrivateRefCountEntry ();
4185
+ ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
4189
4186
4190
4187
buf_state = LockBufHdr (bufHdr );
4191
4188
if (BufTagMatchesRelFileLocator (& bufHdr -> tag , & rel -> rd_locator ) &&
@@ -4242,9 +4239,6 @@ FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
4242
4239
if (use_bsearch )
4243
4240
pg_qsort (srels , nrels , sizeof (SMgrSortArray ), rlocator_comparator );
4244
4241
4245
- /* Make sure we can handle the pin inside the loop */
4246
- ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
4247
-
4248
4242
for (i = 0 ; i < NBuffers ; i ++ )
4249
4243
{
4250
4244
SMgrSortArray * srelent = NULL ;
@@ -4283,7 +4277,9 @@ FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
4283
4277
if (srelent == NULL )
4284
4278
continue ;
4285
4279
4280
+ /* Make sure we can handle the pin */
4286
4281
ReservePrivateRefCountEntry ();
4282
+ ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
4287
4283
4288
4284
buf_state = LockBufHdr (bufHdr );
4289
4285
if (BufTagMatchesRelFileLocator (& bufHdr -> tag , & srelent -> rlocator ) &&
@@ -4478,9 +4474,6 @@ FlushDatabaseBuffers(Oid dbid)
4478
4474
int i ;
4479
4475
BufferDesc * bufHdr ;
4480
4476
4481
- /* Make sure we can handle the pin inside the loop */
4482
- ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
4483
-
4484
4477
for (i = 0 ; i < NBuffers ; i ++ )
4485
4478
{
4486
4479
uint32 buf_state ;
@@ -4494,7 +4487,9 @@ FlushDatabaseBuffers(Oid dbid)
4494
4487
if (bufHdr -> tag .dbOid != dbid )
4495
4488
continue ;
4496
4489
4490
+ /* Make sure we can handle the pin */
4497
4491
ReservePrivateRefCountEntry ();
4492
+ ResourceOwnerEnlargeBuffers (CurrentResourceOwner );
4498
4493
4499
4494
buf_state = LockBufHdr (bufHdr );
4500
4495
if (bufHdr -> tag .dbOid == dbid &&
0 commit comments