Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2850896

Browse files
committed
Code review for auto-tuned effective_cache_size.
Fix integer overflow issue noted by Magnus Hagander, as well as a bunch of other infelicities in commit ee1e566 and its unreasonably large number of followups.
1 parent dd515d4 commit 2850896

File tree

7 files changed

+77
-67
lines changed

7 files changed

+77
-67
lines changed

doc/src/sgml/config.sgml

+13-15
Original file line numberDiff line numberDiff line change
@@ -2974,17 +2974,9 @@ include 'filename'
29742974
<listitem>
29752975
<para>
29762976
Sets the planner's assumption about the effective size of the
2977-
disk cache that is available to a single query. The default
2978-
setting of -1 selects a size equal to four times the size of <xref
2979-
linkend="guc-shared-buffers">, but not less than the size of one
2980-
shared buffer page, typically <literal>8kB</literal>. This value
2981-
can be set manually if the automatic choice is too large or too
2982-
small.
2983-
</para>
2984-
2985-
<para>
2986-
This value is factored into estimates of the cost of using an index;
2987-
a higher value makes it more likely index scans will be used, a
2977+
disk cache that is available to a single query. This is
2978+
factored into estimates of the cost of using an index; a
2979+
higher value makes it more likely index scans will be used, a
29882980
lower value makes it more likely sequential scans will be
29892981
used. When setting this parameter you should consider both
29902982
<productname>PostgreSQL</productname>'s shared buffers and the
@@ -2996,10 +2988,16 @@ include 'filename'
29962988
memory allocated by <productname>PostgreSQL</productname>, nor
29972989
does it reserve kernel disk cache; it is used only for estimation
29982990
purposes. The system also does not assume data remains in
2999-
the disk cache between queries. The auto-tuning
3000-
selected by the default setting of -1 should give reasonable
3001-
results if this database cluster can utilize most of the memory
3002-
on this server.
2991+
the disk cache between queries.
2992+
</para>
2993+
2994+
<para>
2995+
If <varname>effective_cache_size</> is set to -1, which is the
2996+
default, the value is replaced by an automatically selected value,
2997+
currently four times the size of <xref linkend="guc-shared-buffers">.
2998+
For recommended settings of <varname>shared_buffers</>, this should
2999+
give reasonable results if this database cluster can use most of the
3000+
memory on the server.
30033001
</para>
30043002
</listitem>
30053003
</varlistentry>

src/backend/optimizer/path/costsize.c

+59-47
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#ifdef _MSC_VER
7272
#include <float.h> /* for _isnan */
7373
#endif
74+
#include <limits.h>
7475
#include <math.h>
7576

7677
#include "access/htup_details.h"
@@ -96,13 +97,14 @@
9697

9798
#define LOG2(x) (log(x) / 0.693147180559945)
9899

100+
99101
double seq_page_cost = DEFAULT_SEQ_PAGE_COST;
100102
double random_page_cost = DEFAULT_RANDOM_PAGE_COST;
101103
double cpu_tuple_cost = DEFAULT_CPU_TUPLE_COST;
102104
double cpu_index_tuple_cost = DEFAULT_CPU_INDEX_TUPLE_COST;
103105
double cpu_operator_cost = DEFAULT_CPU_OPERATOR_COST;
104106

105-
int effective_cache_size = -1;
107+
int effective_cache_size = -1; /* will get replaced */
106108

107109
Cost disable_cost = 1.0e10;
108110

@@ -456,52 +458,6 @@ cost_index(IndexPath *path, PlannerInfo *root, double loop_count)
456458
path->path.total_cost = startup_cost + run_cost;
457459
}
458460

459-
void
460-
set_default_effective_cache_size(void)
461-
{
462-
/*
463-
* If the value of effective_cache_size is -1, use the preferred
464-
* auto-tune value.
465-
*/
466-
if (effective_cache_size == -1)
467-
{
468-
char buf[32];
469-
470-
snprintf(buf, sizeof(buf), "%d", NBuffers * DEFAULT_EFFECTIVE_CACHE_SIZE_MULTI);
471-
SetConfigOption("effective_cache_size", buf, PGC_POSTMASTER, PGC_S_OVERRIDE);
472-
}
473-
Assert(effective_cache_size > 0);
474-
}
475-
476-
/*
477-
* GUC check_hook for effective_cache_size
478-
*/
479-
bool
480-
check_effective_cache_size(int *newval, void **extra, GucSource source)
481-
{
482-
/*
483-
* -1 indicates a request for auto-tune.
484-
*/
485-
if (*newval == -1)
486-
{
487-
/*
488-
* If we haven't yet changed the boot_val default of -1, just let it
489-
* be. We'll fix it later.
490-
*/
491-
if (effective_cache_size == -1)
492-
return true;
493-
494-
/* Otherwise, substitute the auto-tune value */
495-
*newval = NBuffers * DEFAULT_EFFECTIVE_CACHE_SIZE_MULTI;
496-
}
497-
498-
/* set minimum? */
499-
if (*newval < 1)
500-
*newval = 1;
501-
502-
return true;
503-
}
504-
505461
/*
506462
* index_pages_fetched
507463
* Estimate the number of pages actually fetched after accounting for
@@ -4137,3 +4093,59 @@ page_size(double tuples, int width)
41374093
{
41384094
return ceil(relation_byte_size(tuples, width) / BLCKSZ);
41394095
}
4096+
4097+
/*
4098+
* GUC check_hook for effective_cache_size
4099+
*/
4100+
bool
4101+
check_effective_cache_size(int *newval, void **extra, GucSource source)
4102+
{
4103+
/*
4104+
* -1 is the documented way of requesting auto-tune, but we also treat
4105+
* zero as meaning that, since we don't consider zero a valid setting.
4106+
*/
4107+
if (*newval <= 0)
4108+
{
4109+
/*
4110+
* If we haven't yet changed the initial default of -1, just let it
4111+
* be. We'll fix it later on during GUC initialization, when
4112+
* set_default_effective_cache_size is called. (If we try to do it
4113+
* immediately, we may not be looking at the final value of NBuffers.)
4114+
*/
4115+
if (effective_cache_size == -1)
4116+
return true;
4117+
4118+
/*
4119+
* Otherwise, substitute the auto-tune value, being wary of overflow.
4120+
*/
4121+
if (NBuffers < INT_MAX / 4)
4122+
*newval = NBuffers * 4;
4123+
else
4124+
*newval = INT_MAX;
4125+
}
4126+
4127+
Assert(*newval > 0);
4128+
4129+
return true;
4130+
}
4131+
4132+
/*
4133+
* initialize effective_cache_size at the end of GUC startup
4134+
*/
4135+
void
4136+
set_default_effective_cache_size(void)
4137+
{
4138+
/*
4139+
* If the value of effective_cache_size is still -1 (or zero), replace it
4140+
* with the auto-tune value.
4141+
*/
4142+
if (effective_cache_size <= 0)
4143+
{
4144+
/* disable the short-circuit in check_effective_cache_size */
4145+
effective_cache_size = 0;
4146+
/* and let check_effective_cache_size() compute the setting */
4147+
SetConfigOption("effective_cache_size", "-1",
4148+
PGC_POSTMASTER, PGC_S_OVERRIDE);
4149+
}
4150+
Assert(effective_cache_size > 0);
4151+
}

src/backend/postmaster/postmaster.c

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
#include "utils/builtins.h"
119119
#include "utils/datetime.h"
120120
#include "utils/dynamic_loader.h"
121-
#include "utils/guc.h"
122121
#include "utils/memutils.h"
123122
#include "utils/ps_status.h"
124123
#include "utils/timeout.h"

src/backend/utils/misc/guc.c

+1
Original file line numberDiff line numberDiff line change
@@ -4305,6 +4305,7 @@ SelectConfigFiles(const char *userDoption, const char *progname)
43054305
*/
43064306
pg_timezone_abbrev_initialize();
43074307

4308+
/* Also install the correct value for effective_cache_size */
43084309
set_default_effective_cache_size();
43094310

43104311
/*

src/backend/utils/misc/postgresql.conf.sample

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@
279279
#cpu_tuple_cost = 0.01 # same scale as above
280280
#cpu_index_tuple_cost = 0.005 # same scale as above
281281
#cpu_operator_cost = 0.0025 # same scale as above
282-
#effective_cache_size = -1
282+
#effective_cache_size = -1 # -1 selects auto-tuned default
283283

284284
# - Genetic Query Optimizer -
285285

src/include/optimizer/cost.h

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#define DEFAULT_CPU_INDEX_TUPLE_COST 0.005
2828
#define DEFAULT_CPU_OPERATOR_COST 0.0025
2929

30-
#define DEFAULT_EFFECTIVE_CACHE_SIZE_MULTI 4
31-
3230
typedef enum
3331
{
3432
CONSTRAINT_EXCLUSION_OFF, /* do not use c_e */

src/include/utils/guc.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,10 @@ extern void assign_search_path(const char *newval, void *extra);
387387

388388
/* in access/transam/xlog.c */
389389
extern bool check_wal_buffers(int *newval, void **extra, GucSource source);
390+
extern void assign_xlog_sync_method(int new_sync_method, void *extra);
391+
392+
/* in optimizer/path/costsize.c */
390393
extern bool check_effective_cache_size(int *newval, void **extra, GucSource source);
391394
extern void set_default_effective_cache_size(void);
392-
extern void assign_xlog_sync_method(int new_sync_method, void *extra);
393395

394396
#endif /* GUC_H */

0 commit comments

Comments
 (0)