1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-06 07:49:08 +03:00

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.
This commit is contained in:
Tom Lane
2015-07-25 14:39:00 -04:00
parent b26e3d660d
commit dd7a8f66ed
83 changed files with 3184 additions and 2589 deletions

View File

@@ -337,26 +337,6 @@ typedef struct FuncCall
int location; /* token location, or -1 if unknown */
} FuncCall;
/*
* TableSampleClause - a sampling method information
*/
typedef struct TableSampleClause
{
NodeTag type;
Oid tsmid;
bool tsmseqscan;
bool tsmpagemode;
Oid tsminit;
Oid tsmnextblock;
Oid tsmnexttuple;
Oid tsmexaminetuple;
Oid tsmend;
Oid tsmreset;
Oid tsmcost;
Node *repeatable;
List *args;
} TableSampleClause;
/*
* A_Star - '*' representing all columns of a table or compound field
*
@@ -558,19 +538,23 @@ typedef struct RangeFunction
} RangeFunction;
/*
* RangeTableSample - represents <table> TABLESAMPLE <method> (<params>) REPEATABLE (<num>)
* RangeTableSample - TABLESAMPLE appearing in a raw FROM clause
*
* SQL Standard specifies only one parameter which is percentage. But we allow
* custom tablesample methods which may need different input arguments so we
* accept list of arguments.
* This node, appearing only in raw parse trees, represents
* <relation> TABLESAMPLE <method> (<params>) REPEATABLE (<num>)
* Currently, the <relation> can only be a RangeVar, but we might in future
* allow RangeSubselect and other options. Note that the RangeTableSample
* is wrapped around the node representing the <relation>, rather than being
* a subfield of it.
*/
typedef struct RangeTableSample
{
NodeTag type;
RangeVar *relation;
char *method; /* sampling method */
Node *repeatable;
List *args; /* arguments for sampling method */
Node *relation; /* relation to be sampled */
List *method; /* sampling method name (possibly qualified) */
List *args; /* argument(s) for sampling method */
Node *repeatable; /* REPEATABLE expression, or NULL if none */
int location; /* method name location, or -1 if unknown */
} RangeTableSample;
/*
@@ -810,7 +794,7 @@ typedef struct RangeTblEntry
*/
Oid relid; /* OID of the relation */
char relkind; /* relation kind (see pg_class.relkind) */
TableSampleClause *tablesample; /* sampling method and parameters */
struct TableSampleClause *tablesample; /* sampling info, or NULL */
/*
* Fields valid for a subquery RTE (else NULL):
@@ -912,6 +896,19 @@ typedef struct RangeTblFunction
Bitmapset *funcparams; /* PARAM_EXEC Param IDs affecting this func */
} RangeTblFunction;
/*
* TableSampleClause - TABLESAMPLE appearing in a transformed FROM clause
*
* Unlike RangeTableSample, this is a subnode of the relevant RangeTblEntry.
*/
typedef struct TableSampleClause
{
NodeTag type;
Oid tsmhandler; /* OID of the tablesample handler function */
List *args; /* tablesample argument expression(s) */
Expr *repeatable; /* REPEATABLE expression, or NULL if none */
} TableSampleClause;
/*
* WithCheckOption -
* representation of WITH CHECK OPTION checks to be applied to new tuples
@@ -2520,7 +2517,7 @@ typedef struct RenameStmt
typedef struct AlterObjectSchemaStmt
{
NodeTag type;
ObjectType objectType; /* OBJECT_TABLE, OBJECT_TYPE, etc */
ObjectType objectType; /* OBJECT_TABLE, OBJECT_TYPE, etc */
RangeVar *relation; /* in case it's a table */
List *object; /* in case it's some other object */
List *objarg; /* argument types, if applicable */
@@ -2535,7 +2532,7 @@ typedef struct AlterObjectSchemaStmt
typedef struct AlterOwnerStmt
{
NodeTag type;
ObjectType objectType; /* OBJECT_TABLE, OBJECT_TYPE, etc */
ObjectType objectType; /* OBJECT_TABLE, OBJECT_TYPE, etc */
RangeVar *relation; /* in case it's a table */
List *object; /* in case it's some other object */
List *objarg; /* argument types, if applicable */