diff options
| author | Tom Lane | 2015-07-25 18:39:00 +0000 |
|---|---|---|
| committer | Tom Lane | 2015-07-25 18:39:00 +0000 |
| commit | dd7a8f66ed278eef2f001a98e2312336c61ee527 (patch) | |
| tree | 4daf4c4b1daddc8fc31d7448522b9e66f4369fd7 /contrib | |
| parent | b26e3d660df51a088d14c3c2cfce5990c13c1195 (diff) | |
Redesign tablesample method API, and do extensive code review.
The original implementation of TABLESAMPLE modeled the tablesample method
API on index access methods, which wasn't a good choice because, without
specialized DDL commands, there's no way to build an extension that can
implement a TSM. (Raw inserts into system catalogs are not an acceptable
thing to do, because we can't undo them during DROP EXTENSION, nor will
pg_upgrade behave sanely.) Instead adopt an API more like procedural
language handlers or foreign data wrappers, wherein the only SQL-level
support object needed is a single handler function identified by having
a special return type. This lets us get rid of the supporting catalog
altogether, so that no custom DDL support is needed for the feature.
Adjust the API so that it can support non-constant tablesample arguments
(the original coding assumed we could evaluate the argument expressions at
ExecInitSampleScan time, which is undesirable even if it weren't outright
unsafe), and discourage sampling methods from looking at invisible tuples.
Make sure that the BERNOULLI and SYSTEM methods are genuinely repeatable
within and across queries, as required by the SQL standard, and deal more
honestly with methods that can't support that requirement.
Make a full code-review pass over the tablesample additions, and fix
assorted bugs, omissions, infelicities, and cosmetic issues (such as
failure to put the added code stanzas in a consistent ordering).
Improve EXPLAIN's output of tablesample plans, too.
Back-patch to 9.5 so that we don't have to support the original API
in production.
Diffstat (limited to 'contrib')
| -rw-r--r-- | contrib/pg_stat_statements/pg_stat_statements.c | 10 | ||||
| -rw-r--r-- | contrib/tsm_system_rows/Makefile | 4 | ||||
| -rw-r--r-- | contrib/tsm_system_rows/expected/tsm_system_rows.out | 96 | ||||
| -rw-r--r-- | contrib/tsm_system_rows/sql/tsm_system_rows.sql | 41 | ||||
| -rw-r--r-- | contrib/tsm_system_rows/tsm_system_rows--1.0.sql | 43 | ||||
| -rw-r--r-- | contrib/tsm_system_rows/tsm_system_rows.c | 445 | ||||
| -rw-r--r-- | contrib/tsm_system_rows/tsm_system_rows.control | 2 | ||||
| -rw-r--r-- | contrib/tsm_system_time/Makefile | 4 | ||||
| -rw-r--r-- | contrib/tsm_system_time/expected/tsm_system_time.out | 138 | ||||
| -rw-r--r-- | contrib/tsm_system_time/sql/tsm_system_time.sql | 53 | ||||
| -rw-r--r-- | contrib/tsm_system_time/tsm_system_time--1.0.sql | 38 | ||||
| -rw-r--r-- | contrib/tsm_system_time/tsm_system_time.c | 453 | ||||
| -rw-r--r-- | contrib/tsm_system_time/tsm_system_time.control | 2 |
13 files changed, 799 insertions, 530 deletions
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index 0eb991cdf0e..59b8a2e2b3d 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -2297,6 +2297,7 @@ JumbleRangeTable(pgssJumbleState *jstate, List *rtable) { case RTE_RELATION: APP_JUMB(rte->relid); + JumbleExpr(jstate, (Node *) rte->tablesample); break; case RTE_SUBQUERY: JumbleQuery(jstate, rte->subquery); @@ -2767,6 +2768,15 @@ JumbleExpr(pgssJumbleState *jstate, Node *node) JumbleExpr(jstate, rtfunc->funcexpr); } break; + case T_TableSampleClause: + { + TableSampleClause *tsc = (TableSampleClause *) node; + + APP_JUMB(tsc->tsmhandler); + JumbleExpr(jstate, (Node *) tsc->args); + JumbleExpr(jstate, (Node *) tsc->repeatable); + } + break; default: /* Only a warning, since we can stumble along anyway */ elog(WARNING, "unrecognized node type: %d", diff --git a/contrib/tsm_system_rows/Makefile b/contrib/tsm_system_rows/Makefile index 700ab276db2..609af463c5c 100644 --- a/contrib/tsm_system_rows/Makefile +++ b/contrib/tsm_system_rows/Makefile @@ -1,8 +1,8 @@ -# src/test/modules/tsm_system_rows/Makefile +# contrib/tsm_system_rows/Makefile MODULE_big = tsm_system_rows OBJS = tsm_system_rows.o $(WIN32RES) -PGFILEDESC = "tsm_system_rows - SYSTEM TABLESAMPLE method which accepts number of rows as a limit" +PGFILEDESC = "tsm_system_rows - TABLESAMPLE method which accepts number of rows as a limit" EXTENSION = tsm_system_rows DATA = tsm_system_rows--1.0.sql diff --git a/contrib/tsm_system_rows/expected/tsm_system_rows.out b/contrib/tsm_system_rows/expected/tsm_system_rows.out index 7e0f72b02b7..87b4a8fc64b 100644 --- a/contrib/tsm_system_rows/expected/tsm_system_rows.out +++ b/contrib/tsm_system_rows/expected/tsm_system_rows.out @@ -1,31 +1,83 @@ CREATE EXTENSION tsm_system_rows; -CREATE TABLE test_tablesample (id int, name text) WITH (fillfactor=10); -- force smaller pages so we don't have to load too much data to get multiple pages -INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) FROM generate_series(0, 30) s(i) ORDER BY i; +CREATE TABLE test_tablesample (id int, name text); +INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) + FROM generate_series(0, 30) s(i); ANALYZE test_tablesample; -SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (1000); +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (0); + count +------- + 0 +(1 row) + +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (1); + count +------- + 1 +(1 row) + +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (10); + count +------- + 10 +(1 row) + +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (100); count ------- 31 (1 row) -SELECT id FROM test_tablesample TABLESAMPLE system_rows (8) REPEATABLE (5432); - id ----- - 7 - 14 - 21 - 28 - 4 - 11 - 18 - 25 -(8 rows) - -EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_rows (20) REPEATABLE (10); - QUERY PLAN ------------------------------------------------------------------------------------ - Sample Scan (system_rows) on test_tablesample (cost=0.00..80.20 rows=20 width=4) +-- bad parameters should get through planning, but not execution: +EXPLAIN (COSTS OFF) +SELECT id FROM test_tablesample TABLESAMPLE system_rows (-1); + QUERY PLAN +---------------------------------------- + Sample Scan on test_tablesample + Sampling: system_rows ('-1'::bigint) +(2 rows) + +SELECT id FROM test_tablesample TABLESAMPLE system_rows (-1); +ERROR: sample size must not be negative +-- fail, this method is not repeatable: +SELECT * FROM test_tablesample TABLESAMPLE system_rows (10) REPEATABLE (0); +ERROR: tablesample method system_rows does not support REPEATABLE +LINE 1: SELECT * FROM test_tablesample TABLESAMPLE system_rows (10) ... + ^ +-- but a join should be allowed: +EXPLAIN (COSTS OFF) +SELECT * FROM + (VALUES (0),(10),(100)) v(nrows), + LATERAL (SELECT count(*) FROM test_tablesample + TABLESAMPLE system_rows (nrows)) ss; + QUERY PLAN +---------------------------------------------------------- + Nested Loop + -> Values Scan on "*VALUES*" + -> Aggregate + -> Sample Scan on test_tablesample + Sampling: system_rows ("*VALUES*".column1) +(5 rows) + +SELECT * FROM + (VALUES (0),(10),(100)) v(nrows), + LATERAL (SELECT count(*) FROM test_tablesample + TABLESAMPLE system_rows (nrows)) ss; + nrows | count +-------+------- + 0 | 0 + 10 | 10 + 100 | 31 +(3 rows) + +CREATE VIEW vv AS + SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (20); +SELECT * FROM vv; + count +------- + 20 (1 row) --- done -DROP TABLE test_tablesample CASCADE; +DROP EXTENSION tsm_system_rows; -- fail, view depends on extension +ERROR: cannot drop extension tsm_system_rows because other objects depend on it +DETAIL: view vv depends on function system_rows(internal) +HINT: Use DROP ... CASCADE to drop the dependent objects too. diff --git a/contrib/tsm_system_rows/sql/tsm_system_rows.sql b/contrib/tsm_system_rows/sql/tsm_system_rows.sql index bd812220ed9..e3ab4204eea 100644 --- a/contrib/tsm_system_rows/sql/tsm_system_rows.sql +++ b/contrib/tsm_system_rows/sql/tsm_system_rows.sql @@ -1,14 +1,39 @@ CREATE EXTENSION tsm_system_rows; -CREATE TABLE test_tablesample (id int, name text) WITH (fillfactor=10); -- force smaller pages so we don't have to load too much data to get multiple pages - -INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) FROM generate_series(0, 30) s(i) ORDER BY i; +CREATE TABLE test_tablesample (id int, name text); +INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) + FROM generate_series(0, 30) s(i); ANALYZE test_tablesample; -SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (1000); -SELECT id FROM test_tablesample TABLESAMPLE system_rows (8) REPEATABLE (5432); +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (0); +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (1); +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (10); +SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (100); + +-- bad parameters should get through planning, but not execution: +EXPLAIN (COSTS OFF) +SELECT id FROM test_tablesample TABLESAMPLE system_rows (-1); + +SELECT id FROM test_tablesample TABLESAMPLE system_rows (-1); + +-- fail, this method is not repeatable: +SELECT * FROM test_tablesample TABLESAMPLE system_rows (10) REPEATABLE (0); + +-- but a join should be allowed: +EXPLAIN (COSTS OFF) +SELECT * FROM + (VALUES (0),(10),(100)) v(nrows), + LATERAL (SELECT count(*) FROM test_tablesample + TABLESAMPLE system_rows (nrows)) ss; + +SELECT * FROM + (VALUES (0),(10),(100)) v(nrows), + LATERAL (SELECT count(*) FROM test_tablesample + TABLESAMPLE system_rows (nrows)) ss; + +CREATE VIEW vv AS + SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (20); -EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_rows (20) REPEATABLE (10); +SELECT * FROM vv; --- done -DROP TABLE test_tablesample CASCADE; +DROP EXTENSION tsm_system_rows; -- fail, view depends on extension diff --git a/contrib/tsm_system_rows/tsm_system_rows--1.0.sql b/contrib/tsm_system_rows/tsm_system_rows--1.0.sql index 1a29c584b5a..de508ed7267 100644 --- a/contrib/tsm_system_rows/tsm_system_rows--1.0.sql +++ b/contrib/tsm_system_rows/tsm_system_rows--1.0.sql @@ -1,44 +1,9 @@ -/* src/test/modules/tablesample/tsm_system_rows--1.0.sql */ +/* contrib/tsm_system_rows/tsm_system_rows--1.0.sql */ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION tsm_system_rows" to load this file. \quit -CREATE FUNCTION tsm_system_rows_init(internal, int4, int4) -RETURNS void -AS 'MODULE_PATHNAME' +CREATE FUNCTION system_rows(internal) +RETURNS tsm_handler +AS 'MODULE_PATHNAME', 'tsm_system_rows_handler' LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_rows_nextblock(internal) -RETURNS int4 -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_rows_nexttuple(internal, int4, int2) -RETURNS int2 -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_rows_examinetuple(internal, int4, internal, bool) -RETURNS bool -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_rows_end(internal) -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_rows_reset(internal) -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_rows_cost(internal, internal, internal, internal, internal, internal, internal) -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -INSERT INTO pg_tablesample_method VALUES('system_rows', false, true, - 'tsm_system_rows_init', 'tsm_system_rows_nextblock', - 'tsm_system_rows_nexttuple', 'tsm_system_rows_examinetuple', - 'tsm_system_rows_end', 'tsm_system_rows_reset', 'tsm_system_rows_cost'); diff --git a/contrib/tsm_system_rows/tsm_system_rows.c b/contrib/tsm_system_rows/tsm_system_rows.c index e325eaff498..f251e3e5e06 100644 --- a/contrib/tsm_system_rows/tsm_system_rows.c +++ b/contrib/tsm_system_rows/tsm_system_rows.c @@ -1,240 +1,356 @@ /*------------------------------------------------------------------------- * * tsm_system_rows.c - * interface routines for system_rows tablesample method + * support routines for SYSTEM_ROWS tablesample method * + * The desire here is to produce a random sample with a given number of rows + * (or the whole relation, if that is fewer rows). We use a block-sampling + * approach. To ensure that the whole relation will be visited if necessary, + * we start at a randomly chosen block and then advance with a stride that + * is randomly chosen but is relatively prime to the relation's nblocks. * - * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group + * Because of the dependence on nblocks, this method cannot be repeatable + * across queries. (Even if the user hasn't explicitly changed the relation, + * maintenance activities such as autovacuum might change nblocks.) However, + * we can at least make it repeatable across scans, by determining the + * sampling pattern only once on the first scan. This means that rescans + * won't visit blocks added after the first scan, but that is fine since + * such blocks shouldn't contain any visible tuples anyway. + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * contrib/tsm_system_rows_rowlimit/tsm_system_rows.c + * contrib/tsm_system_rows/tsm_system_rows.c * *------------------------------------------------------------------------- */ #include "postgres.h" -#include "fmgr.h" - -#include "access/tablesample.h" #include "access/relscan.h" +#include "access/tsmapi.h" +#include "catalog/pg_type.h" #include "miscadmin.h" -#include "nodes/execnodes.h" -#include "nodes/relation.h" #include "optimizer/clauses.h" -#include "storage/bufmgr.h" +#include "optimizer/cost.h" #include "utils/sampling.h" PG_MODULE_MAGIC; -/* - * State - */ +PG_FUNCTION_INFO_V1(tsm_system_rows_handler); + + +/* Private state */ typedef struct { - SamplerRandomState randstate; uint32 seed; /* random seed */ - BlockNumber nblocks; /* number of block in relation */ - int32 ntuples; /* number of tuples to return */ - int32 donetuples; /* tuples already returned */ + int64 ntuples; /* number of tuples to return */ + int64 donetuples; /* number of tuples already returned */ OffsetNumber lt; /* last tuple returned from current block */ - BlockNumber step; /* step size */ + BlockNumber doneblocks; /* number of already-scanned blocks */ BlockNumber lb; /* last block visited */ - BlockNumber doneblocks; /* number of already returned blocks */ -} SystemSamplerData; - - -PG_FUNCTION_INFO_V1(tsm_system_rows_init); -PG_FUNCTION_INFO_V1(tsm_system_rows_nextblock); -PG_FUNCTION_INFO_V1(tsm_system_rows_nexttuple); -PG_FUNCTION_INFO_V1(tsm_system_rows_examinetuple); -PG_FUNCTION_INFO_V1(tsm_system_rows_end); -PG_FUNCTION_INFO_V1(tsm_system_rows_reset); -PG_FUNCTION_INFO_V1(tsm_system_rows_cost); - + /* these three values are not changed during a rescan: */ + BlockNumber nblocks; /* number of blocks in relation */ + BlockNumber firstblock; /* first block to sample from */ + BlockNumber step; /* step size, or 0 if not set yet */ +} SystemRowsSamplerData; + +static void system_rows_samplescangetsamplesize(PlannerInfo *root, + RelOptInfo *baserel, + List *paramexprs, + BlockNumber *pages, + double *tuples); +static void system_rows_initsamplescan(SampleScanState *node, + int eflags); +static void system_rows_beginsamplescan(SampleScanState *node, + Datum *params, + int nparams, + uint32 seed); +static BlockNumber system_rows_nextsampleblock(SampleScanState *node); +static OffsetNumber system_rows_nextsampletuple(SampleScanState *node, + BlockNumber blockno, + OffsetNumber maxoffset); +static bool SampleOffsetVisible(OffsetNumber tupoffset, HeapScanDesc scan); static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate); + /* - * Initializes the state. + * Create a TsmRoutine descriptor for the SYSTEM_ROWS method. */ Datum -tsm_system_rows_init(PG_FUNCTION_ARGS) +tsm_system_rows_handler(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - uint32 seed = PG_GETARG_UINT32(1); - int32 ntuples = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2); - HeapScanDesc scan = tsdesc->heapScan; - SystemSamplerData *sampler; + TsmRoutine *tsm = makeNode(TsmRoutine); - if (ntuples < 1) - ereport(ERROR, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("invalid sample size"), - errhint("Sample size must be positive integer value."))); + tsm->parameterTypes = list_make1_oid(INT8OID); - sampler = palloc0(sizeof(SystemSamplerData)); + /* See notes at head of file */ + tsm->repeatable_across_queries = false; + tsm->repeatable_across_scans = true; - /* Remember initial values for reinit */ - sampler->seed = seed; - sampler->nblocks = scan->rs_nblocks; - sampler->ntuples = ntuples; - sampler->donetuples = 0; - sampler->lt = InvalidOffsetNumber; - sampler->doneblocks = 0; - - sampler_random_init_state(sampler->seed, sampler->randstate); - - /* Find relative prime as step size for linear probing. */ - sampler->step = random_relative_prime(sampler->nblocks, sampler->randstate); - - /* - * Randomize start position so that blocks close to step size don't have - * higher probability of being chosen on very short scan. - */ - sampler->lb = sampler_random_fract(sampler->randstate) * - (sampler->nblocks / sampler->step); + tsm->SampleScanGetSampleSize = system_rows_samplescangetsamplesize; + tsm->InitSampleScan = system_rows_initsamplescan; + tsm->BeginSampleScan = system_rows_beginsamplescan; + tsm->NextSampleBlock = system_rows_nextsampleblock; + tsm->NextSampleTuple = system_rows_nextsampletuple; + tsm->EndSampleScan = NULL; - tsdesc->tsmdata = (void *) sampler; - - PG_RETURN_VOID(); + PG_RETURN_POINTER(tsm); } /* - * Get next block number or InvalidBlockNumber when we're done. - * - * Uses linear probing algorithm for picking next block. + * Sample size estimation. */ -Datum -tsm_system_rows_nextblock(PG_FUNCTION_ARGS) +static void +system_rows_samplescangetsamplesize(PlannerInfo *root, + RelOptInfo *baserel, + List *paramexprs, + BlockNumber *pages, + double *tuples) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; + Node *limitnode; + int64 ntuples; + double npages; - sampler->lb = (sampler->lb + sampler->step) % sampler->nblocks; - sampler->doneblocks++; + /* Try to extract an estimate for the limit rowcount */ + limitnode = (Node *) linitial(paramexprs); + limitnode = estimate_expression_value(root, limitnode); - /* All blocks have been read, we're done */ - if (sampler->doneblocks > sampler->nblocks || - sampler->donetuples >= sampler->ntuples) - PG_RETURN_UINT32(InvalidBlockNumber); + if (IsA(limitnode, Const) && + !((Const *) limitnode)->constisnull) + { + ntuples = DatumGetInt64(((Const *) limitnode)->constvalue); + if (ntuples < 0) + { + /* Default ntuples if the value is bogus */ + ntuples = 1000; + } + } + else + { + /* Default ntuples if we didn't obtain a non-null Const */ + ntuples = 1000; + } - PG_RETURN_UINT32(sampler->lb); -} + /* Clamp to the estimated relation size */ + if (ntuples > baserel->tuples) + ntuples = (int64) baserel->tuples; + ntuples = clamp_row_est(ntuples); -/* - * Get next tuple offset in current block or InvalidOffsetNumber if we are done - * with this block. - */ -Datum -tsm_system_rows_nexttuple(PG_FUNCTION_ARGS) -{ - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - OffsetNumber maxoffset = PG_GETARG_UINT16(2); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; - OffsetNumber tupoffset = sampler->lt; + if (baserel->tuples > 0 && baserel->pages > 0) + { + /* Estimate number of pages visited based on tuple density */ + double density = baserel->tuples / (double) baserel->pages; - if (tupoffset == InvalidOffsetNumber) - tupoffset = FirstOffsetNumber; + npages = ntuples / density; + } else - tupoffset++; - - if (tupoffset > maxoffset || - sampler->donetuples >= sampler->ntuples) - tupoffset = InvalidOffsetNumber; + { + /* For lack of data, assume one tuple per page */ + npages = ntuples; + } - sampler->lt = tupoffset; + /* Clamp to sane value */ + npages = clamp_row_est(Min((double) baserel->pages, npages)); - PG_RETURN_UINT16(tupoffset); + *pages = npages; + *tuples = ntuples; } /* - * Examine tuple and decide if it should be returned. + * Initialize during executor setup. */ -Datum -tsm_system_rows_examinetuple(PG_FUNCTION_ARGS) +static void +system_rows_initsamplescan(SampleScanState *node, int eflags) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - bool visible = PG_GETARG_BOOL(3); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; - - if (!visible) - PG_RETURN_BOOL(false); - - sampler->donetuples++; - - PG_RETURN_BOOL(true); + node->tsm_state = palloc0(sizeof(SystemRowsSamplerData)); + /* Note the above leaves tsm_state->step equal to zero */ } /* - * Cleanup method. + * Examine parameters and prepare for a sample scan. */ -Datum -tsm_system_rows_end(PG_FUNCTION_ARGS) +static void +system_rows_beginsamplescan(SampleScanState *node, + Datum *params, + int nparams, + uint32 seed) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + SystemRowsSamplerData *sampler = (SystemRowsSamplerData *) node->tsm_state; + int64 ntuples = DatumGetInt64(params[0]); + + if (ntuples < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT), + errmsg("sample size must not be negative"))); - pfree(tsdesc->tsmdata); + sampler->seed = seed; + sampler->ntuples = ntuples; + sampler->donetuples = 0; + sampler->lt = InvalidOffsetNumber; + sampler->doneblocks = 0; + /* lb will be initialized during first NextSampleBlock call */ + /* we intentionally do not change nblocks/firstblock/step here */ - PG_RETURN_VOID(); + /* + * We *must* use pagemode visibility checking in this module, so force + * that even though it's currently default. + */ + node->use_pagemode = true; } /* - * Reset state (called by ReScan). + * Select next block to sample. + * + * Uses linear probing algorithm for picking next block. */ -Datum -tsm_system_rows_reset(PG_FUNCTION_ARGS) +static BlockNumber +system_rows_nextsampleblock(SampleScanState *node) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; + SystemRowsSamplerData *sampler = (SystemRowsSamplerData *) node->tsm_state; + HeapScanDesc scan = node->ss.ss_currentScanDesc; - sampler->lt = InvalidOffsetNumber; - sampler->donetuples = 0; - sampler->doneblocks = 0; + /* First call within scan? */ + if (sampler->doneblocks == 0) + { + /* First scan within query? */ + if (sampler->step == 0) + { + /* Initialize now that we have scan descriptor */ + SamplerRandomState randstate; + + /* If relation is empty, there's nothing to scan */ + if (scan->rs_nblocks == 0) + return InvalidBlockNumber; + + /* We only need an RNG during this setup step */ + sampler_random_init_state(sampler->seed, randstate); + + /* Compute nblocks/firstblock/step only once per query */ + sampler->nblocks = scan->rs_nblocks; - sampler_random_init_state(sampler->seed, sampler->randstate); - sampler->step = random_relative_prime(sampler->nblocks, sampler->randstate); - sampler->lb = sampler_random_fract(sampler->randstate) * (sampler->nblocks / sampler->step); + /* Choose random starting block within the relation */ + /* (Actually this is the predecessor of the first block visited) */ + sampler->firstblock = sampler_random_fract(randstate) * + sampler->nblocks; + + /* Find relative prime as step size for linear probing */ + sampler->step = random_relative_prime(sampler->nblocks, randstate); + } + + /* Reinitialize lb */ + sampler->lb = sampler->firstblock; + } + + /* If we've read all blocks or returned all needed tuples, we're done */ + if (++sampler->doneblocks > sampler->nblocks || + sampler->donetuples >= sampler->ntuples) + return InvalidBlockNumber; + + /* + * It's probably impossible for scan->rs_nblocks to decrease between scans + * within a query; but just in case, loop until we select a block number + * less than scan->rs_nblocks. We don't care if scan->rs_nblocks has + * increased since the first scan. + */ + do + { + /* Advance lb, using uint64 arithmetic to forestall overflow */ + sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks; + } while (sampler->lb >= scan->rs_nblocks); - PG_RETURN_VOID(); + return sampler->lb; } /* - * Costing function. + * Select next sampled tuple in current block. + * + * In block sampling, we just want to sample all the tuples in each selected + * block. + * + * When we reach end of the block, return InvalidOffsetNumber which tells + * SampleScan to go to next block. */ -Datum -tsm_system_rows_cost(PG_FUNCTION_ARGS) +static OffsetNumber +system_rows_nextsampletuple(SampleScanState *node, + BlockNumber blockno, + OffsetNumber maxoffset) { - PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); - Path *path = (Path *) PG_GETARG_POINTER(1); - RelOptInfo *baserel = (RelOptInfo *) PG_GETARG_POINTER(2); - List *args = (List *) PG_GETARG_POINTER(3); - BlockNumber *pages = (BlockNumber *) PG_GETARG_POINTER(4); - double *tuples = (double *) PG_GETARG_POINTER(5); - Node *limitnode; - int32 ntuples; + SystemRowsSamplerData *sampler = (SystemRowsSamplerData *) node->tsm_state; + HeapScanDesc scan = node->ss.ss_currentScanDesc; + OffsetNumber tupoffset = sampler->lt; - limitnode = linitial(args); - limitnode = estimate_expression_value(root, limitnode); + /* Quit if we've returned all needed tuples */ + if (sampler->donetuples >= sampler->ntuples) + return InvalidOffsetNumber; - if (IsA(limitnode, RelabelType)) - limitnode = (Node *) ((RelabelType *) limitnode)->arg; + /* + * Because we should only count visible tuples as being returned, we need + * to search for a visible tuple rather than just let the core code do it. + */ - if (IsA(limitnode, Const)) - ntuples = DatumGetInt32(((Const *) limitnode)->constvalue); - else + /* We rely on the data accumulated in pagemode access */ + Assert(scan->rs_pageatatime); + for (;;) { - /* Default ntuples if the estimation didn't return Const. */ - ntuples = 1000; + /* Advance to next possible offset on page */ + if (tupoffset == InvalidOffsetNumber) + tupoffset = FirstOffsetNumber; + else + tupoffset++; + + /* Done? */ + if (tupoffset > maxoffset) + { + tupoffset = InvalidOffsetNumber; + break; + } + + /* Found a candidate? */ + if (SampleOffsetVisible(tupoffset, scan)) + { + sampler->donetuples++; + break; + } } - *pages = Min(baserel->pages, ntuples); - *tuples = ntuples; - path->rows = *tuples; + sampler->lt = tupoffset; - PG_RETURN_VOID(); + return tupoffset; } +/* + * Check if tuple offset is visible + * + * In pageatatime mode, heapgetpage() already did visibility checks, + * so just look at the info it left in rs_vistuples[]. + */ +static bool +SampleOffsetVisible(OffsetNumber tupoffset, HeapScanDesc scan) +{ + int start = 0, + end = scan->rs_ntuples - 1; + + while (start <= end) + { + int mid = (start + end) / 2; + OffsetNumber curoffset = scan->rs_vistuples[mid]; + + if (tupoffset == curoffset) + return true; + else if (tupoffset < curoffset) + end = mid - 1; + else + start = mid + 1; + } + + return false; +} +/* + * Compute greatest common divisor of two uint32's. + */ static uint32 gcd(uint32 a, uint32 b) { @@ -250,22 +366,29 @@ gcd(uint32 a, uint32 b) return b; } +/* + * Pick a random value less than and relatively prime to n, if possible + * (else return 1). + */ static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate) { - /* Pick random starting number, with some limits on what it can be. */ - uint32 r = (uint32) sampler_random_fract(randstate) * n / 2 + n / 4, - t; + uint32 r; + + /* Safety check to avoid infinite loop or zero result for small n. */ + if (n <= 1) + return 1; /* * This should only take 2 or 3 iterations as the probability of 2 numbers - * being relatively prime is ~61%. + * being relatively prime is ~61%; but just in case, we'll include a + * CHECK_FOR_INTERRUPTS in the loop. */ - while ((t = gcd(r, n)) > 1) + do { CHECK_FOR_INTERRUPTS(); - r /= t; - } + r = (uint32) (sampler_random_fract(randstate) * n); + } while (r == 0 || gcd(r, n) > 1); return r; } diff --git a/contrib/tsm_system_rows/tsm_system_rows.control b/contrib/tsm_system_rows/tsm_system_rows.control index 84ea7adb49a..4bd0232f972 100644 --- a/contrib/tsm_system_rows/tsm_system_rows.control +++ b/contrib/tsm_system_rows/tsm_system_rows.control @@ -1,5 +1,5 @@ # tsm_system_rows extension -comment = 'SYSTEM TABLESAMPLE method which accepts number rows as a limit' +comment = 'TABLESAMPLE method which accepts number of rows as a limit' default_version = '1.0' module_pathname = '$libdir/tsm_system_rows' relocatable = true diff --git a/contrib/tsm_system_time/Makefile b/contrib/tsm_system_time/Makefile index c42c1c6bb61..168becf54e2 100644 --- a/contrib/tsm_system_time/Makefile +++ b/contrib/tsm_system_time/Makefile @@ -1,8 +1,8 @@ -# src/test/modules/tsm_system_time/Makefile +# contrib/tsm_system_time/Makefile MODULE_big = tsm_system_time OBJS = tsm_system_time.o $(WIN32RES) -PGFILEDESC = "tsm_system_time - SYSTEM TABLESAMPLE method which accepts number rows of as a limit" +PGFILEDESC = "tsm_system_time - TABLESAMPLE method which accepts time in milliseconds as a limit" EXTENSION = tsm_system_time DATA = tsm_system_time--1.0.sql diff --git a/contrib/tsm_system_time/expected/tsm_system_time.out b/contrib/tsm_system_time/expected/tsm_system_time.out index 32ad03c4bdc..ac44f30be90 100644 --- a/contrib/tsm_system_time/expected/tsm_system_time.out +++ b/contrib/tsm_system_time/expected/tsm_system_time.out @@ -1,54 +1,100 @@ CREATE EXTENSION tsm_system_time; -CREATE TABLE test_tablesample (id int, name text) WITH (fillfactor=10); -- force smaller pages so we don't have to load too much data to get multiple pages -INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) FROM generate_series(0, 30) s(i) ORDER BY i; +CREATE TABLE test_tablesample (id int, name text); +INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) + FROM generate_series(0, 30) s(i); ANALYZE test_tablesample; -SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (1000); +-- It's a bit tricky to test SYSTEM_TIME in a platform-independent way. +-- We can test the zero-time corner case ... +SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (0); count ------- - 31 + 0 (1 row) -SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432); - id ----- - 7 - 14 - 21 - 28 - 4 - 11 - 18 - 25 - 1 - 8 - 15 - 22 - 29 - 5 - 12 - 19 - 26 - 2 - 9 - 16 - 23 - 30 - 6 - 13 - 20 - 27 - 3 - 10 - 17 - 24 - 0 -(31 rows) - -EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_time (100) REPEATABLE (10); - QUERY PLAN ------------------------------------------------------------------------------------- - Sample Scan (system_time) on test_tablesample (cost=0.00..100.25 rows=25 width=4) +-- ... and we assume that this will finish before running out of time: +SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (100000); + count +------- + 31 (1 row) --- done -DROP TABLE test_tablesample CASCADE; +-- bad parameters should get through planning, but not execution: +EXPLAIN (COSTS OFF) +SELECT id FROM test_tablesample TABLESAMPLE system_time (-1); + QUERY PLAN +-------------------------------------------------- + Sample Scan on test_tablesample + Sampling: system_time ('-1'::double precision) +(2 rows) + +SELECT id FROM test_tablesample TABLESAMPLE system_time (-1); +ERROR: sample collection time must not be negative +-- fail, this method is not repeatable: +SELECT * FROM test_tablesample TABLESAMPLE system_time (10) REPEATABLE (0); +ERROR: tablesample method system_time does not support REPEATABLE +LINE 1: SELECT * FROM test_tablesample TABLESAMPLE system_time (10) ... + ^ +-- since it's not repeatable, we expect a Materialize node in these plans: +EXPLAIN (COSTS OFF) +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (100000)) ss; + QUERY PLAN +------------------------------------------------------------------------ + Nested Loop + -> Aggregate + -> Materialize + -> Sample Scan on test_tablesample + Sampling: system_time ('100000'::double precision) + -> Values Scan on "*VALUES*" +(6 rows) + +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (100000)) ss; + time | count +--------+------- + 0 | 31 + 100000 | 31 +(2 rows) + +EXPLAIN (COSTS OFF) +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (time)) ss; + QUERY PLAN +---------------------------------------------------------------- + Nested Loop + -> Values Scan on "*VALUES*" + -> Aggregate + -> Materialize + -> Sample Scan on test_tablesample + Sampling: system_time ("*VALUES*".column1) +(6 rows) + +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (time)) ss; + time | count +--------+------- + 0 | 0 + 100000 | 31 +(2 rows) + +CREATE VIEW vv AS + SELECT * FROM test_tablesample TABLESAMPLE system_time (20); +EXPLAIN (COSTS OFF) SELECT * FROM vv; + QUERY PLAN +-------------------------------------------------- + Sample Scan on test_tablesample + Sampling: system_time ('20'::double precision) +(2 rows) + +DROP EXTENSION tsm_system_time; -- fail, view depends on extension +ERROR: cannot drop extension tsm_system_time because other objects depend on it +DETAIL: view vv depends on function system_time(internal) +HINT: Use DROP ... CASCADE to drop the dependent objects too. diff --git a/contrib/tsm_system_time/sql/tsm_system_time.sql b/contrib/tsm_system_time/sql/tsm_system_time.sql index 68dbbf98afd..117de163d85 100644 --- a/contrib/tsm_system_time/sql/tsm_system_time.sql +++ b/contrib/tsm_system_time/sql/tsm_system_time.sql @@ -1,14 +1,51 @@ CREATE EXTENSION tsm_system_time; -CREATE TABLE test_tablesample (id int, name text) WITH (fillfactor=10); -- force smaller pages so we don't have to load too much data to get multiple pages - -INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) FROM generate_series(0, 30) s(i) ORDER BY i; +CREATE TABLE test_tablesample (id int, name text); +INSERT INTO test_tablesample SELECT i, repeat(i::text, 1000) + FROM generate_series(0, 30) s(i); ANALYZE test_tablesample; -SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (1000); -SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432); +-- It's a bit tricky to test SYSTEM_TIME in a platform-independent way. +-- We can test the zero-time corner case ... +SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (0); +-- ... and we assume that this will finish before running out of time: +SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (100000); + +-- bad parameters should get through planning, but not execution: +EXPLAIN (COSTS OFF) +SELECT id FROM test_tablesample TABLESAMPLE system_time (-1); + +SELECT id FROM test_tablesample TABLESAMPLE system_time (-1); + +-- fail, this method is not repeatable: +SELECT * FROM test_tablesample TABLESAMPLE system_time (10) REPEATABLE (0); + +-- since it's not repeatable, we expect a Materialize node in these plans: +EXPLAIN (COSTS OFF) +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (100000)) ss; + +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (100000)) ss; + +EXPLAIN (COSTS OFF) +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (time)) ss; + +SELECT * FROM + (VALUES (0),(100000)) v(time), + LATERAL (SELECT COUNT(*) FROM test_tablesample + TABLESAMPLE system_time (time)) ss; + +CREATE VIEW vv AS + SELECT * FROM test_tablesample TABLESAMPLE system_time (20); -EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_time (100) REPEATABLE (10); +EXPLAIN (COSTS OFF) SELECT * FROM vv; --- done -DROP TABLE test_tablesample CASCADE; +DROP EXTENSION tsm_system_time; -- fail, view depends on extension diff --git a/contrib/tsm_system_time/tsm_system_time--1.0.sql b/contrib/tsm_system_time/tsm_system_time--1.0.sql index 1f390d6ed7a..c59d2e84efd 100644 --- a/contrib/tsm_system_time/tsm_system_time--1.0.sql +++ b/contrib/tsm_system_time/tsm_system_time--1.0.sql @@ -1,39 +1,9 @@ -/* src/test/modules/tablesample/tsm_system_time--1.0.sql */ +/* contrib/tsm_system_time/tsm_system_time--1.0.sql */ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION tsm_system_time" to load this file. \quit -CREATE FUNCTION tsm_system_time_init(internal, int4, int4) -RETURNS void -AS 'MODULE_PATHNAME' +CREATE FUNCTION system_time(internal) +RETURNS tsm_handler +AS 'MODULE_PATHNAME', 'tsm_system_time_handler' LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_time_nextblock(internal) -RETURNS int4 -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_time_nexttuple(internal, int4, int2) -RETURNS int2 -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_time_end(internal) -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_time_reset(internal) -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -CREATE FUNCTION tsm_system_time_cost(internal, internal, internal, internal, internal, internal, internal) -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C STRICT; - -INSERT INTO pg_tablesample_method VALUES('system_time', false, true, - 'tsm_system_time_init', 'tsm_system_time_nextblock', - 'tsm_system_time_nexttuple', '-', 'tsm_system_time_end', - 'tsm_system_time_reset', 'tsm_system_time_cost'); diff --git a/contrib/tsm_system_time/tsm_system_time.c b/contrib/tsm_system_time/tsm_system_time.c index 7708fc07617..83f1455c5fa 100644 --- a/contrib/tsm_system_time/tsm_system_time.c +++ b/contrib/tsm_system_time/tsm_system_time.c @@ -1,286 +1,320 @@ /*------------------------------------------------------------------------- * * tsm_system_time.c - * interface routines for system_time tablesample method + * support routines for SYSTEM_TIME tablesample method * + * The desire here is to produce a random sample with as many rows as possible + * in no more than the specified amount of time. We use a block-sampling + * approach. To ensure that the whole relation will be visited if necessary, + * we start at a randomly chosen block and then advance with a stride that + * is randomly chosen but is relatively prime to the relation's nblocks. * - * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group + * Because of the time dependence, this method is necessarily unrepeatable. + * However, we do what we can to reduce surprising behavior by selecting + * the sampling pattern just once per query, much as in tsm_system_rows. + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * contrib/tsm_system_time_rowlimit/tsm_system_time.c + * contrib/tsm_system_time/tsm_system_time.c * *------------------------------------------------------------------------- */ #include "postgres.h" -#include "fmgr.h" +#ifdef _MSC_VER +#include <float.h> /* for _isnan */ +#endif +#include <math.h> -#include "access/tablesample.h" #include "access/relscan.h" +#include "access/tsmapi.h" +#include "catalog/pg_type.h" #include "miscadmin.h" -#include "nodes/execnodes.h" -#include "nodes/relation.h" #include "optimizer/clauses.h" -#include "storage/bufmgr.h" +#include "optimizer/cost.h" #include "utils/sampling.h" #include "utils/spccache.h" -#include "utils/timestamp.h" PG_MODULE_MAGIC; -/* - * State - */ +PG_FUNCTION_INFO_V1(tsm_system_time_handler); + + +/* Private state */ typedef struct { - SamplerRandomState randstate; uint32 seed; /* random seed */ - BlockNumber nblocks; /* number of block in relation */ - int32 time; /* time limit for sampling */ - TimestampTz start_time; /* start time of sampling */ - TimestampTz end_time; /* end time of sampling */ + double millis; /* time limit for sampling */ + instr_time start_time; /* scan start time */ OffsetNumber lt; /* last tuple returned from current block */ - BlockNumber step; /* step size */ + BlockNumber doneblocks; /* number of already-scanned blocks */ BlockNumber lb; /* last block visited */ - BlockNumber estblocks; /* estimated number of returned blocks - * (moving) */ - BlockNumber doneblocks; /* number of already returned blocks */ -} SystemSamplerData; - - -PG_FUNCTION_INFO_V1(tsm_system_time_init); -PG_FUNCTION_INFO_V1(tsm_system_time_nextblock); -PG_FUNCTION_INFO_V1(tsm_system_time_nexttuple); -PG_FUNCTION_INFO_V1(tsm_system_time_end); -PG_FUNCTION_INFO_V1(tsm_system_time_reset); -PG_FUNCTION_INFO_V1(tsm_system_time_cost); - + /* these three values are not changed during a rescan: */ + BlockNumber nblocks; /* number of blocks in relation */ + BlockNumber firstblock; /* first block to sample from */ + BlockNumber step; /* step size, or 0 if not set yet */ +} SystemTimeSamplerData; + +static void system_time_samplescangetsamplesize(PlannerInfo *root, + RelOptInfo *baserel, + List *paramexprs, + BlockNumber *pages, + double *tuples); +static void system_time_initsamplescan(SampleScanState *node, + int eflags); +static void system_time_beginsamplescan(SampleScanState *node, + Datum *params, + int nparams, + uint32 seed); +static BlockNumber system_time_nextsampleblock(SampleScanState *node); +static OffsetNumber system_time_nextsampletuple(SampleScanState *node, + BlockNumber blockno, + OffsetNumber maxoffset); static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate); + /* - * Initializes the state. + * Create a TsmRoutine descriptor for the SYSTEM_TIME method. */ Datum -tsm_system_time_init(PG_FUNCTION_ARGS) +tsm_system_time_handler(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - uint32 seed = PG_GETARG_UINT32(1); - int32 time = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2); - HeapScanDesc scan = tsdesc->heapScan; - SystemSamplerData *sampler; - - if (time < 1) - ereport(ERROR, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("invalid time limit"), - errhint("Time limit must be positive integer value."))); + TsmRoutine *tsm = makeNode(TsmRoutine); - sampler = palloc0(sizeof(SystemSamplerData)); + tsm->parameterTypes = list_make1_oid(FLOAT8OID); - /* Remember initial values for reinit */ - sampler->seed = seed; - sampler->nblocks = scan->rs_nblocks; - sampler->lt = InvalidOffsetNumber; - sampler->estblocks = 2; - sampler->doneblocks = 0; - sampler->time = time; - sampler->start_time = GetCurrentTimestamp(); - sampler->end_time = TimestampTzPlusMilliseconds(sampler->start_time, - sampler->time); + /* See notes at head of file */ + tsm->repeatable_across_queries = false; + tsm->repeatable_across_scans = false; - sampler_random_init_state(sampler->seed, sampler->randstate); + tsm->SampleScanGetSampleSize = system_time_samplescangetsamplesize; + tsm->InitSampleScan = system_time_initsamplescan; + tsm->BeginSampleScan = system_time_beginsamplescan; + tsm->NextSampleBlock = system_time_nextsampleblock; + tsm->NextSampleTuple = system_time_nextsampletuple; + tsm->EndSampleScan = NULL; - /* Find relative prime as step size for linear probing. */ - sampler->step = random_relative_prime(sampler->nblocks, sampler->randstate); - - /* - * Randomize start position so that blocks close to step size don't have - * higher probability of being chosen on very short scan. - */ - sampler->lb = sampler_random_fract(sampler->randstate) * (sampler->nblocks / sampler->step); - - tsdesc->tsmdata = (void *) sampler; - - PG_RETURN_VOID(); + PG_RETURN_POINTER(tsm); } /* - * Get next block number or InvalidBlockNumber when we're done. - * - * Uses linear probing algorithm for picking next block. + * Sample size estimation. */ -Datum -tsm_system_time_nextblock(PG_FUNCTION_ARGS) +static void +system_time_samplescangetsamplesize(PlannerInfo *root, + RelOptInfo *baserel, + List *paramexprs, + BlockNumber *pages, + double *tuples) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; - - sampler->lb = (sampler->lb + sampler->step) % sampler->nblocks; - sampler->doneblocks++; + Node *limitnode; + double millis; + double spc_random_page_cost; + double npages; + double ntuples; - /* All blocks have been read, we're done */ - if (sampler->doneblocks > sampler->nblocks) - PG_RETURN_UINT32(InvalidBlockNumber); + /* Try to extract an estimate for the limit time spec */ + limitnode = (Node *) linitial(paramexprs); + limitnode = estimate_expression_value(root, limitnode); - /* - * Update the estimations for time limit at least 10 times per estimated - * number of returned blocks to handle variations in block read speed. - */ - if (sampler->doneblocks % Max(sampler->estblocks / 10, 1) == 0) + if (IsA(limitnode, Const) && + !((Const *) limitnode)->constisnull) + { + millis = DatumGetFloat8(((Const *) limitnode)->constvalue); + if (millis < 0 || isnan(millis)) + { + /* Default millis if the value is bogus */ + millis = 1000; + } + } + else { - TimestampTz now = GetCurrentTimestamp(); - long secs; - int usecs; - int usecs_remaining; - int time_per_block; + /* Default millis if we didn't obtain a non-null Const */ + millis = 1000; + } - TimestampDifference(sampler->start_time, now, &secs, &usecs); - usecs += (int) secs *1000000; + /* Get the planner's idea of cost per page read */ + get_tablespace_page_costs(baserel->reltablespace, + &spc_random_page_cost, + NULL); - time_per_block = usecs / sampler->doneblocks; + /* + * Estimate the number of pages we can read by assuming that the cost + * figure is expressed in milliseconds. This is completely, unmistakably + * bogus, but we have to do something to produce an estimate and there's + * no better answer. + */ + if (spc_random_page_cost > 0) + npages = millis / spc_random_page_cost; + else + npages = millis; /* even more bogus, but whatcha gonna do? */ - /* No time left, end. */ - TimestampDifference(now, sampler->end_time, &secs, &usecs); - if (secs <= 0 && usecs <= 0) - PG_RETURN_UINT32(InvalidBlockNumber); + /* Clamp to sane value */ + npages = clamp_row_est(Min((double) baserel->pages, npages)); - /* Remaining microseconds */ - usecs_remaining = usecs + (int) secs *1000000; + if (baserel->tuples > 0 && baserel->pages > 0) + { + /* Estimate number of tuples returned based on tuple density */ + double density = baserel->tuples / (double) baserel->pages; - /* Recalculate estimated returned number of blocks */ - if (time_per_block < usecs_remaining && time_per_block > 0) - sampler->estblocks = sampler->time * time_per_block; + ntuples = npages * density; } - - PG_RETURN_UINT32(sampler->lb); -} - -/* - * Get next tuple offset in current block or InvalidOffsetNumber if we are done - * with this block. - */ -Datum -tsm_system_time_nexttuple(PG_FUNCTION_ARGS) -{ - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - OffsetNumber maxoffset = PG_GETARG_UINT16(2); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; - OffsetNumber tupoffset = sampler->lt; - - if (tupoffset == InvalidOffsetNumber) - tupoffset = FirstOffsetNumber; else - tupoffset++; - - if (tupoffset > maxoffset) - tupoffset = InvalidOffsetNumber; + { + /* For lack of data, assume one tuple per page */ + ntuples = npages; + } - sampler->lt = tupoffset; + /* Clamp to the estimated relation size */ + ntuples = clamp_row_est(Min(baserel->tuples, ntuples)); - PG_RETURN_UINT16(tupoffset); + *pages = npages; + *tuples = ntuples; } /* - * Cleanup method. + * Initialize during executor setup. */ -Datum -tsm_system_time_end(PG_FUNCTION_ARGS) +static void +system_time_initsamplescan(SampleScanState *node, int eflags) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - - pfree(tsdesc->tsmdata); - - PG_RETURN_VOID(); + node->tsm_state = palloc0(sizeof(SystemTimeSamplerData)); + /* Note the above leaves tsm_state->step equal to zero */ } /* - * Reset state (called by ReScan). + * Examine parameters and prepare for a sample scan. */ -Datum -tsm_system_time_reset(PG_FUNCTION_ARGS) +static void +system_time_beginsamplescan(SampleScanState *node, + Datum *params, + int nparams, + uint32 seed) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; + SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state; + double millis = DatumGetFloat8(params[0]); + + if (millis < 0 || isnan(millis)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT), + errmsg("sample collection time must not be negative"))); + sampler->seed = seed; + sampler->millis = millis; sampler->lt = InvalidOffsetNumber; - sampler->start_time = GetCurrentTimestamp(); - sampler->end_time = TimestampTzPlusMilliseconds(sampler->start_time, - sampler->time); - sampler->estblocks = 2; sampler->doneblocks = 0; - - sampler_random_init_state(sampler->seed, sampler->randstate); - sampler->step = random_relative_prime(sampler->nblocks, sampler->randstate); - sampler->lb = sampler_random_fract(sampler->randstate) * (sampler->nblocks / sampler->step); - - PG_RETURN_VOID(); + /* start_time, lb will be initialized during first NextSampleBlock call */ + /* we intentionally do not change nblocks/firstblock/step here */ } /* - * Costing function. + * Select next block to sample. + * + * Uses linear probing algorithm for picking next block. */ -Datum -tsm_system_time_cost(PG_FUNCTION_ARGS) +static BlockNumber +system_time_nextsampleblock(SampleScanState *node) { - PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); - Path *path = (Path *) PG_GETARG_POINTER(1); - RelOptInfo *baserel = (RelOptInfo *) PG_GETARG_POINTER(2); - List *args = (List *) PG_GETARG_POINTER(3); - BlockNumber *pages = (BlockNumber *) PG_GETARG_POINTER(4); - double *tuples = (double *) PG_GETARG_POINTER(5); - Node *limitnode; - int32 time; - BlockNumber relpages; - double reltuples; - double density; - double spc_random_page_cost; - - limitnode = linitial(args); - limitnode = estimate_expression_value(root, limitnode); - - if (IsA(limitnode, RelabelType)) - limitnode = (Node *) ((RelabelType *) limitnode)->arg; + SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state; + HeapScanDesc scan = node->ss.ss_currentScanDesc; + instr_time cur_time; - if (IsA(limitnode, Const)) - time = DatumGetInt32(((Const *) limitnode)->constvalue); - else + /* First call within scan? */ + if (sampler->doneblocks == 0) { - /* Default time (1s) if the estimation didn't return Const. */ - time = 1000; + /* First scan within query? */ + if (sampler->step == 0) + { + /* Initialize now that we have scan descriptor */ + SamplerRandomState randstate; + + /* If relation is empty, there's nothing to scan */ + if (scan->rs_nblocks == 0) + return InvalidBlockNumber; + + /* We only need an RNG during this setup step */ + sampler_random_init_state(sampler->seed, randstate); + + /* Compute nblocks/firstblock/step only once per query */ + sampler->nblocks = scan->rs_nblocks; + + /* Choose random starting block within the relation */ + /* (Actually this is the predecessor of the first block visited) */ + sampler->firstblock = sampler_random_fract(randstate) * + sampler->nblocks; + + /* Find relative prime as step size for linear probing */ + sampler->step = random_relative_prime(sampler->nblocks, randstate); + } + + /* Reinitialize lb and start_time */ + sampler->lb = sampler->firstblock; + INSTR_TIME_SET_CURRENT(sampler->start_time); } - relpages = baserel->pages; - reltuples = baserel->tuples; + /* If we've read all blocks in relation, we're done */ + if (++sampler->doneblocks > sampler->nblocks) + return InvalidBlockNumber; - /* estimate the tuple density */ - if (relpages > 0) - density = reltuples / (double) relpages; - else - density = (BLCKSZ - SizeOfPageHeaderData) / baserel->width; + /* If we've used up all the allotted time, we're done */ + INSTR_TIME_SET_CURRENT(cur_time); + INSTR_TIME_SUBTRACT(cur_time, sampler->start_time); + if (INSTR_TIME_GET_MILLISEC(cur_time) >= sampler->millis) + return InvalidBlockNumber; /* - * We equal random page cost value to number of ms it takes to read the - * random page here which is far from accurate but we don't have anything - * better to base our predicted page reads. + * It's probably impossible for scan->rs_nblocks to decrease between scans + * within a query; but just in case, loop until we select a block number + * less than scan->rs_nblocks. We don't care if scan->rs_nblocks has + * increased since the first scan. */ - get_tablespace_page_costs(baserel->reltablespace, - &spc_random_page_cost, - NULL); + do + { + /* Advance lb, using uint64 arithmetic to forestall overflow */ + sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks; + } while (sampler->lb >= scan->rs_nblocks); - /* - * Assumption here is that we'll never read less than 1% of table pages, - * this is here mainly because it is much less bad to overestimate than - * underestimate and using just spc_random_page_cost will probably lead to - * underestimations in general. - */ - *pages = Min(baserel->pages, Max(time / spc_random_page_cost, baserel->pages / 100)); - *tuples = rint(density * (double) *pages * path->rows / baserel->tuples); - path->rows = *tuples; + return sampler->lb; +} + +/* + * Select next sampled tuple in current block. + * + * In block sampling, we just want to sample all the tuples in each selected + * block. + * + * When we reach end of the block, return InvalidOffsetNumber which tells + * SampleScan to go to next block. + */ +static OffsetNumber +system_time_nextsampletuple(SampleScanState *node, + BlockNumber blockno, + OffsetNumber maxoffset) +{ + SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state; + OffsetNumber tupoffset = sampler->lt; + + /* Advance to next possible offset on page */ + if (tupoffset == InvalidOffsetNumber) + tupoffset = FirstOffsetNumber; + else + tupoffset++; + + /* Done? */ + if (tupoffset > maxoffset) + tupoffset = InvalidOffsetNumber; + + sampler->lt = tupoffset; - PG_RETURN_VOID(); + return tupoffset; } +/* + * Compute greatest common divisor of two uint32's. + */ static uint32 gcd(uint32 a, uint32 b) { @@ -296,22 +330,29 @@ gcd(uint32 a, uint32 b) return b; } +/* + * Pick a random value less than and relatively prime to n, if possible + * (else return 1). + */ static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate) { - /* Pick random starting number, with some limits on what it can be. */ - uint32 r = (uint32) sampler_random_fract(randstate) * n / 2 + n / 4, - t; + uint32 r; + + /* Safety check to avoid infinite loop or zero result for small n. */ + if (n <= 1) + return 1; /* * This should only take 2 or 3 iterations as the probability of 2 numbers - * being relatively prime is ~61%. + * being relatively prime is ~61%; but just in case, we'll include a + * CHECK_FOR_INTERRUPTS in the loop. */ - while ((t = gcd(r, n)) > 1) + do { CHECK_FOR_INTERRUPTS(); - r /= t; - } + r = (uint32) (sampler_random_fract(randstate) * n); + } while (r == 0 || gcd(r, n) > 1); return r; } diff --git a/contrib/tsm_system_time/tsm_system_time.control b/contrib/tsm_system_time/tsm_system_time.control index ebcee19d23a..c247987c66d 100644 --- a/contrib/tsm_system_time/tsm_system_time.control +++ b/contrib/tsm_system_time/tsm_system_time.control @@ -1,5 +1,5 @@ # tsm_system_time extension -comment = 'SYSTEM TABLESAMPLE method which accepts time in milliseconds as a limit' +comment = 'TABLESAMPLE method which accepts time in milliseconds as a limit' default_version = '1.0' module_pathname = '$libdir/tsm_system_time' relocatable = true |
