1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-18 17:42:25 +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

@ -444,6 +444,16 @@ _outSeqScan(StringInfo str, const SeqScan *node)
_outScanInfo(str, (const Scan *) node);
}
static void
_outSampleScan(StringInfo str, const SampleScan *node)
{
WRITE_NODE_TYPE("SAMPLESCAN");
_outScanInfo(str, (const Scan *) node);
WRITE_NODE_FIELD(tablesample);
}
static void
_outIndexScan(StringInfo str, const IndexScan *node)
{
@ -591,14 +601,6 @@ _outCustomScan(StringInfo str, const CustomScan *node)
node->methods->TextOutCustomScan(str, node);
}
static void
_outSampleScan(StringInfo str, const SampleScan *node)
{
WRITE_NODE_TYPE("SAMPLESCAN");
_outScanInfo(str, (const Scan *) node);
}
static void
_outJoin(StringInfo str, const Join *node)
{
@ -2478,36 +2480,6 @@ _outCommonTableExpr(StringInfo str, const CommonTableExpr *node)
WRITE_NODE_FIELD(ctecolcollations);
}
static void
_outRangeTableSample(StringInfo str, const RangeTableSample *node)
{
WRITE_NODE_TYPE("RANGETABLESAMPLE");
WRITE_NODE_FIELD(relation);
WRITE_STRING_FIELD(method);
WRITE_NODE_FIELD(repeatable);
WRITE_NODE_FIELD(args);
}
static void
_outTableSampleClause(StringInfo str, const TableSampleClause *node)
{
WRITE_NODE_TYPE("TABLESAMPLECLAUSE");
WRITE_OID_FIELD(tsmid);
WRITE_BOOL_FIELD(tsmseqscan);
WRITE_BOOL_FIELD(tsmpagemode);
WRITE_OID_FIELD(tsminit);
WRITE_OID_FIELD(tsmnextblock);
WRITE_OID_FIELD(tsmnexttuple);
WRITE_OID_FIELD(tsmexaminetuple);
WRITE_OID_FIELD(tsmend);
WRITE_OID_FIELD(tsmreset);
WRITE_OID_FIELD(tsmcost);
WRITE_NODE_FIELD(repeatable);
WRITE_NODE_FIELD(args);
}
static void
_outSetOperationStmt(StringInfo str, const SetOperationStmt *node)
{
@ -2594,6 +2566,16 @@ _outRangeTblFunction(StringInfo str, const RangeTblFunction *node)
WRITE_BITMAPSET_FIELD(funcparams);
}
static void
_outTableSampleClause(StringInfo str, const TableSampleClause *node)
{
WRITE_NODE_TYPE("TABLESAMPLECLAUSE");
WRITE_OID_FIELD(tsmhandler);
WRITE_NODE_FIELD(args);
WRITE_NODE_FIELD(repeatable);
}
static void
_outAExpr(StringInfo str, const A_Expr *node)
{
@ -2845,6 +2827,18 @@ _outRangeFunction(StringInfo str, const RangeFunction *node)
WRITE_NODE_FIELD(coldeflist);
}
static void
_outRangeTableSample(StringInfo str, const RangeTableSample *node)
{
WRITE_NODE_TYPE("RANGETABLESAMPLE");
WRITE_NODE_FIELD(relation);
WRITE_NODE_FIELD(method);
WRITE_NODE_FIELD(args);
WRITE_NODE_FIELD(repeatable);
WRITE_LOCATION_FIELD(location);
}
static void
_outConstraint(StringInfo str, const Constraint *node)
{
@ -3002,6 +2996,9 @@ _outNode(StringInfo str, const void *obj)
case T_SeqScan:
_outSeqScan(str, obj);
break;
case T_SampleScan:
_outSampleScan(str, obj);
break;
case T_IndexScan:
_outIndexScan(str, obj);
break;
@ -3038,9 +3035,6 @@ _outNode(StringInfo str, const void *obj)
case T_CustomScan:
_outCustomScan(str, obj);
break;
case T_SampleScan:
_outSampleScan(str, obj);
break;
case T_Join:
_outJoin(str, obj);
break;
@ -3393,12 +3387,6 @@ _outNode(StringInfo str, const void *obj)
case T_CommonTableExpr:
_outCommonTableExpr(str, obj);
break;
case T_RangeTableSample:
_outRangeTableSample(str, obj);
break;
case T_TableSampleClause:
_outTableSampleClause(str, obj);
break;
case T_SetOperationStmt:
_outSetOperationStmt(str, obj);
break;
@ -3408,6 +3396,9 @@ _outNode(StringInfo str, const void *obj)
case T_RangeTblFunction:
_outRangeTblFunction(str, obj);
break;
case T_TableSampleClause:
_outTableSampleClause(str, obj);
break;
case T_A_Expr:
_outAExpr(str, obj);
break;
@ -3450,6 +3441,9 @@ _outNode(StringInfo str, const void *obj)
case T_RangeFunction:
_outRangeFunction(str, obj);
break;
case T_RangeTableSample:
_outRangeTableSample(str, obj);
break;
case T_Constraint:
_outConstraint(str, obj);
break;