PostgreSQL Source Code git master
Loading...
Searching...
No Matches
system.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/tsmapi.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "optimizer/optimizer.h"
#include "utils/fmgrprotos.h"
Include dependency graph for system.c:

Go to the source code of this file.

Data Structures

struct  SystemSamplerData
 

Functions

static void system_samplescangetsamplesize (PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
 
static void system_initsamplescan (SampleScanState *node, int eflags)
 
static void system_beginsamplescan (SampleScanState *node, Datum *params, int nparams, uint32 seed)
 
static BlockNumber system_nextsampleblock (SampleScanState *node, BlockNumber nblocks)
 
static OffsetNumber system_nextsampletuple (SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
 
Datum tsm_system_handler (PG_FUNCTION_ARGS)
 

Function Documentation

◆ system_beginsamplescan()

static void system_beginsamplescan ( SampleScanState node,
Datum params,
int  nparams,
uint32  seed 
)
static

Definition at line 139 of file system.c.

143{
145 double percent = DatumGetFloat4(params[0]);
146 double dcutoff;
147
151 errmsg("sample percentage must be between 0 and 100")));
152
153 /*
154 * The cutoff is sample probability times (PG_UINT32_MAX + 1); we have to
155 * store that as a uint64, of course. Note that this gives strictly
156 * correct behavior at the limits of zero or one probability.
157 */
158 dcutoff = rint(((double) PG_UINT32_MAX + 1) * percent / 100);
159 sampler->cutoff = (uint64) dcutoff;
160 sampler->seed = seed;
161 sampler->nextblock = 0;
163
164 /*
165 * Bulkread buffer access strategy probably makes sense unless we're
166 * scanning a very small fraction of the table. The 1% cutoff here is a
167 * guess. We should use pagemode visibility checking, since we scan all
168 * tuples on each selected page.
169 */
170 node->use_bulkread = (percent >= 1);
171 node->use_pagemode = true;
172}
#define PG_UINT32_MAX
Definition c.h:604
uint64_t uint64
Definition c.h:547
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define InvalidOffsetNumber
Definition off.h:26
static float4 DatumGetFloat4(Datum X)
Definition postgres.h:461
static int fb(int x)

References DatumGetFloat4(), ereport, errcode(), errmsg(), ERROR, fb(), InvalidOffsetNumber, PG_UINT32_MAX, SampleScanState::tsm_state, SampleScanState::use_bulkread, and SampleScanState::use_pagemode.

Referenced by tsm_system_handler().

◆ system_initsamplescan()

static void system_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 130 of file system.c.

131{
133}
#define palloc0_object(type)
Definition fe_memutils.h:75

References palloc0_object, and SampleScanState::tsm_state.

Referenced by tsm_system_handler().

◆ system_nextsampleblock()

static BlockNumber system_nextsampleblock ( SampleScanState node,
BlockNumber  nblocks 
)
static

Definition at line 178 of file system.c.

179{
181 BlockNumber nextblock = sampler->nextblock;
182 uint32 hashinput[2];
183
184 /*
185 * We compute the hash by applying hash_any to an array of 2 uint32's
186 * containing the block number and seed. This is efficient to set up, and
187 * with the current implementation of hash_any, it gives
188 * machine-independent results, which is a nice property for regression
189 * testing.
190 *
191 * These words in the hash input are the same throughout the block:
192 */
193 hashinput[1] = sampler->seed;
194
195 /*
196 * Loop over block numbers until finding suitable block or reaching end of
197 * relation.
198 */
199 for (; nextblock < nblocks; nextblock++)
200 {
201 uint32 hash;
202
203 hashinput[0] = nextblock;
204
205 hash = DatumGetUInt32(hash_any((const unsigned char *) hashinput,
206 (int) sizeof(hashinput)));
207 if (hash < sampler->cutoff)
208 break;
209 }
210
211 if (nextblock < nblocks)
212 {
213 /* Found a suitable block; remember where we should start next time */
214 sampler->nextblock = nextblock + 1;
215 return nextblock;
216 }
217
218 /* Done, but let's reset nextblock to 0 for safety. */
219 sampler->nextblock = 0;
220 return InvalidBlockNumber;
221}
uint32 BlockNumber
Definition block.h:31
#define InvalidBlockNumber
Definition block.h:33
uint32_t uint32
Definition c.h:546
static Datum hash_any(const unsigned char *k, int keylen)
Definition hashfn.h:31
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:232
static unsigned hash(unsigned *uv, int n)
Definition rege_dfa.c:715

References DatumGetUInt32(), fb(), hash(), hash_any(), InvalidBlockNumber, and SampleScanState::tsm_state.

Referenced by tsm_system_handler().

◆ system_nextsampletuple()

static OffsetNumber system_nextsampletuple ( SampleScanState node,
BlockNumber  blockno,
OffsetNumber  maxoffset 
)
static

Definition at line 236 of file system.c.

239{
242
243 /* Advance to next possible offset on page */
246 else
247 tupoffset++;
248
249 /* Done? */
250 if (tupoffset > maxoffset)
252
253 sampler->lt = tupoffset;
254
255 return tupoffset;
256}
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27

References fb(), FirstOffsetNumber, InvalidOffsetNumber, and SampleScanState::tsm_state.

Referenced by tsm_system_handler().

◆ system_samplescangetsamplesize()

static void system_samplescangetsamplesize ( PlannerInfo root,
RelOptInfo baserel,
List paramexprs,
BlockNumber pages,
double tuples 
)
static

Definition at line 88 of file system.c.

93{
96
97 /* Try to extract an estimate for the sample percentage */
100
101 if (IsA(pctnode, Const) &&
102 !((Const *) pctnode)->constisnull)
103 {
105 if (samplefract >= 0 && samplefract <= 100 && !isnan(samplefract))
106 samplefract /= 100.0f;
107 else
108 {
109 /* Default samplefract if the value is bogus */
110 samplefract = 0.1f;
111 }
112 }
113 else
114 {
115 /* Default samplefract if we didn't obtain a non-null Const */
116 samplefract = 0.1f;
117 }
118
119 /* We'll visit a sample of the pages ... */
120 *pages = clamp_row_est(baserel->pages * samplefract);
121
122 /* ... and hopefully get a representative number of tuples from them */
123 *tuples = clamp_row_est(baserel->tuples * samplefract);
124}
float float4
Definition c.h:643
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition clauses.c:2408
double clamp_row_est(double nrows)
Definition costsize.c:213
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define linitial(l)
Definition pg_list.h:178
tree ctl root
Definition radixtree.h:1857
Definition nodes.h:135

References clamp_row_est(), DatumGetFloat4(), estimate_expression_value(), fb(), IsA, linitial, and root.

Referenced by tsm_system_handler().

◆ tsm_system_handler()

Datum tsm_system_handler ( PG_FUNCTION_ARGS  )

Definition at line 67 of file system.c.

68{
70
71 tsm->parameterTypes = list_make1_oid(FLOAT4OID);
72 tsm->repeatable_across_queries = true;
73 tsm->repeatable_across_scans = true;
74 tsm->SampleScanGetSampleSize = system_samplescangetsamplesize;
75 tsm->InitSampleScan = system_initsamplescan;
76 tsm->BeginSampleScan = system_beginsamplescan;
77 tsm->NextSampleBlock = system_nextsampleblock;
78 tsm->NextSampleTuple = system_nextsampletuple;
79 tsm->EndSampleScan = NULL;
80
82}
static OffsetNumber system_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
Definition system.c:236
static void system_initsamplescan(SampleScanState *node, int eflags)
Definition system.c:130
static void system_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
Definition system.c:139
static BlockNumber system_nextsampleblock(SampleScanState *node, BlockNumber nblocks)
Definition system.c:178
static void system_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
Definition system.c:88
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define makeNode(_type_)
Definition nodes.h:161
#define list_make1_oid(x1)
Definition pg_list.h:242

References fb(), list_make1_oid, makeNode, PG_RETURN_POINTER, system_beginsamplescan(), system_initsamplescan(), system_nextsampleblock(), system_nextsampletuple(), and system_samplescangetsamplesize().