From f6d208d6e51810c73f0e02c477984a6b44627f11 Mon Sep 17 00:00:00 2001 From: Simon Riggs Date: Fri, 15 May 2015 14:37:10 -0400 Subject: TABLESAMPLE, SQL Standard and extensible Add a TABLESAMPLE clause to SELECT statements that allows user to specify random BERNOULLI sampling or block level SYSTEM sampling. Implementation allows for extensible sampling functions to be written, using a standard API. Basic version follows SQLStandard exactly. Usable concrete use cases for the sampling API follow in later commits. Petr Jelinek Reviewed by Michael Paquier and Simon Riggs --- src/include/nodes/execnodes.h | 9 +++++++++ src/include/nodes/nodes.h | 4 ++++ src/include/nodes/parsenodes.h | 37 +++++++++++++++++++++++++++++++++++++ src/include/nodes/plannodes.h | 6 ++++++ 4 files changed, 56 insertions(+) (limited to 'src/include/nodes') diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index fcfe1107f92..972368019a9 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -1237,6 +1237,15 @@ typedef struct ScanState */ typedef ScanState SeqScanState; +/* + * SampleScan + */ +typedef struct SampleScanState +{ + ScanState ss; + struct TableSampleDesc *tsdesc; +} SampleScanState; + /* * These structs store information about index quals that don't have simple * constant right-hand sides. See comments for ExecIndexBuildScanKeys() diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 768f413a451..8b275f6e263 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -61,6 +61,7 @@ typedef enum NodeTag T_ValuesScan, T_CteScan, T_WorkTableScan, + T_SampleScan, T_ForeignScan, T_CustomScan, T_Join, @@ -97,6 +98,7 @@ typedef enum NodeTag T_BitmapOrState, T_ScanState, T_SeqScanState, + T_SampleScanState, T_IndexScanState, T_IndexOnlyScanState, T_BitmapIndexScanState, @@ -419,6 +421,8 @@ typedef enum NodeTag T_OnConflictClause, T_CommonTableExpr, T_RoleSpec, + T_RangeTableSample, + T_TableSampleClause, /* * TAGS FOR REPLICATION GRAMMAR PARSE NODES (replnodes.h) diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 053f1b01213..6723f46f3f1 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -335,6 +335,26 @@ 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 * @@ -535,6 +555,22 @@ typedef struct RangeFunction * of function returning RECORD */ } RangeFunction; +/* + * RangeTableSample - represents TABLESAMPLE () REPEATABLE () + * + * 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. + */ +typedef struct RangeTableSample +{ + NodeTag type; + RangeVar *relation; + char *method; /* sampling method */ + Node *repeatable; + List *args; /* arguments for sampling method */ +} RangeTableSample; + /* * ColumnDef - column definition (used in various creates) * @@ -772,6 +808,7 @@ typedef struct RangeTblEntry */ Oid relid; /* OID of the relation */ char relkind; /* relation kind (see pg_class.relkind) */ + TableSampleClause *tablesample; /* sampling method and parameters */ /* * Fields valid for a subquery RTE (else NULL): diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h index 65f71d81705..4e655b0e6c1 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -286,6 +286,12 @@ typedef struct Scan */ typedef Scan SeqScan; +/* ---------------- + * table sample scan node + * ---------------- + */ +typedef Scan SampleScan; + /* ---------------- * index scan node * -- cgit v1.2.3