diff --git a/contrib/pg_selectivities/.gitignore b/contrib/pg_selectivities/.gitignore new file mode 100644 index 0000000..5dcb3ff --- /dev/null +++ b/contrib/pg_selectivities/.gitignore @@ -0,0 +1,4 @@ +# Generated subdirectories +/log/ +/results/ +/tmp_check/ diff --git a/contrib/pg_selectivities/Makefile b/contrib/pg_selectivities/Makefile new file mode 100644 index 0000000..9843326 --- /dev/null +++ b/contrib/pg_selectivities/Makefile @@ -0,0 +1,21 @@ +# contrib/pg_selectivities/Makefile + +MODULE_big = pg_selectivities +OBJS = pg_selectivities.o $(WIN32RES) + +EXTENSION = pg_selectivities +DATA = pg_selectivities--1.0.sql +PGFILEDESC = "pg_selectivities - functions to implemente planner hints" + +REGRESS = pg_selectivities + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = contrib/pg_selectivities +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/contrib/pg_selectivities/expected/pg_selectivities.out b/contrib/pg_selectivities/expected/pg_selectivities.out new file mode 100644 index 0000000..11fdf3f --- /dev/null +++ b/contrib/pg_selectivities/expected/pg_selectivities.out @@ -0,0 +1,30 @@ +set cpu_operator_cost TO 0; +set cpu_tuple_cost TO 0; +create extension pg_selectivities; +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=7; + QUERY PLAN +-------------------------------------------------------------------------- + Function Scan on generate_series (cost=0.00..0.00 rows=1000000 width=4) +(1 row) + +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=7 and pg_always(1); + QUERY PLAN +-------------------------------------------------------------------------- + Function Scan on generate_series (cost=0.00..0.00 rows=1000000 width=4) + Filter: pg_always('1'::double precision) +(2 rows) + +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=7 and pg_always(0.001); + QUERY PLAN +----------------------------------------------------------------------- + Function Scan on generate_series (cost=0.00..0.00 rows=1000 width=4) + Filter: pg_always('0.001'::double precision) +(2 rows) + +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=6 or pg_never(0.01); + QUERY PLAN +------------------------------------------------------------------------ + Function Scan on generate_series (cost=0.00..0.00 rows=10000 width=4) + Filter: pg_never('0.01'::double precision) +(2 rows) + diff --git a/contrib/pg_selectivities/pg_selectivities--1.0.sql b/contrib/pg_selectivities/pg_selectivities--1.0.sql new file mode 100644 index 0000000..90307a8 --- /dev/null +++ b/contrib/pg_selectivities/pg_selectivities--1.0.sql @@ -0,0 +1,21 @@ +/* contrib/pg_selectivities/pg_selectivities--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION pg_selectivities" to load this file. \quit + +CREATE FUNCTION pg_selectivities_support(internal) +RETURNS internal +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE FUNCTION pg_always(float) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT PARALLEL SAFE +SUPPORT pg_selectivities_support; + +CREATE FUNCTION pg_never(float) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT PARALLEL SAFE +SUPPORT pg_selectivities_support; diff --git a/contrib/pg_selectivities/pg_selectivities.c b/contrib/pg_selectivities/pg_selectivities.c new file mode 100644 index 0000000..9a8ef54 --- /dev/null +++ b/contrib/pg_selectivities/pg_selectivities.c @@ -0,0 +1,50 @@ +/* + * contrib/pg_selectivities/pg_selectivities.c + */ +#include "postgres.h" +#include "nodes/supportnodes.h" +#include "fmgr.h" + +PG_MODULE_MAGIC; + +PG_FUNCTION_INFO_V1(pg_selectivities_support); +PG_FUNCTION_INFO_V1(pg_always); +PG_FUNCTION_INFO_V1(pg_never); + +Datum +pg_selectivities_support(PG_FUNCTION_ARGS) +{ + Node *rawreq = (Node *) PG_GETARG_POINTER(0); + Node *ret = NULL; + + if (IsA(rawreq, SupportRequestSelectivity)) + { + SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq; + if (list_length(req->args) == 1) + { + Node * node; + node=(Node*) linitial(req->args); + if (IsA(node, Const)) + { + Selectivity s1; + s1= DatumGetFloat8(((Const*)node)->constvalue); + req->selectivity = s1; + ret = (Node *) req; + } + } + } + + PG_RETURN_POINTER(ret); +} + +Datum +pg_always(PG_FUNCTION_ARGS) +{ + PG_RETURN_BOOL(true); +} + +Datum +pg_never(PG_FUNCTION_ARGS) +{ + PG_RETURN_BOOL(false); +} diff --git a/contrib/pg_selectivities/pg_selectivities.control b/contrib/pg_selectivities/pg_selectivities.control new file mode 100644 index 0000000..1605068 --- /dev/null +++ b/contrib/pg_selectivities/pg_selectivities.control @@ -0,0 +1,5 @@ +# pg_selectivities extension +comment = 'functions which always return true or false, but report a custom selectivity' +default_version = '1.0' +module_pathname = '$libdir/pg_selectivities' +relocatable = true diff --git a/contrib/pg_selectivities/sql/pg_selectivities.sql b/contrib/pg_selectivities/sql/pg_selectivities.sql new file mode 100644 index 0000000..47dd0fc --- /dev/null +++ b/contrib/pg_selectivities/sql/pg_selectivities.sql @@ -0,0 +1,8 @@ +set cpu_operator_cost TO 0; +set cpu_tuple_cost TO 0; +create extension pg_selectivities; + +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=7; +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=7 and pg_always(1); +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=7 and pg_always(0.001); +explain select * from (select 7 from generate_series(1,1000000)) foo(x) where x=6 or pg_never(0.01);