Skip to content

Commit 4db485e

Browse files
committed
Put back a backwards-compatible version of sampling support functions.
Commit 83e176e removed the longstanding support functions for block sampling without any consideration of the impact this would have on third-party FDWs. The new API is not notably more functional for FDWs than the old, so forcing them to change doesn't seem like a good thing. We can provide the old API as a wrapper (more or less) around the new one for a minimal amount of extra code.
1 parent f5916bb commit 4db485e

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

src/backend/utils/misc/sampling.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* sampling.c
44
* Relation block sampling routines.
55
*
6-
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
6+
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
*
@@ -239,3 +239,47 @@ sampler_random_fract(SamplerRandomState randstate)
239239
{
240240
return pg_erand48(randstate);
241241
}
242+
243+
244+
/*
245+
* Backwards-compatible API for block sampling
246+
*
247+
* This code is now deprecated, but since it's still in use by many FDWs,
248+
* we should keep it for awhile at least. The functionality is the same as
249+
* sampler_random_fract/reservoir_init_selection_state/reservoir_get_next_S,
250+
* except that a common random state is used across all callers.
251+
*/
252+
static ReservoirStateData oldrs;
253+
254+
double
255+
anl_random_fract(void)
256+
{
257+
/* initialize if first time through */
258+
if (oldrs.randstate[0] == 0)
259+
sampler_random_init_state(random(), oldrs.randstate);
260+
261+
/* and compute a random fraction */
262+
return sampler_random_fract(oldrs.randstate);
263+
}
264+
265+
double
266+
anl_init_selection_state(int n)
267+
{
268+
/* initialize if first time through */
269+
if (oldrs.randstate[0] == 0)
270+
sampler_random_init_state(random(), oldrs.randstate);
271+
272+
/* Initial value of W (for use when Algorithm Z is first applied) */
273+
return exp(-log(sampler_random_fract(oldrs.randstate)) / n);
274+
}
275+
276+
double
277+
anl_get_next_S(double t, int n, double *stateptr)
278+
{
279+
double result;
280+
281+
oldrs.W = *stateptr;
282+
result = reservoir_get_next_S(&oldrs, t, n);
283+
*stateptr = oldrs.W;
284+
return result;
285+
}

src/include/commands/vacuum.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,9 @@ extern void analyze_rel(Oid relid, RangeVar *relation, int options,
198198
BufferAccessStrategy bstrategy);
199199
extern bool std_typanalyze(VacAttrStats *stats);
200200

201+
/* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */
202+
extern double anl_random_fract(void);
203+
extern double anl_init_selection_state(int n);
204+
extern double anl_get_next_S(double t, int n, double *stateptr);
205+
201206
#endif /* VACUUM_H */

src/include/utils/sampling.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* sampling.h
44
* definitions for sampling functions
55
*
6-
* Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
6+
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* src/include/utils/sampling.h
@@ -13,7 +13,8 @@
1313
#ifndef SAMPLING_H
1414
#define SAMPLING_H
1515

16-
#include "storage/bufmgr.h"
16+
#include "storage/block.h" /* for typedef BlockNumber */
17+
1718

1819
/* Random generator for sampling code */
1920
typedef unsigned short SamplerRandomState[3];
@@ -23,6 +24,7 @@ extern void sampler_random_init_state(long seed,
2324
extern double sampler_random_fract(SamplerRandomState randstate);
2425

2526
/* Block sampling methods */
27+
2628
/* Data structure for Algorithm S from Knuth 3.4.2 */
2729
typedef struct
2830
{
@@ -40,7 +42,8 @@ extern void BlockSampler_Init(BlockSampler bs, BlockNumber nblocks,
4042
extern bool BlockSampler_HasMore(BlockSampler bs);
4143
extern BlockNumber BlockSampler_Next(BlockSampler bs);
4244

43-
/* Reservoid sampling methods */
45+
/* Reservoir sampling methods */
46+
4447
typedef struct
4548
{
4649
double W;
@@ -52,4 +55,11 @@ typedef ReservoirStateData *ReservoirState;
5255
extern void reservoir_init_selection_state(ReservoirState rs, int n);
5356
extern double reservoir_get_next_S(ReservoirState rs, double t, int n);
5457

58+
/* Old API, still in use by assorted FDWs */
59+
/* For backwards compatibility, these declarations are duplicated in vacuum.h */
60+
61+
extern double anl_random_fract(void);
62+
extern double anl_init_selection_state(int n);
63+
extern double anl_get_next_S(double t, int n, double *stateptr);
64+
5565
#endif /* SAMPLING_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy