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

Commitcddc4dc

Browse files
committed
Avoid portability issues in autoprewarm.c.
autoprewarm.c mostly considered the number of blocks it might be dealingwith as being int64. This is unnecessary, because NBuffers is declaredas int, and there's been no suggestion that we might widen it in theforeseeable future. Moreover, using int64 is problematic because thecode expected INT64_FORMAT to work with fscanf(), something we don'tguarantee, and which indeed fails on some older buildfarm members.On top of that, the module randomly used uint32 rather than int64 variablesto hold block counters in several places, so it would fail anyway if weever did have NBuffers wider than that; and it also supposed that pg_qsortcould sort an int64 number of elements, which is wrong on 32-bit machines(though no doubt a 32-bit machine couldn't actually have that manybuffers).Hence, change all these variables to plain int.In passing, avoid shadowing one variable named i with another,and avoid casting away const in apw_compare_blockinfo.Discussion:https://postgr.es/m/7773.1525288909@sss.pgh.pa.us
1 parentac7a7e3 commitcddc4dc

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

‎contrib/pg_prewarm/autoprewarm.c

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include"postgres.h"
28+
2829
#include<unistd.h>
2930

3031
#include"access/heapam.h"
@@ -73,9 +74,9 @@ typedef struct AutoPrewarmSharedState
7374
/* Following items are for communication with per-database worker */
7475
dsm_handleblock_info_handle;
7576
Oiddatabase;
76-
int64prewarm_start_idx;
77-
int64prewarm_stop_idx;
78-
int64prewarmed_blocks;
77+
intprewarm_start_idx;
78+
intprewarm_stop_idx;
79+
intprewarmed_blocks;
7980
}AutoPrewarmSharedState;
8081

8182
void_PG_init(void);
@@ -86,7 +87,7 @@ PG_FUNCTION_INFO_V1(autoprewarm_start_worker);
8687
PG_FUNCTION_INFO_V1(autoprewarm_dump_now);
8788

8889
staticvoidapw_load_buffers(void);
89-
staticint64apw_dump_now(boolis_bgworker,booldump_unlogged);
90+
staticintapw_dump_now(boolis_bgworker,booldump_unlogged);
9091
staticvoidapw_start_master_worker(void);
9192
staticvoidapw_start_database_worker(void);
9293
staticboolapw_init_shmem(void);
@@ -274,7 +275,7 @@ static void
274275
apw_load_buffers(void)
275276
{
276277
FILE*file=NULL;
277-
int64num_elements,
278+
intnum_elements,
278279
i;
279280
BlockInfoRecord*blkinfo;
280281
dsm_segment*seg;
@@ -317,7 +318,7 @@ apw_load_buffers(void)
317318
}
318319

319320
/* First line of the file is a record count. */
320-
if (fscanf(file,"<<"INT64_FORMAT">>\n",&num_elements)!=1)
321+
if (fscanf(file,"<<%d>>\n",&num_elements)!=1)
321322
ereport(ERROR,
322323
(errcode_for_file_access(),
323324
errmsg("could not read from file \"%s\": %m",
@@ -336,7 +337,7 @@ apw_load_buffers(void)
336337
&blkinfo[i].tablespace,&blkinfo[i].filenode,
337338
&forknum,&blkinfo[i].blocknum)!=5)
338339
ereport(ERROR,
339-
(errmsg("autoprewarm block dump file is corrupted at line"INT64_FORMAT,
340+
(errmsg("autoprewarm block dump file is corrupted at line%d",
340341
i+1)));
341342
blkinfo[i].forknum=forknum;
342343
}
@@ -355,28 +356,28 @@ apw_load_buffers(void)
355356
/* Get the info position of the first block of the next database. */
356357
while (apw_state->prewarm_start_idx<num_elements)
357358
{
358-
uint32i=apw_state->prewarm_start_idx;
359-
Oidcurrent_db=blkinfo[i].database;
359+
intj=apw_state->prewarm_start_idx;
360+
Oidcurrent_db=blkinfo[j].database;
360361

361362
/*
362363
* Advance the prewarm_stop_idx to the first BlockRecordInfo that does
363364
* not belong to this database.
364365
*/
365-
i++;
366-
while (i<num_elements)
366+
j++;
367+
while (j<num_elements)
367368
{
368-
if (current_db!=blkinfo[i].database)
369+
if (current_db!=blkinfo[j].database)
369370
{
370371
/*
371372
* Combine BlockRecordInfos for global objects with those of
372373
* the database.
373374
*/
374375
if (current_db!=InvalidOid)
375376
break;
376-
current_db=blkinfo[i].database;
377+
current_db=blkinfo[j].database;
377378
}
378379

379-
i++;
380+
j++;
380381
}
381382

382383
/*
@@ -388,7 +389,7 @@ apw_load_buffers(void)
388389
break;
389390

390391
/* Configure stop point and database for next per-database worker. */
391-
apw_state->prewarm_stop_idx=i;
392+
apw_state->prewarm_stop_idx=j;
392393
apw_state->database=current_db;
393394
Assert(apw_state->prewarm_start_idx<apw_state->prewarm_stop_idx);
394395

@@ -415,8 +416,7 @@ apw_load_buffers(void)
415416

416417
/* Report our success. */
417418
ereport(LOG,
418-
(errmsg("autoprewarm successfully prewarmed "INT64_FORMAT
419-
" of "INT64_FORMAT" previously-loaded blocks",
419+
(errmsg("autoprewarm successfully prewarmed %d of %d previously-loaded blocks",
420420
apw_state->prewarmed_blocks,num_elements)));
421421
}
422422

@@ -427,7 +427,7 @@ apw_load_buffers(void)
427427
void
428428
autoprewarm_database_main(Datummain_arg)
429429
{
430-
uint32pos;
430+
intpos;
431431
BlockInfoRecord*block_info;
432432
Relationrel=NULL;
433433
BlockNumbernblocks=0;
@@ -557,13 +557,14 @@ autoprewarm_database_main(Datum main_arg)
557557
* Dump information on blocks in shared buffers. We use a text format here
558558
* so that it's easy to understand and even change the file contents if
559559
* necessary.
560+
* Returns the number of blocks dumped.
560561
*/
561-
staticint64
562+
staticint
562563
apw_dump_now(boolis_bgworker,booldump_unlogged)
563564
{
564-
uint32i;
565+
intnum_blocks;
566+
inti;
565567
intret;
566-
int64num_blocks;
567568
BlockInfoRecord*block_info_array;
568569
BufferDesc*bufHdr;
569570
FILE*file;
@@ -630,7 +631,7 @@ apw_dump_now(bool is_bgworker, bool dump_unlogged)
630631
errmsg("could not open file \"%s\": %m",
631632
transient_dump_file_path)));
632633

633-
ret=fprintf(file,"<<"INT64_FORMAT">>\n",num_blocks);
634+
ret=fprintf(file,"<<%d>>\n",num_blocks);
634635
if (ret<0)
635636
{
636637
intsave_errno=errno;
@@ -691,8 +692,7 @@ apw_dump_now(bool is_bgworker, bool dump_unlogged)
691692
apw_state->pid_using_dumpfile=InvalidPid;
692693

693694
ereport(DEBUG1,
694-
(errmsg("wrote block details for "INT64_FORMAT" blocks",
695-
num_blocks)));
695+
(errmsg("wrote block details for %d blocks",num_blocks)));
696696
returnnum_blocks;
697697
}
698698

@@ -727,11 +727,14 @@ autoprewarm_start_worker(PG_FUNCTION_ARGS)
727727

728728
/*
729729
* SQL-callable function to perform an immediate block dump.
730+
*
731+
* Note: this is declared to return int8, as insurance against some
732+
* very distant day when we might make NBuffers wider than int.
730733
*/
731734
Datum
732735
autoprewarm_dump_now(PG_FUNCTION_ARGS)
733736
{
734-
int64num_blocks;
737+
intnum_blocks;
735738

736739
apw_init_shmem();
737740

@@ -741,7 +744,7 @@ autoprewarm_dump_now(PG_FUNCTION_ARGS)
741744
}
742745
PG_END_ENSURE_ERROR_CLEANUP(apw_detach_shmem,0);
743746

744-
PG_RETURN_INT64(num_blocks);
747+
PG_RETURN_INT64((int64)num_blocks);
745748
}
746749

747750
/*
@@ -869,7 +872,7 @@ do { \
869872
return -1;\
870873
else if (a->fld > b->fld)\
871874
return 1;\
872-
} while(0);
875+
} while(0)
873876

874877
/*
875878
* apw_compare_blockinfo
@@ -883,8 +886,8 @@ do { \
883886
staticint
884887
apw_compare_blockinfo(constvoid*p,constvoid*q)
885888
{
886-
BlockInfoRecord*a= (BlockInfoRecord*)p;
887-
BlockInfoRecord*b= (BlockInfoRecord*)q;
889+
constBlockInfoRecord*a= (constBlockInfoRecord*)p;
890+
constBlockInfoRecord*b= (constBlockInfoRecord*)q;
888891

889892
cmp_member_elem(database);
890893
cmp_member_elem(tablespace);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp