Redesign tablesample method API, and do extensive code review.

Tom Lane <tgl@sss.pgh.pa.us>

Commit: 6fcb337fa507723d6940ed8e5658d3da1fac6195
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: 2015-07-25T18:39:00Z
Releases: 9.5.0
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.

Files

PathChange+/−
contrib/pg_stat_statements/pg_stat_statements.c modified +10 −0
contrib/tsm_system_rows/expected/tsm_system_rows.out modified +74 −22
contrib/tsm_system_rows/Makefile modified +2 −2
contrib/tsm_system_rows/sql/tsm_system_rows.sql modified +33 −8
contrib/tsm_system_rows/tsm_system_rows--1.0.sql modified +4 −39
contrib/tsm_system_rows/tsm_system_rows.c modified +284 −161
contrib/tsm_system_rows/tsm_system_rows.control modified +1 −1
contrib/tsm_system_time/expected/tsm_system_time.out modified +92 −46
contrib/tsm_system_time/Makefile modified +2 −2
contrib/tsm_system_time/sql/tsm_system_time.sql modified +45 −8
contrib/tsm_system_time/tsm_system_time--1.0.sql modified +4 −34
contrib/tsm_system_time/tsm_system_time.c modified +247 −206
contrib/tsm_system_time/tsm_system_time.control modified +1 −1
doc/src/sgml/catalogs.sgml modified +0 −120
doc/src/sgml/datatype.sgml modified +10 −1
doc/src/sgml/postgres.sgml modified +1 −1
doc/src/sgml/ref/select.sgml modified +67 −59
doc/src/sgml/tablesample-method.sgml modified +242 −80
doc/src/sgml/tsm-system-rows.sgml modified +26 −13
doc/src/sgml/tsm-system-time.sgml modified +28 −14
src/backend/access/heap/heapam.c modified +48 −13
src/backend/access/tablesample/bernoulli.c modified +162 −164
src/backend/access/tablesample/Makefile modified +3 −3
src/backend/access/tablesample/system.c modified +193 −119
src/backend/access/tablesample/tablesample.c modified +15 −340
src/backend/catalog/dependency.c modified +8 −0
src/backend/catalog/Makefile modified +3 −2
src/backend/commands/explain.c modified +81 −26
src/backend/executor/execAmi.c modified +4 −3
src/backend/executor/nodeSamplescan.c modified +391 −46
src/backend/nodes/copyfuncs.c modified +56 −59
src/backend/nodes/equalfuncs.c modified +28 −36
src/backend/nodes/nodeFuncs.c modified +44 −31
src/backend/nodes/outfuncs.c modified +41 −47
src/backend/nodes/readfuncs.c modified +17 −44
src/backend/optimizer/path/allpaths.c modified +90 −10
src/backend/optimizer/path/costsize.c modified +30 −27
src/backend/optimizer/plan/createplan.c modified +22 −12
src/backend/optimizer/plan/initsplan.c modified +3 −1
src/backend/optimizer/plan/planner.c modified +9 −10
src/backend/optimizer/plan/setrefs.c modified +10 −8
src/backend/optimizer/plan/subselect.c modified +6 −1
src/backend/optimizer/prep/prepjointree.c modified +11 −2
src/backend/optimizer/util/pathnode.c modified +4 −4
src/backend/parser/gram.y modified +13 −14
src/backend/parser/parse_clause.c modified +134 −56
src/backend/parser/parse_func.c modified +0 −144
src/backend/rewrite/rewriteHandler.c modified +4 −0
src/backend/utils/adt/pseudotypes.c modified +27 −0
src/backend/utils/adt/ruleutils.c modified +44 −50
src/backend/utils/cache/lsyscache.c modified +0 −27
src/backend/utils/cache/syscache.c modified +0 −23
src/backend/utils/errcodes.txt modified +2 −0
src/backend/utils/misc/sampling.c modified +1 −1
src/bin/psql/tab-complete.c modified +6 −4
src/include/access/heapam.h modified +3 −1
src/include/access/tablesample.h deleted +0 −61
src/include/access/tsmapi.h added +81 −0
src/include/catalog/catversion.h modified +1 −1
src/include/catalog/indexing.h modified +0 −5
src/include/catalog/pg_proc.h modified +10 −27
src/include/catalog/pg_tablesample_method.h deleted +0 −81
src/include/catalog/pg_type.h modified +2 −0
src/include/executor/nodeSamplescan.h modified +1 −1
src/include/nodes/execnodes.h modified +12 −3
src/include/nodes/nodes.h modified +5 −4
src/include/nodes/parsenodes.h modified +28 −31
src/include/nodes/plannodes.h modified +6 −1
src/include/optimizer/cost.h modified +2 −1
src/include/parser/parse_func.h modified +0 −5
src/include/port.h modified +0 −4
src/include/utils/builtins.h modified +8 −0
src/include/utils/lsyscache.h modified +0 −1
src/include/utils/syscache.h modified +0 −2
src/port/erand48.c modified +3 −0
src/test/regress/expected/rowsecurity.out modified +13 −11
src/test/regress/expected/rules.out modified +4 −0
src/test/regress/expected/sanity_check.out modified +0 −1
src/test/regress/expected/tablesample.out modified +184 −100
src/test/regress/output/misc.source modified +4 −1
src/test/regress/serial_schedule modified +1 −1
src/test/regress/sql/rowsecurity.sql modified +6 −2
src/test/regress/sql/tablesample.sql modified +59 −31